限于水平,代码可能有些不足之处,希望有人能补充,谢谢!
代码如下:
#include "stdio.h"
#include "malloc.h" #define LEN sizeof(struct student) #define L 100 struct student
{ char name[L]; char tel[L]; struct student * next; }; int n , m, i;
void operation()
{ printf(" ***通信录操作界面***\n\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"); printf("|选项(按 0)----------(其他)退出|\n\n"); } struct student * creat(void) /*通信录的建立*/
{ struct student * h; struct student * p,*q; printf(" 请输入要增加的个数:\n"); scanf("%d",&m); h = NULL; if(m > 0) { q = p = (struct student *) malloc(LEN); for(i = 0;i < m;i++) { p = (struct student *) malloc(LEN); printf(" 姓 名:"); scanf("%s",p -> name); printf(" 电话号码:"); scanf("%s",p -> tel); q -> next = p; if(i == 0) h = p; q = p; } q -> next = NULL; } else { printf(" 输入有误!\n"); creat(); } return h; } struct student * add(void) /*添加信息*/
{ struct student * p; p = NULL; p = (struct student *) malloc(LEN); printf(" 姓名:"); scanf("%s",p -> name); printf(" 电话号码:"); scanf("%s",p -> tel); p -> next = NULL; return p; } void print(struct student * h) /*通信录的输出*/
{ struct student * p; p = h; if(h != NULL) { printf("| 姓名 电话号码 |\n"); while(p != NULL) { printf("| %-10s%-19s|\n",p -> name,p -> tel); p = p -> next; } } else printf("| |\n"); }
struct student *insert(struct student * h,struct student * inname,int n)/*信息的插入*/
{ struct student * p,* q,* in;int i = 1; in = inname; p = h; if(h == NULL) { h = in; in ->next = NULL; } else { if(n == 1) { h = in; in -> next = p; printf("添加成功!\n"); } else if(n > 1) { while(i != n || p -> next != NULL ) { q = p;p = p -> next;i ++; } if(i == n) { q -> next = in; in -> next = p; printf("添加成功!\n"); } if(i < n) { p -> next = in; in -> next = NULL; printf("你的通信录只有 %d 信息!\n",i); printf("已默认添加到最后一个了!\n"); } } else { p -> next = in; in -> next = NULL; printf("已默认添加到最后一个了!\n"); } } return h; } struct student * del(struct student *h,char delname[])/*信息的删除*/ { struct student * p,*q; if(h ==NULL) printf("通信录为空!\n"); p = h; while(*delname != *p -> name && p -> next != NULL) { q = p;p = p -> next; } if(*delname == *p -> name) { if(p == h) h = p -> next; else q -> next = p -> next; printf("删除成功!\n\n"); n = n - 1; } else printf("%s 在你的通信录中找不到!\n\n",delname); return h; } void find(struct student * h,char fname[])/*信息的查找*/
{ struct student * p; if(h ==NULL) printf("通信录为空!\n"); else { p = h; while(*fname != *p -> name && p -> next != NULL) p = p -> next; if(*p -> name == *fname) printf(" %s 的电话号码是: %s\n",p -> name,p -> tel); else printf("在你的通信录中找不到 %s 的信息!\n",fname); } } struct student * change(struct student *h,char cname[])/*信息的更改*/ { struct student * p; if(h ==NULL) printf("通信录为空!\n"); else { p = h; while(*cname != *p -> name && p -> next != NULL) p = p -> next; if(*p -> name == *cname) { printf("输入电话号码:"); scanf("%s",&p -> tel); printf("更改成功!\n"); } else printf("在你的通信录中找不到 %s 的信息!\n",cname); } return h; } void select(struct student * head) /*程序提示之对通信录进行什么样的操作*/
{ void interf(); void select1(); struct student *inname; char delname[L],fname[L],cname[L]; int N,No; operation(); scanf("%d",&N); if(head != NULL || N == 0 || N == 1 || N ==2) { if(N == 1) { head = creat(); select1(head); } else if (N == 2) { inname = (struct student *) malloc(LEN); printf("输入你要增加的联系人的姓名和电话号码\n"); inname = add(); printf("输入你要添加到通信录的第几个位置!\n"); scanf("%d",&No); insert(head,inname,No); /*调用单链表的插入*/ select1(head); } else if(N == 3) { printf("输入你要删除的联系人的姓名!\n"); scanf("%s",delname); head = del(head,delname); /*调用单链表的删除*/ select1(head); } else if(N == 4) { select1(head); /*调用单链表的输出*/ } else if(N == 5) { printf("输入你要查找的联系人的姓名!\n"); scanf("%s",fname); find(head,fname); select1(head); } else if(N == 6) { printf("输入你要修改的联系人的姓名!\n"); scanf("%s",cname); change(head,cname); select1(head); } else if(N == 0) select(head); else { printf(" 输入错误!请重新选择你将要进行的操作!\n"); select(head); } } else select1(head); } void interf(struct student * HH)
{ printf(" ***通信录主界面***\n\n"); printf("| **********通信录********** |\n"); printf("| ========================== |\n"); printf("| |\n"); print(HH); printf("| =========================== |\n"); printf("|选项(按 0)----------(其他)退出|\n\n"); } void select1(struct student *h) /*程序提示之是否对通信录进行操作*/ { int m; interf(h); scanf("%d",&m); if(m == 0) select(h); else ; } void main()
{ struct student * H; H = NULL; select1(H); } 结束
|
|
来自: yuqiong_ma > 《科技》