本文在链表操作的基础上,编写一个简易的手机通讯录,实现通讯录上对联系人的添加,删除,查找,排序等功能
#include <stdio.h>
#include <string.h>#include <stdlib.h>
#define LEN sizeof(struct content)
typedef struct content
{
char name[20];
int number;
struct content *next;
}CON;
CON * init_content()
{
CON *head;
head=(CON *)malloc(LEN);
if(head==NULL)
{
printf("ERROR\n");
}
head->next=NULL;
printf("\n");
printf(">>>>INIT IS GOOD!<<<<\n");
printf("\n");
return head;
}
void print_content(CON *head)
{
CON *p;
p=head->next;
if(p==NULL)
printf("PLEASE CREATE YOUR CONTENT!\n");
while(p!=NULL)
{
printf("%s %d\n",p->name,p->number);
p=p->next;
}
}
CON * create_content(CON *head)
{
CON *p;
head = (CON *)malloc(LEN);
//head->next = NULL; /* 先建立一个带头结点的单链表 */
p=head->next;
while(1)
{
p = (CON *)malloc(LEN); /* 生成新结点 */
printf(">> NAME :");
scanf(" %s", &p->name);
if(strcmp(p->name,"*")==0)
break;
printf(">> NUMBER :");
scanf("%d", &p->number);
p->next = head->next;
head->next = p; /* 插入到表头 */
}
printf("******************\n");
return head;
}
CON * insert_content(CON *head,int i)
{
char a[20]; int b;
CON *p,*s;
int j=1;
p = head;
printf(">>ADD NAME :");
scanf("%s",&a);
printf(">>ADD NUMBER :");
scanf("%d",&b);
while (p && j < i) /* 寻找第i个结点 */
{
p = p->next;
++j;
}
if (!p || j > i)
printf("NO FIND!"); /* 第i个元素不存在 */
s = (CON *)malloc(LEN); /* 生成新结点(C语言标准函数) */
strcpy(s->name,a);
s->number=b;
s->next = p->next; /* 将p的后继结点赋值给s的后继 */
p->next = s; /* 将s赋值给p的后继 */
printf("******************\n");
return head;
}
CON *sort_content(CON *head)
{
CON *p;
char a[20];
int i,j,temp;
p=head->next;
for(j=0;j<3;j++)
{
p=head->next;
for(i=0;i<3-j;i++)
{
if(strcmp(p->name,p->next->name)>0)
{
temp=p->number;
p->number=p->next->number;
p->next->number=temp;
strcpy(a,p->name);
strcpy(p->name,p->next->name);
strcpy(p->next->name,a);
}
p=p->next;
}
}
return head;
}
CON *delete_content(CON *head) //shan chu lian biao zhong zhi ding jie dian
{
CON *present,*after;
char s[20];
printf(">>DELETE NAME :");
scanf("%s",&s);
if(head==NULL)
{
printf("EMPTY!!!!\n");
return head;
}
if(strcmp(head->name,s)==0)
{
present=head;
head=head->next;
free(present);
}
else
{
present=head;
after=head->next;
while((after!=NULL)&&(strcmp(after->name,s)!=0))
{
present=after;
after=after->next;
}
if(after!=NULL)
{
present->next=after->next;
free(after);
}
else
printf("NO FIND!!!!\n");
}
return head;
}
void find_content(CON *head)
{
CON *p,*q;
char c[20];
printf("FIND NAME :");
scanf(" %s",&c);
p=head;
while(1)
{
if(strcmp(p->name,c)==0)
{
printf("NAME NUMBER\n");
printf("******************\n");
printf("%s %d\n",p->name,p->number);
break;
}
p=p->next;
if(p==NULL)
{
printf("******************\n");
printf("NO FIND!\n");
break;
}
}
}
void modify_content(CON *head)
{
CON *p,*q;
char c[20];
int g;
printf("MODIFY NAME :");
scanf(" %s",&c);
p=head;
while(1)
{
if(strcmp(p->name,c)==0)
{
printf("MODIFY NUMBER :");
scanf(" %d",&g);
p->number=g;
printf("NAME NUMBER\n");
printf("******************\n");
printf("%s %d\n",p->name,p->number);
break;
}
p=p->next;
if(p==NULL)
{
printf("******************\n");
printf("NO FIND!\n");
break;
}
}
}
void interface(void)
{
printf("*******************************************\n");
printf("** KEY 0 <<>> INIT **\n");
printf("** KEY 1 <<>> NEW **\n");
printf("** KEY 2 <<>> ADD **\n");
printf("** KEY 3 <<>> FIND **\n");
printf("** KEY 4 <<>> SORT **\n");
printf("** KEY 5 <<>> DELETE **\n");
printf("** KEY 6 <<>> MODIFY **\n");
printf("** KEY 9 <<>> EXIT **\n");
printf("*******************************************\n");
}
int main()
{
CON *head;
int KEY;
interface();
while(1)
{
printf(">>KEY A WORD<< :");
scanf("%d",&KEY);
switch(KEY)
{
case 0: head=init_content();
break;
case 1: head=create_content(head);
printf("NAME NUMBER\n");
print_content(head);
printf("******************\n");
break;
case 2: head=insert_content(head,1);
printf("NAME NUMBER\n");
print_content(head);
printf("******************\n");
break;
case 4: head=sort_content(head);
printf("NAME NUMBER\n");
print_content(head);
printf("******************\n");
break;
case 3: find_content(head);
printf("******************\n");
break;
case 5: head=delete_content(head);
print_content(head);
printf("******************\n");
break;
case 6: modify_content(head);
printf("******************\n");
break;
case 9: exit(0);
break;
default: printf("ERROR KEY!!!\n");
break;
}
}
return 0;
}