#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
int num;
struct node *next;
}*p,*q,*head,*temp,*p1,*endpointer,*p2,*p3;
int main()
{
/*create*/
p=q=p1=p2=head=temp=(struct node*)malloc(sizeof(struct node));
printf("input num ");scanf("%d",&p->num);
p->next=NULL;
while(p->num)
{
q=p;
p=(struct node*)malloc(sizeof(struct node));
printf("input num ");scanf("%d",&p->num);
q->next=p;
}
q->next=NULL;
endpointer=q;
printf("结果:");
while(temp)
{
printf("%2d ",temp->num);
temp=temp->next;
}
putchar('\n');
/*insert*/
free(temp);
temp=(struct node*)malloc(sizeof(struct node));
printf("input insert num");
scanf("%d",&temp->num);
while(p1!=NULL && p1->num<temp->num)
{
p=p1;
p1=p1->next;
}
if(head->num>temp->num)/*top*/
{
p2=temp;
temp->next=p1;
}
else if(endpointer->num<temp->num)/*end*/
{
p->next=temp;
temp->next=NULL;
endpointer=temp;
}
else /*medium*/
{
p->next=temp;
temp->next=p1;
}
head=p2;
printf("结果:");
while(p2)
{
printf("%2d ",p2->num);
p2=p2->next;
}
putchar('\n');
/*delete*/
free(temp);free(p1);free(p);
p1=head;
int nun;
printf("input delete num");
scanf("%d",&nun);
while(p1->next!=NULL && p1->num!=nun)
{
p=p1;
p1=p1->next;
}
if(head->num==nun==p1->num)
{
head=p1->next;
}
else if(endpointer->num==nun==p1->num)
{
p->next=NULL;
}
else
{
p->next=p1->next;
}
printf("结果:");
while(head)
{
printf("%2d ",head->num);
head=head->next;
}
putchar('\n');
}
有人看下这个链表哪错了,创建、插入没出问题,删除时出错了。
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- threenewbee 2019-06-23 12:33关注
/*delete*/
//free(temp);
//free(p1);
//free(p);
这三行去掉本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 YoloV5 第三方库的版本对照问题
- ¥15 请完成下列相关问题!
- ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
- ¥15 求daily translation(DT)偏差订正方法的代码
- ¥15 js调用html页面需要隐藏某个按钮
- ¥15 ads仿真结果在圆图上是怎么读数的
- ¥20 Cotex M3的调试和程序执行方式是什么样的?
- ¥20 java项目连接sqlserver时报ssl相关错误
- ¥15 一道python难题3
- ¥15 牛顿斯科特系数表表示