#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);
}
}
C语言双链表,可实现增删改查功能,还可以将链表反转。
最新推荐文章于 2024-02-21 21:19:50 发布