贵州师范大学
Guizhou Normal University
《算法与数据结构》
实验报告
学院:大数据与计算机科学学院
专业:数据科学与大数据技术
班级:20级数据科学与大数据技术1班
指导老师:杨阿祧
学号:201177010054
姓名:王治文
实验课程名称: 数据结构与算法综合实验
实验项目名称 |
基于顺序表的手机通讯录 |
报告成绩 |
|||
实验者 |
王治文 |
专业班级 |
大数据1班 |
组别 |
|
同组者 |
完成日期 |
2021\9\30 |
|||
第一部分:实验分析与设计
1.目的: (1)掌握顺序表的定位、插入、删除等基本操作。 2.要求: 增加记录、删除记录、显示所有记录、查询记录、文件备份、退出。通讯录记录信息包括:姓名,电话,email等。 (1)通讯录的每一条信息包括姓名,单位,电话!(可用数组或数据库); (2)输入功能:可以一次完成若干条信息的输入; (3)显示功能:完成全部通讯录信息的显示(一屏最多显示10条,超过十条应能够自动分屏显示); (4)查找功能:完成按姓名查找通讯信息; (5)删除功能:完成按姓名删除通讯信息; (6)应提供一个界面来调用各个功能,调用界面和各个功能的操作界面应尽可能清晰美观!
定义储存联系人信息的结构体。 界面:通过switch函数个用户展示使用各个功能的选项,让用户自己选择功能。 输入、显示:用while循环语句,用户依次输入联系人信息和联系人信息的显示。 删除、插入:输入名字匹配对应项删除或插入。
插入元素需将链表从插入位置整个向后移,而删除则需要从删除位置后面的节点整个向前移将删除点覆盖;用户界面用switch函数实现用户选择功能。 三、主要仪器设备及耗材 Codeblocks、windows10、WPS Office、Chrome 第二部分:实验过程和结果
实现代码: #include "stdlib.h" #define NEW (struct node *)malloc(sizeof(struct node)) struct student { char name[10],tel[11]; }a[20]; struct node { char name[20],tel[11]; struct node *next; }; main() { struct student *jianli(),*delete(struct student *); struct student *charu(struct student *); void xianshi(struct student *); struct node *create(),*delnode(struct node*,char *); struct node *insert(struct node *,struct node *,char *); void prlist(struct node *); struct student *p; struct node *head=NULL,*stu; char s[80],name[20],q[80]; int c,w; a:; system("cls"); printf("\nEnter your choice\n"); printf(" BLOCK\n CHAIN TABLE\n Quit\n"); gets(q); w=atoi(q); switch(w) { case 1: do { do { printf("----------------------------------------------\n"); printf("******************Phone book******************\n"); printf("----------------------------------------------\n"); printf(" | | <1> Add a note | |\n"); printf(" | | <2> Show the list | |\n"); printf(" | | <3> Delete a note | |\n"); printf(" | | <4> Insert a note | |\n"); printf(" | | <0> Quit | |\n"); printf("----------------------------------------------\n"); printf(" Enter your choice(0-4):"); gets(s); c=atoi(s); }while(c<0||c>4); system("cls"); switch(c) { case 1: p=jianli();break; case 2: xianshi(p);break; case 3: printf("\nPlease input the name to deleted\n"); p=delete(p);break; case 4: printf("\nPlease input the new name\n"); p=charu(p);break; } }while(c);goto a; case 2: do { do {printf("----------------------------------------------\n"); printf("******************Phone book******************\n"); printf("----------------------------------------------\n"); printf(" | | <1> Add a note | |\n"); printf(" | | <2> Show the list | |\n"); printf(" | | <3> Delete a note | |\n"); printf(" | | <4> Insert a note | |\n"); printf(" | | <0> Quit | |\n"); printf("----------------------------------------------\n"); printf(" Enter your choice(0-4):"); gets(s); c=atoi(s); }while(c<0||c>4); system("cls"); switch(c) { case 1: head=create();break; case 2: prlist(head);break; case 3: printf("\nInput the name to deleted\n"); gets(name); head=delnode(head,name);break; case 4: stu=NEW; printf("\nInput the new node\n"); printf("name:"); gets(stu->name); printf("tel:"); gets(stu->tel); stu->next=NULL; printf("\nInsert position\n"); printf("name:"); gets(name); head=insert(head,stu,name); } }while(c);goto a; } } #include "string.h" struct student *jianli() { char c1[10],c2[11]; int i=0; printf("name:"); gets(c1); while(strlen(c1)!=0) { strcpy(a[i].name,c1); printf("tel:"); gets(c2); strcpy(a[i].tel,c2); i++; printf("name:"); gets(c1); } return a; } #include "string.h" struct student *delete(struct student *p) { char c1[10]; int i=0,j,l=0; while(strlen(p[i].name)!=0) i++; printf("name:"); gets(c1); for(j=0;j<=i+1;j++) if(strcmp(c1,p[j].name)==0) {p[j]=p[j+1]; l=j;} while(l<i+1) {p[l]=p[l+1]; l++;} return p; } #include "string.h" struct student *charu(struct student *p) { char c1[10],c2[11]; int i=0; while(strlen(p[i].name)!=0) i++; printf("name:"); gets(c1); strcpy(p[i].name,c1); printf("tel:"); gets(c2); strcpy(p[i].tel,c2); return p; } #include "string.h" void xianshi(struct student *p) { int i=0; printf("name\ttel\n\n"); while(strlen(p[i].name)!=0) { printf("%s\t%s\n",p[i].name,p[i].tel); i++;} } #include "stdlib.h" #include "string.h" #define NEW (struct node *)malloc(sizeof(struct node)) struct node *create() { struct node *h; struct node *p,*q; char name[20]; h=q=NULL; printf("name:"); gets(name); while(strlen(name)!=0) { p=NEW; if(p==NULL) { printf("Allocation failure\n"); exit(0); } strcpy(p->name,name); printf("tel:"); gets(p->tel); p->next=NULL; if(h==NULL) h=p; else q->next=p; q=p; printf("name:"); gets(name); } return h; } struct node *insert(struct node *head,struct node *p0,char *x) { struct node *p,*q; if(head==NULL) { head=p0; p0->next=NULL; } else { p=head; while(strcmp(x,p->name)!=0&&p->next!=NULL) { q=p;p=q->next;} if(strcmp(x,p->name)==0) { if(p==head) head=p0; else q->next=p0; p0->next=p; } else { p->next=p0; p0->next=NULL; } } return head; } void prlist(struct node *head) { struct node *p; p=head; printf("name\ttel\n\n"); while(p!=NULL) { printf("%s\t%s\n",p->name,p->tel); p=p->next; } } struct node *delnode(struct node *head,char *x) { struct node *p,*q; if(head==NULL) { printf("this is a empty list."); return head; } p=head; while(strcmp(x,p->name)!=0&&p->next!=NULL) { q=p;p=p->next;} if(strcmp(x,p->name)==0) { if(p==head) head=p->next; else q->next=p->next; free(p); } else printf("Not found."); return head; } 过程如下:
第一步:通过switch函数个用户展示使用各个功能的选项,让用户自己选择功能 第二步:用printf函数构造一个简单界面。 第三步:输入新联系人。 第四步:删除联系人 第五步:展示整个通讯录 如上图所示,结果过程无误。
能够完成实验目的所述动作 第三部分:实验小结、收获与体会: 有一说一,难的要老命,看书看视频,问同学,才把这个勉强做了,一个人的力量还是太薄弱。总的来说吧,也是对自己的一种历练,各方面能力都有所成长。 |