C语言双链表,可实现增删改查功能,还可以将链表反转。

本文介绍了一个双向链表的实现及基本操作,包括创建、插入、删除和反转等,并提供了完整的C语言代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int  number=0;
typedef struct Node
{
int data;
struct Node *next;
struct Node *prior;
}Node,*Dlinklist;
Dlinklist Dlinklistcreate() 
{
Node *L,*r,*p;
L=(Node *)malloc(sizeof(Node));
int x;
L->next=NULL;
r=L;
r->next=NULL;
printf("please input the interger\n");
while(scanf("%d",&x)!=EOF)
{
 p=(Node *)malloc(sizeof(Node));
 p->data=x;
 p->next=r->next;
 r->next=p;
 p->prior=r;
 r=p;
 number++;
}
r->next=NULL;
return L;
}
Dlinklist Dlinklistreverse(Dlinklist L)
{
Node *p,*q,*r;
p=L->next;
q=p->next;
p->next=NULL;
while(q)
{
 r=q->next;
 q->next=p;
 p=q;
 q=r;
}
L->next=p;
}
Dlinklist Dlinklistinsert(Dlinklist L,int i,int x)
{
Node *p,*q;
p=L->next;
q=(Node *)malloc(sizeof(Node));
int count;
printf("please insert the position and the number\n");
for(count=1;count<i-1;count++)
 {
  p=p->next;
 }
q->data=x;
q->next=p->next;
p->next->prior=q;
p->next=q;
q->prior=p;
return L;
}
Dlinklist DlinklistDelete(Dlinklist L,int i)
{
Dlinklist p;
p=L->next;
int count=1;
for(count=1;count<i;count++)
 {
 p=p->next;
 }
if(p==NULL)
 printf("The position is not illegal");
else if(p->next==NULL)
 {
 p->prior->next=NULL;
 free(p);
 }
else
 {
 p->prior->next=p->next;
 p->next->prior=p->prior;
 free(p);
 }
}
void main()
{
 Dlinklist list,start;
 list=Dlinklistcreate();
 int i;
 int x; 
 for(start=list->next;start!=NULL;start=start->next)
  {
  printf("%d\n",start->data);
  }
 scanf("%d",&i);
 scanf("%d",&x);
 Dlinklistinsert(list, i,x);
 for(start=list->next;start!=NULL;start=start->next)
  {
  printf("%d\n",start->data);
  }
 printf("please input the delete position\n");
 scanf("%d",&i);
 DlinklistDelete(list, i);
 for(start=list->next;start!=NULL;start=start->next)
  {
  printf("%d\n",start->data);
  }
 printf("output the reverse list\n");
 Dlinklistreverse(list);
 for(start=list->next;start!=NULL;start=start->next)
  {
  printf("%d\n",start->data);
  }
 for(start=list->next;start!=NULL;start=start->next)
  {
  free(start);
  }  
}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 


二.内核链表 内核链表是一种链表,Linux内核中的链表都是用这种形式实现的 1.特性 内核链表是一种双向循环链表,内核链表的节点节点结构中只有指针域 使用内核链表的时候,将内核链表作为一个成员放入到一个结构体中使用 我们在链表中找到内核链表结构的地址,通过这个地址就可以找到外部大结构体的地址,通过大结构体就可以访问其中的成员 优势: 内核链表突破了保存数据的限制,可以用内核链表来保存任何数据(使用一种链表表示各种类型的数据,通用性很强) 内核链表中只有指针域,维护起来更加方便,效率更高 2.使用 内核链表在内核中已经被实现,我们只需要调用其接口直接使用即可 内核链表的实现代码在内核源代码的list.h文件中 3.源代码分析 (1)节点结构: struct list_head { struct list_head *next, *prev;//前置指针 后置指针 }; (2)初始化 #define INIT_LIST_HEAD(ptr) do { \ (ptr)->next = (ptr); (ptr)->prev = (ptr); \ } while (0) (3)插入 //从头部插入 static inline void list_add(struct list_head *new, struct list_head *head)//传入要插入的节点和要插入的链表 { __list_add(new, head, head->next); } //从尾部插入 static inline void list_add_tail(struct list_head *new, struct list_head *head) { __list_add(new, head->prev, head); } (4)通过节点找到外部结构体的地址 //返回外部结构体的地址,第一个参数是节点地址,第二个参数是外部结构体的类型名,第三个参数是节点在外部结构体中的成员名 #define list_entry(ptr, type, member) ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member))) (5)遍历内核链表 //遍历内核链表 #define list_for_each(pos, head) \ for (pos = (head)->next; pos != (head); \ pos = pos->next) //安全遍历内核链表 #define list_for_each_safe(pos, n, head) \ for (pos = (head)->next, n = pos->next; pos != (head); \ pos = n, n = pos->next) C语言双链表,可以增加。插入,删除,修改,销毁等函数接口。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值