这个通讯录是不带文件操作的第一版。
1)2018年12月14日 15:31 发布
2)同日 17:11 第一次修改
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ERR 0
#define NORM 1
struct addresslist
{
char ID[20];
char name[20];
char address[30];
char tel[20];
char mobile[20];
struct addresslist * next;
};
typedef struct addresslist Formt;
typedef struct addresslist * Link;
void screen_head()
{
system("clear");
printf("***********************************\n");
printf("*****Address Book Ver 1.0 Alph*****\n");
printf("***********************************\n");
sleep(2);
system("clear");
}
void screen_info()
{
printf("************************************\n");
printf("1.Add user\n");
printf("2.List user\n");
printf("3.Search for uesr\n");
printf("4.Delete uesr info\n");
printf("5.Modify user info\n");
printf("6.Exit\n");
printf("************************************\n");
printf("Plz input your choise : \n");
}
void creat_link(Link *head)
{
(*head) = (Link)malloc(sizeof(Formt));
(*head) -> next = NULL;
}
int is_malloc(Link new_node)
{
if(new_node == NULL)
{
return ERR;
}
else
{
return NORM;
}
}
void insert_info(Link head)
{
Link new_node = NULL;
do
{
new_node = (Link)malloc(sizeof(Formt));
}while(is_malloc(new_node) == ERR);
printf("ID :\n");
scanf("%s",new_node -> ID);
printf("Name :\n");
scanf("%s",new_node -> name);
printf("Address :\n");
scanf("%s",new_node -> address);
printf("Tel :\n");
scanf("%s",new_node -> tel);
printf("mobile :\n");
scanf("%s",new_node -> mobile);
Link p = NULL;
p = head;
while(p -> next != NULL)
{
p = p -> next;
}
p -> next = new_node;
new_node -> next = NULL;
}
void search_info(Link head)
{
Link p = head -> next;
char name[10];
printf("plz input the name you want to find : \n");
scanf("%s" , name);
while(p)
{
if(strcmp(p -> name , name) == 0 )
{
printf("ID : %s \n Name : %s \n Address : %s \n Tel : %s \n Phone : %s \n",p -> ID ,p -> name , p -> address, p -> tel , p -> mobile );
}
else
{
p = p -> next;
}
if (p == NULL)
{
printf("there is nobody in list\n");
}
}
}
void list_info(Link head)
{
Link p = NULL;
if(NULL == head -> next)
{
printf("Nobody in list\n");
}
else
{
p = head -> next;
while(p)
{
printf("ID : %s \n Name : %s \n Address : %s \n Tel : %s \n Phone : %s \n",p -> ID ,p -> name , p -> address, p -> tel , p -> mobile );
p = p -> next;
}
}
return ;
}
void del_list(Link head)
{
Link p = head -> next;
Link tmp;
char name[30];
if(head -> next = NULL)
{
printf("The list is empty");
return;
}
while(p)
{
if(strcmp (p -> name , name) == 0)
{
tmp = p -> next;
p -> next = tmp -> next;
free(p);
printf("Delete Success\n");
return ;
}
}
}
int main()
{
Link head = NULL;
int choice = 0;
creat_link(&head);
screen_head();
while(1)
{
screen_info();
scanf("%d" , &choice);
switch(choice)
{
case 1:
insert_info(head);
printf("****************************************\n");
break;
case 2:
list_info(head);
printf("****************************************\n");
break;
case 3:
search_info(head);
printf("****************************************\n");
break;
case 4:
del_list(head);
printf("***************************************\n");
break;
}
}
return 0;
}