数据结构用顺序表实现的电话通讯录(C语言)#includestdio.h#includemalloc.h#includemath.h#includestring.h#defineFALSE0#defineERROR0#defineOK1#defineINFEASIBLE-1#defineLIST_INIT_SIZE10#defineLIST_INCREMENT5#defineN5typedefintStatus;typedefstruct{charname[10];//姓名charnum[15];//号码}student;typedefstruct{student*elem;//存储空间基址intlength;//当前长度intlistsize;//当前分配的存储空间(以sizeof(student)为单位)}Sqlist;SqlistL;//定义全局变量L为Sqllist类型StatusListInsert(Sqlist&L,inti,studente){//插入学生信息到顺序表L中intj;student*newbase;if(i1||iL.length+1)returnERROR;//i值不合法if(L.length=L.listsize)//当前存储空间已满,增加分配{newbase=(student*)realloc(L.elem,(LIST_INIT_SIZE+LIST_INCREMENT)*(sizeof(student)));if(!newbase)//存储分配失败exit(OVERFLOW);L.elem=newbase;//新基址L.listsize+=LIST_INCREMENT;//增加存储容量}for(j=L.length;j=i;j--)L.elem[j]=L.elem[j-1];//插入位置及之后元素的右移L.elem[i-1]=e;L.length++;returnOK;}voidInitList(Sqlist&L){//创建顺序表L.elem=(student*)malloc(LIST_INIT_SIZE*sizeof(student));if(!L.elem)exit(OVERFLOW);//存储分配失败L.length=0;//空表长度L.listsize=LIST_INIT_SIZE;//初始存储容量}voidListTraverse(SqlistL,void(*vi)(student&)){//显示顺序表中的所有记录system(cls);printf(姓名电话号码\n);student*p;inti;p=L.elem;for(i=1;iL.length+1;i++)vi(*p++);printf(输入0:返回菜单请输入您的选择:);}voidprint(student&a){//信息输出printf(%-10s%-8s,a.name,a.num);printf(\n);}intListLength(SqlistL)//返回顺序表的长度{returnL.length;}intLocateElem(SqlistL,studente,Status(*compare)(student,student)){//返回L中第一个与e满足关系compare的数据的位序,//若这样的数据不存在,则返回为0;inti=1;//i的初值为第1个元素的位序student*p=L.elem;//p的初值为第1个元素的存储位置while(i=L.length&&!compare(*p++,e))i++;if(i=L.length)returni;elsereturnERROR;}StatusListDelete(Sqlist&L,inti,student&e){//在顺序表L中删除第i个元素,并返回OKsystem(cls);//清除屏幕intj;if(i1||iL.length)//i值不合法returnERROR;else{e=L.elem[i-1];//p为删除元素的位置for(j=i;j=L.length;j++)L.elem[j-1]=L.elem[j];L.length--;returnOK;}}voidwrong(){//错误提示信息输出printf(youhaveinputedawrongnumber);printf(pleaseinputthenumberthatisbetween0and8);}voidDelete(Sqlist&L){//根据i删除顺序表中的记录studente;intj;printf(请输入要删除的位置(1≦i≦%d):,L.length+1);scanf(%d,&j);ListDelete(L,j,e);printf(已删除!);printf(输入0:返回菜单请输入您的选择:);}voidScanIn(Sqlist&L){//信息输入system(cls);inti;studente;chara[2];printf(请输入信息:\n);do{student*p=L.elem;printf(请输入姓名:);scanf(%s,e.name);printf(请输入电话号码:);scanf(%s,e.num);printf(请输入你要插入的位置(1≦i≦%d):,L.length+1);scanf(%d,&i);ListInsert(L,i,e);jump:printf(是否继续输入信息(y/n));scanf(%s,a);}while(strcmp(a,y)==0||strcmp(a,Y)==0);printf(输入0:返回菜单请输入您的选择:);}studentstu[N]={{小易,13711111111},{小二,13422222222},{小伞,18733333333},{小斯,15844444444},{小武,13455555555}};intstuIntsertList(Sqlist&L){//将结构体数组stu中的记录插入到顺序表inti,j;for(i=0;iN;i++)j=ListInsert(L,i+1,stu[i]);system(cls);if(j)printf(已成功加入到顺序表中\n);printf(输入0:返回菜单请输入您的选择:);returnOK;}voidmenu(){//菜单函数printf(\t\t*************************************\n);printf(\t\t*1.导入记录*\n);printf(\t\t*2.输入记录*\n);printf(\t\t*3.删除记录*\n);printf(\t\t*4.显示所有记录*\n);printf(\t\t*0.返回本菜单*\n);printf(\t\t*************************************\n\n\n);}//开始函数voidstart(){printf(\n);printf(\t\t\t**************************\n);printf(\t\t\t*欢迎使用*\n);printf(\t\t\t*电话查询系统*\n);printf(\t\t\t**************************\n);printf(\t\t\t**\n);printf(\t\t\t**************************\n);printf(\n);menu();}intmain(){inti=0,j;studente;Sqlistp,q;InitList(L);start();while(scanf(%d,&i)){switch(i){case1:stuIntsertList(L);break;case2:ScanIn(L);break;case3:Delete(L);break;case4:ListTraverse(L,print);break;case0:menu();break;default:wrong();}}return0;}