| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 509 人关注过本帖
标题:请教高手指点下
只看楼主 加入收藏
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;
p1=p2=(struct Link *)malloc(sizeof(struct Link));
scanf("%d",&p1->date);
head=NULL;
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(head==NULL)
printf("null LINK:\n");
else
while(p!=NULL)
{
printf("%d\t\n",p->date);
p=p->next;
}

}


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 *p;
struct Link *s;
int del_num;
p=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(p->date!=del_num&&p->next!=NULL)
{
s=p;
p=p->next;
}
if(p->date==del_num)
{
if(p==head)
{
head=head->next;
free(p);
}
else
{
s->next=p->next;
free(p);
}
}
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);
p1=head;
if(head==NULL)
printf("the Link is null;\n");
else
{
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;
}
}
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: break;
}
}

getch();
}
程序存在一个bug 就是插入后 在显示 出现死循环。 不知道为什么
请高手指点下!

搜索更多相关主题的帖子: Link struct head NULL 
2005-10-31 12:30
wenyong
Rank: 1
等 级:新手上路
帖 子:251
专家分:0
注 册:2005-8-9
收藏
得分:0 
没人指点指点吗?

2005-11-01 08:46
快速回复:请教高手指点下
数据加载中...
 
   



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

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