双向链表的建立就是在每个链表结点加上一个父指针和一个子指针,一个指向前一个结点,一个指向后一个结点。
#include<stdio.h>
#include<stdlib.h>
int n;
typedef struct node
{
struct node *pre;
int data;
struct node *next;
}link;
#define len sizeof(link)
link *creat()
{
link *head,*p1,*p2;
p1=p2=(link *)malloc(len);
scanf("%d",&p1->data);n=0;
while(p1->data!=0)
{
n++;
if(n==1) head=p1;
else{p2->next = p1; p1->pre = p2;}
p2=p1;
p1=(link *)malloc(len);
scanf("%d",&p1->data);
}
free((void*)p1);
p2->next=NULL;
return head;
}
本文介绍了一种使用C语言实现双向链表的方法。通过为每个节点添加前驱和后继指针,可以实现在链表中方便地进行双向遍历。文章提供了一个具体的创建双向链表的函数实现。
2240

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



