学习了链表后我用链表写了一个简单的通讯录程序,主要能实现以下功能
使用链表实现增加(在增加人员的过程中有一个自动排序功能,比如按姓名排序)、删除、修改、查找的功能;
(1)添加用户信息(号码长度 号码是否重复)
(2)列出好友信息(按姓名排序)
(3)查找好友信息(按姓名查找)
(4)删除好友
(5)退出
#ifndef _ADDRESS_H_
#define _ADDRESS_H_
#define N 30
#define FAILURE 10000
#define SUCCESS 10001
struct addresslist
{
char ID[N];
char Name[N];
char Add[N];
char Tel[N];
char HomeTel[N];
struct addresslist *next;
};
typedef struct addresslist Address;
typedef Address *LinkList;
#endif
#include
#include"address_list.h"
#include
#include
void show()
{
system("clear");
printf("******************************************\n\n");
printf("******* WELCOME TO ADDRESS LIST *******\n\n");
printf("******************************************\n\n");
sleep(2);
system("clear");
}
void PrintInfo()
{
printf("**************************************************************\n\n");
printf(" 1.添加用户信息 \n");
printf(" 2.列出好友信息 \n");
printf(" 3.查找好友信息 \n");
printf(" 4.删除好友 \n");
printf(" 5.修改好友信息 \n");
printf(" 6.退出 \n\n");
printf("**************************************************************\n\n");
printf("Please input your choice :\n");
}
void LinkInit(LinkList *L)
{
(*L)=(LinkList)malloc(sizeof(Address));
(*L)->next = NULL;
}
int InsertInfo(LinkList *L)
{
LinkList temp = *L;
LinkList p;
p=(LinkList)malloc(sizeof(Address));
printf("Please input the id:\n");
scanf("%s",p->ID);
printf("Please input the name:\n");
scanf("%s",p->Name);
printf("Please input the add:\n");
scanf("%s",p->Add);
printf("Please input the tel:\n");
scanf("%s",p->Tel);
while(strlen(p->Tel)!=11)
{
printf("Error,Please input the tel again!\n");
scanf("%s",p->Tel);
break;
}
printf("Please input the hometel:\n");
scanf("%s",p->HomeTel);
while(strlen(p->HomeTel)!=8)
{
printf("Error,Please input the hometel again!\n");
scanf("%s",p->HomeTel);
break;
}
while(temp->next)
{
if(strcmp((temp->next)->Tel,p->Tel) == 0)
{
printf("The same telphone!\n");
return 0;
}
temp = temp->next;
}
temp=*L;
if((temp->next)==NULL)
{
p->next=temp->next;
temp->next=p;
}
else
{
while(temp->next)
{
if(strcmp((temp->next)->Name,p->Name)>0)
{
p->next=temp->next;
temp->next=p;
break;
}
temp=temp->next;
}
if(temp->next ==NULL)
{
p->next = temp->next;
temp->next =p ;
}
}
}
int ListInfo(LinkList L)
{
if(NULL==L->next)
{
printf("The Addresslist is empty!\n");
}
else
{
LinkList p=L->next;
while(p)
{
printf("ID:%s NAME:%s ADD:%s TEL:%s HOMETEL:%s \n",&p->ID,p->Name,p->Add,p->Tel,p->HomeTel);
p=p->next;
}
}
}
int SearchInfo(LinkList L)
{
LinkList p = L->next;
char a[10]={0};
printf("Please input the name you want to find:\n");
scanf("%s",a);
while(p)
{
if(strcmp(p->Name,a)==0)
{
printf("ID:%s NAME:%s ADD:%s TEL:%s HOMETEL:%s \n",p->ID,p->Name,p->Add,p->Tel,p->HomeTel);
break;
}
else
{
p=p->next;
}
}
if(p==NULL)
{
printf("No one!\n");
}
}
int DeleteInfo(LinkList L)
{
LinkList p=L;
LinkList temp;
if(L==NULL)
{
return FAILURE;
}
char a[10]={0};
printf("Please input the name you want to delete:\n");
scanf("%s",a);
while(p)
{
if(strcmp((p->next)->Name,a)==0)
{
temp=p->next;
p->next=temp->next;
free(temp);
printf("Delete Success!\n");
break;
}
}
return SUCCESS;
}
int ChangeInfo(LinkList *L)
{
LinkList p=*L;
LinkList temp;
LinkList n=*L;
char a[10]={0};
printf("Please input the name you want to change:\n");
scanf("%s",a);
while(p->next)
{
temp=(LinkList)malloc(sizeof(Address));
if(strcmp((p->next)->Name,a)==0)
{
printf("Please input the information you want to change:ID NAME ADD TEL HOMETEL\n");
scanf("%s%s%s%s%s",&temp->ID,temp->Name,temp->Add,temp->Tel,temp->HomeTel);
if(strlen(temp->Tel) != 11)
{
printf("ERROR Tel!\n");
return 0;
}
if(strlen(temp->HomeTel) != 8)
{
printf("ERROR HomeTel!\n");
return 0;
}
while(n->next)
{
if(strcmp((n->next)->Tel,temp->Tel) == 0)
{
printf("Exist the same tel!\n");
return 0;
}
n = n->next;
}
strcpy((p->next)->ID , temp->ID);
strcpy((p->next)->Name , temp->Name);
strcpy((p->next)->Add , temp->Add);
strcpy((p->next)->Tel , temp->Tel);
strcpy((p->next)->HomeTel , temp->HomeTel);
printf("Change Success!\n");
break;
}
else
{
p = p->next;
}
}
}
#include
#include"address_list.h"
#include
int main()
{
LinkList list;
char choice[10] = {0};
LinkInit(&list);
show();
while (1)
{
PrintInfo();
scanf("%s", choice);
switch (atoi(&choice[0]))
{
case 1:
InsertInfo(&list);
printf("**************************************************************\n\n");
break;
case 2:
ListInfo(list);
printf("**************************************************************\n\n");
break;
case 3:
SearchInfo(list);
printf("**************************************************************\n\n");
break;
case 4:
DeleteInfo(list);
printf("**************************************************************\n\n");
break;
case 5:
ChangeInfo(&list);
printf("**************************************************************\n\n");
break;
case 6:
exit(1);
break;
default:
printf("Unkown Input!\n");
break;
}
}