#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
struct addressbook
{
int num;
char name[20];
char tel[20];
char email[20];
char qq[20];
struct addressbook *next;
};
typedef struct addressbook Addressbook;
typedef struct addressbook *Link;
int Main_Menu();
void Add_Recort(Link *head,Link new_node);
void Delete_Recort(Link *head);
void Scan_Recort(Link *head);
void Search_Recort(Link *head);
void Amend_Recort(Link *head);
/********鍒涘缓閾捐〃澶?******/
void create_link(Link *head)
{
*head = NULL;
}
/********涓诲嚱鏁?*******/
int main()
{
int num;
Link head = NULL;
Link new_node = NULL;
create_link(&head);
printf("***********************娆㈣繋杩涘叆閫氳褰曠郴缁?**********************\n");
while(1)
{
num = Main_Menu();
switch(num)
{
case 0 : exit(0);
case 1 : Add_Recort(&head,new_node);break;
case 2 : Delete_Recort(&head);break;
case 3 : Scan_Recort(&head);break;
case 4 : Search_Recort(&head);break;
case 5 : Amend_Recort(&head);break;
default : printf("There is no such option!\n");
break;
}
}
}
/********涓昏彍鍗曞嚱鏁?******/
int Main_Menu()
{
int n;
printf(" *****************MENU**************** \n");
printf(" 1.娣诲姞鑱旂郴浜? \n");
printf(" 2.鍒犻櫎鑱旂郴浜? \n");
printf(" 3.鏌ョ湅鑱旂郴浜? \n");
printf(" 4.鎼滅储鑱旂郴浜? \n");
printf(" 5.淇敼鑱旂郴浜? \n");
printf(" 0.閫€鍑洪€氳褰? \n");
printf(" ************************************* \n");
printf(" Enter your option <0-5>:");
scanf("%d",&n);
return n;
}
/********娣诲姞鑱旂郴浜?*******/
void Add_Recort(Link *head,Link new_node)
{
Link tmp;
new_node = (Link)malloc(sizeof(Addressbook));
if(new_node == NULL)
{
printf("malloc error!\n");
exit(-1);
}
printf("**********************娣诲姞鑱旂郴浜?*********************\n");
tmp = *head;
if(*head == NULL)
{
printf(" 搴忓彿:");
scanf("%d",&new_node->num);
printf(" 鑱旂郴浜?");
scanf("%s",new_node->name);
printf(" 鐢佃瘽:",tmp->tel);
scanf("%s",new_node->tel);
printf(" 閭:",tmp->email);
scanf("%s",new_node->email);
printf(" QQ:",tmp->qq);
scanf("%s",new_node->qq);
write(fd,new_node,sizeof(new_node));
new_node->next = *head;
*head = new_node;
close(fd);
printf(" 娣诲姞鎴愬姛锛乗n");
}
else
{
while(tmp->next != NULL)
{
tmp = tmp->next;
}
printf(" 搴忓彿:");
scanf("%d",&new_node->num);
printf(" 鑱旂郴浜?");
scanf("%s",new_node->name);
printf(" 鐢佃瘽:",tmp->tel);
scanf("%s",new_node->tel);
printf(" 閭:",tmp->email);
scanf("%s",new_node->email);
printf(" QQ:",tmp->qq);
scanf("%s",new_node->qq);
tmp->next = new_node;
new_node->next = NULL;
printf(" 娣诲姞鎴愬姛锛乗n");
}
}
/********鍒犻櫎鑱旂郴浜?*******/
void Delete_Recort(Link *head)
{
Link tmp;
Link p;
int num;
tmp = p = *head;
printf("************************鍒犻櫎鑱旂郴浜?*******************\n");
printf("杈撳叆瑕佸垹闄よ仈绯讳汉鐨勫簭鍙?");
scanf("%d",&num);
if(*head == NULL)
{
printf("addressbook is empty!\n");
return;
}
while(tmp->num != num && tmp->next != NULL)
{
p = tmp;
tmp = tmp->next;
}
if(tmp->num == num)
{
if(tmp == *head)
{
*head = tmp->next;
free(tmp);
printf(" 鍒犻櫎鎴愬姛锛乗n");
}
else
{
p->next = tmp->next;
free(tmp);
printf(" 鍒犻櫎鎴愬姛锛乗n");
}
}
else
{
printf("no such recort!\n");
}
}
/*******鏌ョ湅鑱旂郴浜?*******/
void Scan_Recort(Link *head)
{
Link tmp=*head;
printf("**********************鏌ョ湅鑱旂郴浜?*********************\n");
if(*head == NULL)
{
printf("addressbook is empty!\n");
return;
}
while(tmp != NULL)
{
printf(" *******************\n");
printf(" 搴忓彿:%d\n",tmp->num);
printf(" 濮撳悕:%s\n",tmp->name);
printf(" 鐢佃瘽:%s\n",tmp->tel);
printf(" email:%s\n",tmp->email);
printf(" QQ:%s\n",tmp->qq);
printf(" *******************\n");
tmp = tmp->next;
}
}
/*******鎼滅储鑱旂郴浜?*******/
void Search_Recort(Link *head)
{
int num;
Link tmp;
printf("**********************鎼滅储鑱旂郴浜?*******************\n");
tmp = *head;
printf(" 杈撳叆瑕佹悳绱㈣仈绯讳汉鐨勫簭鍙?");
scanf("%d",&num);
while(tmp->num != num && tmp->next != NULL)
{
tmp = tmp->next;
}
if(tmp->num == num)
{
printf(" 搴忓彿:%d\n",tmp->num);
printf(" 鑱旂郴浜?%s\n",tmp->name);
printf(" 鐢佃瘽:%s\n",tmp->tel);
printf(" 閭:%s\n",tmp->email);
printf(" QQ:%s\n",tmp->qq);
}
else
{
printf("no such recort!\n");
return;
}
}
/*******淇敼鑱旂郴浜?******/
void Amend_Recort(Link *head)
{
int num,n;
Link tmp;
tmp = *head;
printf("***********************淇敼鑱旂郴浜?********************\n");
printf(" 杈撳叆瑕佷慨鏀圭殑鑱旂郴浜哄簭鍙?");
scanf("%d",&num);
while(tmp->num != num && tmp->next != NULL)
{
tmp = tmp->next;
}
if(tmp->num == num)
{
printf(" 1.搴忓彿\n");
printf(" 2.濮撳悕\n");
printf(" 3.鐢佃瘽\n");
printf(" 4.email\n");
printf(" 5.QQ\n");
printf(" 杈撳叆瑕佷慨鏀圭殑搴忓彿:");
scanf("%d",&n);
printf(" 杈撳叆瑕佷慨鏀圭殑鍐呭:");
switch(n)
{
case 1:scanf("%d",&tmp->num);break;
case 2:scanf("%s",tmp->name);break;
case 3:scanf("%s",tmp->tel);break;
case 4:scanf("%s",tmp->email);break;
case 5:scanf("%s",tmp->qq);break;
default:
printf("no such option!\n");
break;
}
printf(" 淇敼鎴愬姛锛乗n");
}
else
{
printf("no such recort!\n");
}
}