限于水平,代碼可能有些不足之處,希望有人能補充,謝謝!
代碼如下:
#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(" 請輸入要增加的個(gè)數:\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(" 電話(huà)號碼:");
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(" 電話(huà)號碼:");
scanf("%s",p -> tel);
p -> next = NULL;
return p;
}
void print(struct student * h) /*通信錄的輸出*/
{
struct student * p;
p = h;
if(h != NULL)
{
printf("| 姓名 電話(huà)號碼 |\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("已默認添加到最后一個(gè)了!\n");
}
}
else
{
p -> next = in;
in -> next = NULL;
printf("已默認添加到最后一個(gè)了!\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 的電話(huà)號碼是: %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("輸入電話(huà)號碼:");
scanf("%s",&p -> tel);
printf("更改成功!\n");
}
else printf("在你的通信錄中找不到 %s 的信息!\n",cname);
}
return h;
}
void select(struct student * head) /*程序提示之對通信錄進(jìn)行什么樣的操作*/
{
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("輸入你要增加的聯(lián)系人的姓名和電話(huà)號碼\n");
inname = add();
printf("輸入你要添加到通信錄的第幾個(gè)位置!\n");
scanf("%d",&No);
insert(head,inname,No); /*調用單鏈表的插入*/
select1(head);
}
else if(N == 3)
{
printf("輸入你要刪除的聯(lián)系人的姓名!\n");
scanf("%s",delname);
head = del(head,delname); /*調用單鏈表的刪除*/
select1(head);
}
else if(N == 4)
{
select1(head); /*調用單鏈表的輸出*/
}
else if(N == 5)
{
printf("輸入你要查找的聯(lián)系人的姓名!\n");
scanf("%s",fname);
find(head,fname);
select1(head);
}
else if(N == 6)
{
printf("輸入你要修改的聯(lián)系人的姓名!\n");
scanf("%s",cname);
change(head,cname);
select1(head);
}
else if(N == 0)
select(head);
else
{
printf(" 輸入錯誤!請重新選擇你將要進(jìn)行的操作!\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) /*程序提示之是否對通信錄進(jìn)行操作*/
{
int m;
interf(h);
scanf("%d",&m);
if(m == 0)
select(h);
else ;
}
void main()
{
struct student * H;
H = NULL;
select1(H);
}
結束