#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN sizeof(struct student)
struct student
{
int num;
char name[20];
char tel[11];
struct student *next;
};
typedef struct student node;
typedef struct student *Link;
void display(Link head)
{
Link p=head->next;
if(p==NULL)
{
printf("the link is empty!\n");
}
while(p!=NULL)
{
printf("%d\t%s\t%s\n",p->num,p->name,p->tel);
p=p->next;
}
}//打印信息
void release_node(Link *head)
{
Link p=(*head)->next;
while((*head)->next!=NULL)
{
(*head)->next=p->next;
free(p);
p=(*head)->next;
}
}//释放出头结点外的所有结点
void menu()
{
printf("\n\twelcome contact information!\n");
printf("\t1 add information\n");
printf("\t2 delete information\n");
printf("\t3 change information\n");
printf("\t4 serch information\n");
printf("\t5 show information\n");
printf("\t6 release information\n");
printf("\t7 exit\n");
}//主菜单
void is_malloc_ok(Link new_node)
{
Link p;
p=new_node;
if(p==NULL)
{
printf("malloc error!\n");
exit(-1);
}
}
void create_node(Link *new_node)
{
*new_node=(Link)malloc(LEN);
is_malloc_ok(*new_node);
printf("输入成员学号:\n");
scanf("%d",&(*new_node)->num);
printf("输入成员姓名:\n");
scanf("%s",&(*new_node)->name);
printf("输入成员电话:\n");
scanf("%s",&(*new_node)->tel);
printf("添加新成员成功\n");
}
void create_link(Link *head)
{
*head=(Link)malloc(LEN);
(*head)->next=NULL;
}
void insert_node_tail(Link head,Link new_node)
{
Link p=head;
while(p->next!=NULL)
{
p=p->next;
}
p->next=new_node;
new_node->next=NULL;
}
void delete_node(Link head,char dname[20])
{
Link p,q;
p=q=head;
while(p->next!=NULL && strcmp(p->next->name,dname)!=0)
{
p=p->next;
}
if(p->next==NULL)
{
printf("没有此联系人!\n");
return ;
}
else
{
q=p->next;
p->next=p->next->next;
free(q);
}
}
void find_node(Link head,char name[20])
{
Link p;
p=head;
while(p->next!=NULL && strcmp(p->name,name)!=0)
{
p=p->next;
}
if(strcmp (p->name,name)==0)
{
printf("%d\t%s\t%s\n",p->num,p->name,p->tel);
}
}
void change_node(Link head,char cname[20])
{
int i;
Link p;
p=head;
while(p->next!=NULL&&strcmp(p->name,cname)!=0)
{
p=p->next;
}
if(strcmp(p->name,cname)==0)
{
printf("enter change choice:\n");
scanf("%d",&i);
switch(i)
{
case 0:
{
printf("change num:\n");
scanf("%d",&p->num);
break;
}
case 1:
{
printf("change name:\n");
scanf("%s",p->name);
break;
}
case 2:
{
printf("change tel:\n");
scanf("%s",p->tel);
break;
}
default:
{
printf("no information to change!\n");
break;
}
}
printf("%d\t%s\t%s\n",p->num,p->name,p->tel);
}
}
int main()
{
Link head,new_node;
create_link(&head);
printf("welcome my phone! \n");
while(1)
{
int choice;
menu();
scanf("%d",&choice);
printf("choice = %d\n", choice);
switch(choice)
{
case 1:
{
create_node(&new_node);
insert_node_tail(head, new_node);
// display(head);
break;
}
case 2:
{
char dname[20] = {0};
printf("delete name:\n");
scanf("%s",dname);
delete_node(head,dname);
// display(head);
break;
}
case 3:
{
char cname[20]={0};
printf("change name:\n");
scanf("%s",cname);
change_node(head,cname);
break;
}
case 4:
{
char fname[20] = {0};
printf("find name:\n");
scanf("%s",fname);
find_node(head,fname);
break;
}
case 5:
{
display (head);
break;
}
case 6:
{
release_node(&head);
display(head);
break;
}
case 7:
{
exit(-1);
break;
}
default:
{
printf("wrong choice!\n");
break;