创建一个单链表通讯录

mac2024-04-21  9

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: Add a record: Add a student record from the input.Delete a record: Delete a student record according to the student id from the input.Output all records: Display all the records in the address list.Search by name: Input the student name and then output the whole information of the student.Save records: Save all the records in the address list to a certain file.Clear records: Delete all the records in the address list and then delete the file.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; }

下面是代码运行截图:

大家可以放心引用!

最新回复(0)