创建一个单链表通讯录

  1. address list design

Design a classmate's address list, requested as follows:

  • Each student in the address list contains the following information: student id、name、telephone number. If you need more fields, please add them yourself.
  • The program has a main menu containing the following functions:
  1. Add a record: Add a student record from the input.
  2. Delete a record: Delete a student record according to the student id from the input.
  3. Output all records: Display all the records in the address list.
  4. Search by name: Input the student name and then output the whole information of the student.
  5. Save records: Save all the records in the address list to a certain file.
  6. Clear records: Delete all the records in the address list and then delete the file.
  7. Quit

hint

  • When the program starts, it should be determined whether there is a record file. If the file exists, read each record from it to the list.
  • After the user selects and completes a function of the main menu, the program should return to the main menu.
  • When a record is added, it should be inserted into the tail of the list.
  • If a record does not exist when performing delete or and search operation, the program should output some information to the user.
  • You do not need to write files when adding records or deleting records.
  • When you want to save a record you’d better overwrite the file. (Or delete the original file first, and then save all the records)
  • Each module is written in the form of a function, called by the main function.

optional

  • Add a sorting function in the main menu, the sorting result should be in an ascending order according to the student number. Sorting methods can be done by bubble sort or insert sort.

下面的代码在Dev-C++ 上编译通过!

#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<stdlib.h>
#include<windows.h> 
typedef struct Stu
{
long long id;//学生ID 
char name[10];//学生姓名 
long long tel;//学生电话号码 
struct Stu*next;//next链表指针 
}stud;//使用long long长整型是因为int和long的数据类型的长度不足以支持到11位 
FILE *fp; //文件指针,便于接下来对文件进行操作 
void CreLi(stud *&L)//以函数的形式创建链表,方便在主函数中直接调用 
{
L=(stud*)malloc(sizeof(stud));//分配空间 
L->next=NULL;//将头指为空 
}
void Loading(stud * &L,long long ID,char *Name,long long Tel)//将新建的数据以节点形式传入 
{//使用&是为了将载入数据的链表带回主函数 
stud *r,*temp=L;//将t指为L,并用while函数指向其末尾; 
while(temp->next!=NULL)
{
temp=temp->next;
}
r=(stud *)malloc(sizeof(stud));//为临时数据的临时节点分配空间 
r->id=ID;
strcpy(r->name,Name);
r->tel=Tel;
//以上三步将传入的值赋给临时节点 
r->next=NULL;//确保末尾指向空 
temp->next=r;//将临时变量的值赋给t链 
r->next=NULL;
}
bool DeLi(stud * &L ,long long ID) //删除记录
{
stud *pre=L;
while(pre->next!=NULL && pre->next->id!=ID)//执行查找 
{
pre=pre->next;
}
if(pre==NULL){//未找到输出提示 
printf("Record does not exist\n");
return false;
}
else//找到则执行删除操作 
{
stud*q=(stud*)malloc(sizeof(stud));
q=pre->next;//删除值赋给q 
pre->next=pre->next->next;//踢掉待删值 
free(q);//删除完成 
printf("successfully delete\n");
return true;
}
}
void Show(stud *L ) //输出联系人 
{
if(L !=NULL)
L =L ->next;
if(L!=NULL)
{
while(L!=NULL)//疯狂循环,疯狂输出 
{ 
printf("ID\t\tName\tTel\n");
printf("%lld\t",L->id);
printf("%s\t",L->name);
printf("%lld\n\n",L->tel);
L=L->next;
}
if(L==NULL)
printf("Address book printed!\n");
}
else
{
printf("Address book is empty! Please add contacts first!\n");
}
}
bool NOS(stud *&L,char *Name) //按名字查找
{
stud *p;
p=L->next;
while(p!=NULL&&(strcmp(p->name,Name)))//名字对应查找 
{//strcmp用来判断字符串是否相等,即对链表的遍历 
p=p->next;
}
if(p==NULL)
{
printf("The contact was not found in the address book!\n");
return false;
}
else
{
printf("ID\t\tName\tTel\n");
printf("%lld\t",p->id);
printf("%s\t",p->name);
printf("%lld\n\n",p->tel);
return true;
}
}
void Saving(stud * L) //将内存中的联系人文件存入本地磁盘 
{
fp=fopen("data.txt","w"); //打开文件 
//w是指:打开一个文本文件,清除原内容,写入文件。如果文件不存在,则会创建一个新文件。 
if(L!=NULL)
L=L->next;
while(L!=NULL)
{
fprintf(fp,"%s\t%lld\t%lld\n",L->name,L->id,L->tel);//写入文件,按行写入 
//分别以长整型,字符串,长整型写入txt文件中。 
L=L->next;//每写入一个联系人数据,就将链表指向下一节点。 
}
fclose(fp);//正确的关闭文件,确保真的写入数据 
}
void Empty(stud * &L) //清空记录
{
stud *p=L,*q=L;
p=p->next; 
if(p!=NULL)
q=p->next;//一前一后循环删除 
while(q!=NULL)
{
free(p);//清除临时节点 
p=q;//重给链值 
q=p->next;//指向下一待删节点 
}
free(p);//清除最后一次赋值 
L->next=NULL;//s最终指向空 
printf("Address Book clearance completed\n");
}
void Start(stud *L) // 初始化和载入文件中数据
{
system("color 07"); //屏幕、 字体颜色
char Name[10];
long long ID,Tel;
//a是以追加模式打开文件,不会清除原有数据,若没有,则新建 
fp=fopen("data.txt","a"); //就是为了确保文件没有时可以再建一个 
fclose(fp);
fp=fopen("data.txt","r");//读取原有文件 
while(fscanf(fp,"%s%lld%lld",Name,&ID,&Tel)!=EOF) //从文件中读入数据
Loading(L,ID,Name,Tel);//不停地将文件中的数据写入链表并传出。 
fclose(fp);//确保关闭 
printf("File data read status: read successfully!\n");
}
int main()
{
int n;
long long ID,Tel;
char Name[10];
stud *L;
CreLi(L);
Start(L);
printf("Please select the sequence number you want to operate:\n");
printf("=====  1. Add Records \t2. Delete records   \n");
printf("=====  3. Show record \t4. Search by name   \n");
printf("=====  5. Saving records6. Clear records \n");
printf("=====  7. Sign out\t\t\t\t \n");
while(~scanf("%d",&n))
{
if(n>=1&&n<=6){
switch(n)
{
case 1:
{
printf("Enter student id:\n");
scanf("%lld",&ID);
printf("Enter the name:\n");
scanf("%s",&Name);
printf("Enter phone number:\n");
scanf("%lld",&Tel); 
Loading(L,ID,Name,Tel);break;}
case 2:
{printf("Enter student id:\n");
scanf("%lld",&ID);
DeLi(L,ID);
break;}
case 3:
{Show(L);break;}
case 4:
{printf("Enter the name:\n");
scanf("%s",&Name);
NOS(L,Name);break;}
case 5:
{Saving(L);printf("Data saved successfully\n");break;}
case 6:
{Empty(L);break;}
}
}
if(n==7)
{
printf("It's coming to an end. Just a moment, please.\n");
Sleep(3000); 
break;}
printf("=====  1. Add Records \t2. Delete records   \n");
printf("=====  3. Show record \t4. Search by name   \n");
printf("=====  5. Saving records6. Clear records \n");
printf("=====  7. Sign out\t\t\t\t \n");
}
return 0;
}

下面是代码运行截图:

大家可以放心引用!

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
第一个模块——主函数main()的功能是:根据选单的选项调用各函数,并完成相应的功能。 
   第二个模块——Menu()的功能是:显示提示选单。 
   第三个模块——Quit()的功能是:退出选单。 
   第四个模块——Create()的功能是:创建新的数据记录。 
   第五个模块——Add()的功能是:增加新的数据记录,并返回选单。 
   第六个模块——Find()的功能是:按要求查询相关的信息,如果找到了,则显示该信息,如果未找到,则提示文件中没有该信息,并返回选单。 
   第七个模块——Alter()[的功能是:修改某条记录的信息,如果未找到要修改的记录,则提示系统中无此记录,并返回选单。 
   第八个模块——Delete()的功能是:删除某条记录,如果未找到要删除的记录,则提示通讯录中没有,并返回选单。 
   第九个模块——List()的功能是:显示所有记录。 一、用链表或者顺序表实现以下系统,完成线性表的建立(至少包括10个结点),以及线性表中信息(结点)的插入、查找、删除、修改、输出等操作,具体的模块要求见上方的“总的模块要求”。建议用“文件”存储数据。 1.通讯录管理系统的设计与实现 (1)通讯者信息包括:编号(char num[10])、姓名(char name[10])、性别(char sex[10])、电话(char phone[20]) (2)除了总的模块要求外,还需统计通讯录中男性人数及女性人数,并求出通讯录中的第一个模块——主函数main()的功能是:根据选单的选项调用各函数,并完成相应的功能。 
   第二个模块——Menu()的功能是:显示提示选单。 
   第三个模块——Quit()的功能是:退出选单。 
   第四个模块——Create()的功能是:创建新的数据记录。 
   第五个模块——Add()的功能是:增加新的数据记录,并返回选单。 
   第六个模块——Find()的功能是:按要求查询相关的信息,如果找到了,则显示该信息,如果未找到,则提示文件中没有该信息,并返回选单。 
   第七个模块——Alter()[的功能是:修改某条记录的信息,如果未找到要修改的记录,则提示系统中无此记录,并返回选单。 
   第八个模块——Delete()的功能是:删除某条记录,如果未找到要删除的记录,则提示通讯录中没有,并返回选单。 
   第九个模块——List()的功能是:显示所有记录。 一、用链表或者顺序表实现以下系统,完成线性表的建立(至少包括10个结点),以及线性表中信息(结点)的插入、查找、删除、修改、输出等操作,具体的模块要求见上方的“总的模块要求”。建议用“文件”存储数据。 1.通讯录管理系统的设计与实现 (1)通讯者信息包括:编号(char num[10])、姓名(char name[10])、性别(char sex[10])、电话(char phone[20]) (2)除了总的模块要求外,还需统计通讯录中男性人数及女性人数,并求出通讯录中的男女比例。 男女比例。
通讯录管理系统 通讯录(add_book)中的联系人包含以下信息项:姓名、手机、办公电话、家庭电话、电子邮箱、所在省市、工作单位、家庭住址,群组分类(亲属、同事、同学、朋友、其他)。 Name Mobile phones Office phone Family telephone E-mail In cities Work units0 Address Group classification (relative, colleagues, friends, classmates, other). 系统的主要功能包括: 1. 输入联系人的信息,要求:至少输入10个联系人的数据,且注意数据的多样性。 2. 按姓名对联系人信息进行排序,并将排序后信息存放到一个文本文件中。 3. 添加联系人的信息,在已经存在的通讯录文件中添加若干个联系人。要求:添加后仍按联系人的姓名排序,并保存至原文件。 4. 删除联系人的信息,输入一个姓名,若通讯录中有该联系人的信息,则删除该联系人,否则输出提示信息,并提示用户选择是否继续进行删除操作。 5. 修改联系人的信息,输入一个姓名,根据具体需要修改该联系人的某一项信息,将修改后的信息重新保存到通讯录文件中,并提示用户选择是否继续进行修改操作。 6. 按不同条件对通讯录进行查询操作,输出满足条件的联系人的信息。 (1) 按姓名查询,包括精确查询(输入全名),模糊查询(输入姓); (2) 按手机号码查询,输入全部号码或号码位段(如输入130、133、139等); (3) 按群组分类查询,输入分类名称,输出该群组的全部联系人信息。 7. 输出联系人的信息Contact information,按一定格式输出信息,保证信息排列整齐美观。ContactPerson
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值