写链表的代码,实现链表
按值查找返回位置的功能

按位置查找返回值

释放单链表

链表逆置

#ifndef __LINK_H__
#define __LINK_H__
#include <stdio.h>
#include <stdlib.h>
typedef int datatype;
typedef struct node
{
union
{
datatype data;
int len;
};
struct node *next;
}node,*node_p;
//1、创建单链表
node_p create_link_list();
//2、创建结点
node_p create_node(datatype data);
//3、头插
void insert_head(node_p H,datatype data);
//4、判空
int empty_link(node_p H);
//5、头删
void dele_head(node_p H);
//6、输出
void show(node_p H);
//7、尾插
void insert_tail(node_p H,datatype data);
//8、尾删
void dele_tail(node_p H);
//9、按位置插入
void insert_pos(node_p H,datatype data,int pos);
//10、按位置删除
void dele_pos(node_p H,int pos);
//11、按值查找,返回元素位置
int search_value(node_p H,datatype key);
//12、按位置查找,返回值
int search_pos(node_p H,int pos);
//13、释放单链表
void free_link_list(node_p H);
//14、链表逆置
void inverse_link_list(node_p H);
#endif
#include "link.h"
int main()
{
node_p H=create_link_list();
insert_head(H,10);
insert_head(H,20);
insert_head(H,30);
insert_head(H,40);
show(H);
inverse_link_list(H);
show(H);
return 0;
}
#include "link.h"
//1、创建单链表
node_p create_link_list()
{
node_p H=(node_p)malloc(sizeof(node));
if(H==NULL)
{
return NULL;
}
H->len=0;
H->next=NULL;
return H;
}
//2、创建结点
node_p create_node(datatype data)
{
node_p new=(node_p)malloc(sizeof(node));
if(new==NULL)
{
return NULL;
}
new->data=data;
return new;
}
//3、头插
void insert_head(node_p H,datatype data)
{
if(H==NULL)
{
printf("入参为空\n");
return;
}
node_p new=create_node(data);
new->next=H->next;
H->next=new;
H->len++;
}
//4、判空
int empty_link(node_p H)
{
if(H==NULL)
{
printf("入参为空\n");
return -1;
}
return H->next==NULL? 1:0;
}
//5、头删
void dele_head(node_p H)
{
if(H==NULL)
{
printf("入参为空\n");
return;
}
if(empty_link(H))
{
printf("链表为空\n");
return;
}
node_p del = H->next;
H->next=H->next->next;
free(del);
H->len--;
}
//6、输出
void show(node_p H)
{
if(H==NULL)
{
printf("入参为空\n");
return;
}
if(empty_link(H))
{
printf("链表为空\n");
return;
}
node_p p=H->next;
while(p!=NULL)
{
printf("%d->",p->data);
p=p->next;
}
printf("NULL\n");
/*
node_p new=create_node(1);
new->next=H->next;
while(new->next!=NULL)
{
printf("%d\n",new->next->data);
new->next=new->next->next;
}*/
}
//7、尾插
void insert_tail(node_p H,datatype data)
{
if(H==NULL)
{
printf("入参为空\n");
return;
}
node_p p=H->next;
while(p->next!=NULL)
{
p=p->next;
}
node_p new = create_node(data);
p->next = new;
new->next = NULL;
H->len++;
}
//8、尾删
void dele_tail(node_p H)
{
if(H==NULL)
{
printf("入参为空,请检查\n");
return;
}
node_p p= H;
while(p->next->next!=NULL)
{
p=p->next;
}
node_p del= p->next;
free(del);
p->next=NULL;
H->len--;
}
//9、按位置插入
void insert_pos(node_p H,datatype data,int pos)
{
if(H==NULL)
{
printf("入参为空,请检查\n");
return;
}
if(pos<0||pos>H->len+1)
{
printf("请输入正确的序号\n");
}
node_p p=H;
for(int i=0;i<pos-1;i++)
{
p=p->next;
}
node_p new = create_node(data);
new->next=p->next;
p->next=new;
H->len++;
}
//10、按位置删除
void dele_pos(node_p H,int pos)
{
if(H==NULL)
{
printf("入参为空,请检查\n");
return;
}
if(pos<1||pos>H->len)
{
printf("请输入正确的序号\n");
}
node_p p=H;
for(int i=0;i<pos-1;i++)
{
p=p->next;
}
node_p del=p->next;
p->next=p->next->next;
free(del);
H->len--;
}
//11、按值查找,返回元素位置
int search_value(node_p H,datatype key)
{
if(H==NULL)
{
printf("入参为空,请检查\n");
return -1;
}
node_p p=H;
int pos=0;
while(p!=NULL)
{
if(p->data==key)
{
return pos;
}
pos++;
p=p->next;
}
printf("没有该值\n");
}
//12、按位置查找,返回值
int search_pos(node_p H,int pos)
{
if(H==NULL)
{
printf("入参为空,请检查\n");
return -1;
}
node_p p=H;
for(int i=0;i<pos;i++)
{
p=p->next;
}
return p->data;
}
//13、释放单链表
void free_link_list(node_p H)
{
if(H==NULL)
{
printf("入参为空,请检查\n");
return ;
}
if(empty_link(H))
{
printf("链表为空\n");
}
node_p p=H;
int i=0;
node_p arr[99];
while(p !=NULL)
{
arr[i]=p;
i++;
p=p->next;
}
i--;
for(;i>1;i--)
{
free(arr[i]);
}
H->next=NULL;
}
//14、链表逆置
void inverse_link_list(node_p H)
{
if(H==NULL)
{
printf("入参为空,请检查\n");
return ;
}
if(empty_link(H))
{
printf("链表为空\n");
}
node_p p=H;
int i=0;
node_p arr[99];
while(p !=NULL)
{
arr[i]=p;
i++;
p=p->next;
}
i--;
H->next=arr[i];
for(;i>1;i--)
{
arr[i]->next=arr[i-1];
}
arr[i]->next=NULL;
}
C语言实现基础单链表操作:创建、插入、删除、查找和链表逆置
本文详细介绍了如何使用C语言实现单链表,包括创建链表、节点操作(头插、尾插、头删、尾删)、按值和位置查找以及链表的释放和逆置功能。
6895

被折叠的 条评论
为什么被折叠?



