双链表的情况与单链表类似,只是增加了一个前置链而已。
#include<stdio.h>
#include<stdlib.h>
typedef struct node * pnode;
typedef struct node
{
int data;
pnode pre;
pnode nect;
}Node;
pnode create(int m)
{
pnode head = (pnode)malloc(sizeof(Node));
pnode q = head;
head->data = m;
m--;
while(m)
{
pnode p = (pnode)malloc(sizeof(Node));
q->nect = p;
p->data = m;
p->pre = q;
q = p;
m--;
}
q->nect = NULL;
return head;
}
int main()
{
int data = 1;
int i = 0;
pnode head;
head = create(5);
for(;i < 5;i++)
{
printf("%d",head->data);
if(head->nect != NULL)
{
head = head->nect;
}
}
for(;i < 10;i++)
{
printf("%d",head->data);
head = head->pre;
}
return 0;
}