双链表程序插入和删除

tondayong1981 2004-06-20 03:22:53
帮忙看一下双链表程序是否正确????????
#define NULL 0
typedef struct dnode
{int data;
struct dnode *prior,*next;
}dlinklist;

dlinklist *createdlinklist()
{ int data;
dlinklist *head,*s,*r;
head=(dlinklist *)malloc(sizeof(dlinklist));
r=head;
scanf("%d",&data);
while(data!=-1)
{s=(dlinklist *)malloc(sizeof(dlinklist));
s->data=data;
r->next=s;
s->prior=r;
r=s;
scanf("%d",&data);
}
r->next=head;
return head;
}


printdlinklist(dlinklist *head)
{dlinklist *p;
p=head->next;
if(head==NULL)
printf("dlinklist not exist:\n");
else
{printf("head");
while(p!=head)
{printf("->%3d",p->data);
p=p->next;
}
printf("->end\n");
}
}

INSERTBEFORE(dlinklist *head,int x,int y)
{dlinklist *s,*p;
s=(dlinklist *)malloc(sizeof(dlinklist));
s->data=y;
p=head->next;
while((p!=head)&&(p->data!=x))
p=p->next;
s->prior=p->prior;
s->next=p;
p->prior->next=s;
p->prior=s;
}

DELETE(dlinklist *head,int m)
{dlinklist *p;
p=head->next;
while((p!=head)&&(p->data!=m))
p=p->next;
p->prior->next=p->next;
p->next->prior=p->prior;
free(p);
}

main()
{dlinklist *head;
int x,y,m;
printf("please input some numbers to create a dlinjklist:\n");
head=createdlinklist();
printf("The data are :\n");
printdlinklist(head);
printf("please input x and y,y will be insertwd before x:\n");
scanf("%d%d",&x,&y);
INSERTBEFORE(head,x,y);
printdlinklist(head);
printf("please input the number to ne deleted:\n");
scanf("%d",&m);
DELETE(head,m);
printdlinklist(head);
printf("\n");
}
...全文
136 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
peter9606 2004-06-28
  • 打赏
  • 举报
回复
printf("please input x and y,y will be insertwd before x:\n");
scanf("%d%d",&x,&y);

分开写就对了
scanf("%d",&x);
scanf("%d",&y);
haha52 2004-06-20
  • 打赏
  • 举报
回复
UP
insulator 2004-06-20
  • 打赏
  • 举报
回复
好像dlinklist *createdlinklist()最后少一句
head->prior=r
这样才能形成循环

loveyou19840806 2004-06-20
  • 打赏
  • 举报
回复
你自己测试一下吧,

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧