| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 524 人关注过本帖
标题:为什么不能删除第一个接点?????
只看楼主 加入收藏
wenyong
Rank: 1
等 级:新手上路
帖 子:251
专家分:0
注 册:2005-8-9
收藏
 问题点数:0 回复次数:1 
为什么不能删除第一个接点?????

#define NULL 0
#include<stdio.h>
#include<stdlib.h>
struct Link{
int date;
struct Link *next;
};

struct Link *Creat_Link()
{
struct Link *head;
struct Link *p1,*p2;
head=(struct Link *)malloc(sizeof(struct Link));
scanf("%d",&head->date);
head->next=NULL;
p1=head;
while(p1->date>0)
{
if(head==NULL)
head=p1;
else
{
p2->next=p1;
p2=p1;
p1=NULL;
p1=(struct Link *)malloc(sizeof(struct Link));
scanf("%d",&p1->date);

}
}
p2->next=NULL;
return(head);
}

void Output_Link(struct Link *head)
{
struct Link *p;
p=head;
if(p==NULL)
printf("null LINK:\n");
else
while(p!=NULL)
{
printf("%d\t",p->date);
p=p->next;
}
printf("\n");

}


struct Link *Seek_Link(struct Link *head)
{
struct Link *p;
int seek;
p=head;
printf("please input the what you want to seek:\n");
scanf("%d",&seek);
if(head==NULL)
{
printf("this link is NULL\n");
system("pause");
}
while(p!=NULL)
{
if(p->date==seek)
{
printf("%d\n",p->date);
break;
}
else
p=p->next;
if(p==NULL)
{
printf("not exist\n");
system("pause");
break;
}
}
return(head);
}

struct Link*Del_Link(struct Link*head)
{
struct Link *p1;
struct Link *p2;
int del_num;
p1=head;
printf("please input the date you want to del:\n");
scanf("%d",&del_num);
if(head==NULL)
{
printf("this link is NULL\n");
system("pause");
}
else
{
while(p1->date!=del_num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(p1->date==del_num)
{

if(head->date==del_num)
{
head=p1->next;
free(head);
}
else
{
p2->next=p1->next;
free(p1);
}
}
else
{
printf("not exist\n");
}
}
return(head);
}

struct Link *Insert_Link(struct Link *head)
{
struct Link *p1,*p2,*p0;
p0=(struct Link *)malloc(sizeof(struct Link ));
printf("please input the num you want to insert;\n");
scanf("%d",&p0->date);
if(head==NULL)
head=p0;
else
{
p1=head;
while(p1->date!=p0->date&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(p1->date==p0->date)
if(p1==head)
{
head=p0;
p0->next=p1;
}
else if(p1->date==p0->date&&p1->next!=NULL)
{
p1->next=p0;
p0->next=p2;
}
else
{
p1->next=p0;
p0->next=NULL;
}
else

printf("not find:\n");

}
return(head);
}


main()
{
struct Link *head;
int o;
head=Creat_Link();
while(1)
{
printf("choose the num");
scanf("%d",&o);
switch(o)
{
case 1: Insert_Link(head); break;
case 2: Del_Link(head); break;
case 3: Seek_Link(head); break;
case 4: Output_Link(head); break;
default: exit(1);
}
}

getch();
}


搜索更多相关主题的帖子: 删除 
2005-11-05 17:09
落花吹雪
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2005-11-20
收藏
得分:0 

没耐心看 楼主分析下你的思路 第一个创建链表的函数都错了 你前面三个语句应该包含再循环里啊 还有*p2你没赋值啊 好乱啊

2005-12-03 16:24
快速回复:为什么不能删除第一个接点?????
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.010859 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved