实现添加,查找,删除,查看联系人
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define isLink 1
#define unLink 0
struct person
{
char name[20];
int sex;
char tel[15];
struct person * next;
};
typedef struct person Node;
typedef struct person * Link;
void creatlink(Link* head)
{
do
{
*head = (Link)malloc(sizeof(Node));
(*head)->next = NULL;
}while(if_creatok(*head) == unLink);
}
int if_creatok(Link newnode)
{
if(newnode == NULL)
{
printf("this node is not exist");
return unLink;
}
else
{
return isLink;
}
}
/*void creatnode(Link *newnode)
{
do
{
* newnode = (Link)malloc(sizeof(Node));
}while(if_creatok(*newnode) == unLink);
printf("please input name:\n");
scanf("%s",(*newnode)->name);
printf("please input sex:\n");
scanf("%d",&(*newnode)->sex);
printf("please input tel:\n");
scanf("%s",(*newnode)->tel);
}*/
void insert_node_tail(Link head)
{
Link p = head;
Link newnode = NULL;
/* while(p->next != NULL)
{
p = p->next;
}
p->next = newnode;
newnode->next = NULL;*/
do
{
newnode = (Link)malloc(sizeof(Node));
}while(if_creatok(newnode) == unLink);
printf("please input name:\n");
scanf("%s",(newnode)->name);
getchar();
printf("please input sex:\n");
scanf("%d",&(newnode)->sex);
getchar();
printf("please input tel:\n");
gets((newnode)->tel);
while(p->next != NULL)
{
p = p->next;
}
p->next = newnode;
newnode->next = NULL;
}
void display(Link head)
{
Link p = head;
if(p == NULL)
{
printf("link id already clear\n");
}
else
{
p = p->next;
while(p != NULL)
{
printf("name:%s\n",p->name);
printf("sex:%d\n",p->sex);
printf("tel:%s\n",p->tel);
p = p->next;
}
}
}
void releasenode(Link *head)
{
Link p =* head;
if(p == NULL)
{
printf("link is already clear\n");
}
else
{
while(p != NULL)
{
*head = (*head)->next;
free(p);
p = *head;
}
}
}
void deletenode(Link head)
{
Link p ,q;
p = q = head;
char name[20];
int i;
printf("please input name of contacts you want to delete\n");
scanf("%s",name);
while(p -> next)
{
if(strcmp(p->next->name,name) == 0)
{
i++;
}
p = p->next;
}
if(i == 0)
{
printf(" no such person");
}
if( i == 1)
{
p = head;
while(p->next)
{
if(strcmp(p->next->name,name) == 0)
{
Link q = NULL;
q = p->next;
p->next = q->next;
free(q);
printf("delete finish\n");
}
else
{
p =p->next;
}
}
}
if( i > 1)
{
printf("more than one contacts you want to delete ,please input the tel of contacts you want to delete\n ");
char num[20];
p = head;
while(p->next)
{
if(strcmp(p->next->name,name)== 0 )
{
printf(" name:%s\t sex:%d\t tel:%s\t",p->next->name,p->next->sex,p->next->tel);
}
p = p->next;
}
scanf("%s",num);
p = head;
while(p->next)
{
if(strcmp(p->next->tel,num)== 0 )
{
Link q = NULL;
q = p->next;
p ->next = q->next;
free(q);
printf("delete finish!\n");
}
else
{
p = p->next;
}
}
}
}
void serchnode(Link head)
{
Link p ;
p = head;
printf("please input name you want to search for\n");
char name[20];
scanf("%s",name);
while(p != NULL)
{
if(strcmp(p->name,name) == 0)
{
printf("\nname:%s",p->name);
printf("\nsex:%d", p->sex);
printf("\ntel:%s", p->tel);
}
p = p->next;
}
}
int main()
{
Link head = NULL ;
Link newnode = NULL;
creatlink(&head);
int action ;
printf("*************Welcome to Address List**************\n");
printf("*** What can I do for you ? ***\n");
printf("** Add Contacts please input 1 **\n");
printf("** Delete Contacts please input 2 **\n");
printf("** Modify contacts please input 3 **\n");
printf("*** Display contacts please input 4 ***\n");
printf("*** Search Contacts please input 5 ***\n");
printf("*** exit please input 0 ****\n");
printf("**************************************************\n");
while(1)
{
printf("please input :\n");
scanf("%d",&action);
switch(action)
{
case 1:// creatnode(&newnode);
insert_node_tail(head);
break;
case 2: deletenode(head);
display(head);
break;
case 3:
display(head);
break;
case 4: display(head);
break;
case 5: serchnode(head);
break;
case 0: releasenode(&head);
display(head);
break;
default:
break;
}
if(action == 0)
{
break;
}
}
return 0;
}
结果: