通讯录问题

RobertXufei 2009-11-04 07:26:18
《数据结构》实验题目
实验一、通讯录管理(链表)
 实验目的:熟练掌握单链表操作的基本算法实现。
 实现功能:用带头结点的单链表作存储结构,实现通讯录单链表的建立、通讯者的插入、删除、查询以及通讯录链表的输出功能。
 实验机时:6
 设计要求:
为了实现通讯录管理的操作功能,首先设计一个含有多个菜单项的主控菜单程序,然后再为这些菜单项配上相应的功能。
主控菜单设计要求:
• 菜单内容
程序运行后,给出6个菜单项的内容和输入提示:
1.通讯录链表的建立
2.通讯者结点的插入
3.通讯者结点的查询
4.通讯者结点的删除
5.通讯录链表的输出
0.退出管理系统
请选择0—5:
• 菜单设计要求:使用数字0—5来选择菜单项,其它输入则不起作用。

# include<stdio.h>
# include<malloc.h>
# include<string.h>
# include<stdlib.h>
# define NULL 0
# define LEN sizeof(struct people)
struct people
{
long num;
char name;
char tel;
char add;
struct people *next;
};

int n;
struct people *creat(void)
{struct people *head;
struct people *p1,*p2;
n=0;
p1=p2=(struct people*)malloc(LEN);
printf("information:\n");
scanf("%ld,%s,%s,%s",&p1->num,p1->name,p1->tel,p1->add);printf("\n");
head=NULL;
while(p1->num)
{n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct people*)malloc(LEN);
scanf("%ld%s%s%s",&p1->num,p1->name,p1->tel,p1->add);
}
p2->next=NULL;
return(head);
}

void print(struct people *head)
{struct people *p;
printf("\n Information is:\n");
p=head;
if(head)
while(p)
{printf("%ld %s %s %s",p->num,p->name,p->tel,p->add);
p=p->next;
}
}

struct people *delet(struct people *head,long num)
{struct people *p1,*p2;
if(head==NULL) {printf("\n 列表为空!\n");}
p1=head;
while(num!=p1->num&&p1==NULL)
{p2=p1;p1=p1->next;}
if(num==p1->num)
{if(p1==head)head=p1->next;
else p2->next=p1->next;
printf("删除:%ld\n",num);
n=n-1;
}
else printf("%ld 还没有输入!\n",num);

return(head);
}
struct people *insert(struct people *head,struct people *stud)
{struct people *p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL)
{head=p0;p0->next=NULL;}
else
{while((p0->num>p1->num)&&(p1->next!=NULL))
{p2=p1;
p1=p1->next;}
if(p0->num<=p1->num)
{if(head==p1)head=p0;
else p2->next=p0;
p0->next=p1;}
else
{p1->next=p0;p0->next=NULL;}
}
n=n+1;
return(head);
}

struct people * search(struct people *h,struct people *x)
{
struct people *p;
long y;
p=h->next;
while(p!=NULL)
{
y=p->num;
if(y==x)
return(p);
else p=p->next;
}
if(p==NULL)
printf("data not find!");
return 0;
}

void quit()
{
printf("\n Thank you for your using!\n Press any key to quit...");
exit(0);
}

void menu()
{printf("****************************************************\n");
printf(" 欢迎使用通讯录! \n");
printf(" 请输入您想实现的功能:(键入其序号) \n");
printf(" 1.建立通讯录 \n");
printf(" 2.添加联系人 \n");
printf(" 3.查找联系人 \n");
printf(" 4.删除联系人 \n");
printf(" 5.输出所有联系人 \n");
printf(" 6.退出 \n");
printf("****************************************************\n");
}


void main()
{struct people*head,stu;
long del_num;
int c;
menu();
scanf("%d",c);
switch(c){
case 1:
printf("请依次输入:学号 : 姓名: 电话 : 住址 : (用逗号隔开)\n ");
head=creat();
break;
case 2:
printf("\ninput the inserted number:");
scanf("%ld %s %s %s",&stu.num,stu.name,stu.tel,stu.add);
head=insert(head,&stu);
break;
case 3:
printf("\n请输入想要查找的姓名:\n");

case 4:
printf("\ninput the deleted number:");
scanf("%ld",&del_num);
head=delet(head,del_num);
break;
case 5:
print(head);
break;
case 6:
quit();
break;

}
}
...全文
142 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
RobertXufei 2009-11-06
  • 打赏
  • 举报
回复
还是不好使耶。没有错误,但是程序功能不行
麻烦你帮帮改改

阿里嘎多
kouwenlong 2009-11-04
  • 打赏
  • 举报
回复
结构写的有问题:
struct people
{
long num;
char name[20];
char tel[20];
char add[20];
struct people *next;
};
还有个别的错误,比如:
struct people * search(struct people *h,struct people *x)
{
struct people *p;
long y;
p=h->next;
while(p!=NULL)
{
y=p->num;
if(y==x->num)//这里我给你改过来了
return(p);
else p=p->next;
}
if(p==NULL)
printf("data not find!");
return 0;
}
RobertXufei 2009-11-04
  • 打赏
  • 举报
回复
呵呵!由于刚加入,分数少。
kouwenlong 2009-11-04
  • 打赏
  • 举报
回复
1分我也要。

69,374

社区成员

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

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