**
链表实现带增删查改的手机通讯录
**
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
typedef struct _ContactsData
{
char name[40];
char mobileNum[20];
char fixedNum[20];
char companyName[50];
}ContactsData;
typedef struct _ContactsNode
{
ContactsData data;
struct _ContactsNode *next;
}ContactsNode;
//链表的遍历
void travalist(ContactsNode *head)
{
head=head->next;
while(head)
{
printf("%10s %10s %8s %12s\n",head->data.name,head->data.mobileNum,head->data.fixedNum,
head->data.companyName );
head=head->next;
}
}
//插入
ContactsNode *creatlist()
{
ContactsNode * head=(ContactsNode*)malloc(sizeof(ContactsNode));
if(NULL==head)
exit(-1);
head->next =NULL;//创建一个空链表
return head;
}
//查
ContactsNode *searchList(ContactsNode *head,char * pname)
{
head=head->next ;
while(head)
{
if(!strcmp(head->data.name ,pname))
{
printf("%10s %10s %5s %10s\n",head->data.name,head->data.mobileNum,head->data.fixedNum,
head->data.companyName );
break;
}
head=head->next;
}
return head;
}
//优化
void deleteNodeofList(ContactsNode * head,ContactsNode * pfind)
{
if(pfind->next==NULL)
{
while(head->next!=pfind)
head=head->next;//找到pfind
head->next=pfind->next;
free(pfind);
pfind=NULL;
}
else
{
ContactsNode *t=pfind->next;
pfind->data=pfind->next->data;//将下一个数据的直接赋给前一个
pfind->next=pfind->next->next;
free(t);
}
}
//(求长),
int lenList(ContactsNode* head)
{
int len=0;
head=head->next ;
while(head)
{
len++;
head=head->next ;
}
return len;
}
//冒泡排序
void popSortList(ContactsNode *head)
{
int len=lenList(head);
ContactsNode *prep,*p,*q,*t;//*t用于指针的交换
for(int i=0;i<len-1;i++)
{
prep=head;
p=head->next;
q=p->next;
for(int j=0;j<len-1-i;j++)
{
if(strcmp(p->data.name,q->data.name)>0 )
{
prep->next =q;
p->next =q->next ;
q->next =p;
t=p;
p=q;
q=t;
}
prep=prep->next ;//无论怎样都要移动
p=p->next ;
q=p->next ;
}
}
}
void *insertList(ContactsNode *head,char *name,char *mobileNum,char *fixedNum,char *companyName)//插入链表
{
ContactsNode* cur=(ContactsNode *)malloc(sizeof(ContactsNode));
if(NULL==head)
exit(-1);
strcpy(cur->data.name,name);
strcpy(cur->data.mobileNum,mobileNum);
strcpy(cur->data.fixedNum,fixedNum);
strcpy(cur->data.companyName,companyName);
//头插法与尾插法的主要区别在此 ,让先来的指针有所指向
cur->next=head->next;
head->next=cur;
}
//链表的逆置
void reverseList(ContactsNode *head)
{
ContactsNode *cur= head->next;
head->next=NULL;
ContactsNode * t;
while(cur)
{
t=cur;
cur=cur->next ;
t->next =head->next;
head->next=t;
}
}
//链表的销毁
//有多少个malloc就要销毁多少个free
void destoryList(ContactsNode *head)
{
while(head)
{
ContactsNode *t=head;
head=head->next;
free(t);
}
}
int main()
{
ContactsNode *head=creatlist();
srand(time(NULL));
for(int i=0;i<5;i++)
{
printf("请输入通讯录信息\n");
scanf("%s %s %s %s",head->data.name,head->data.mobileNum,head->data.fixedNum,head->data.companyName);
insertList(head,head->data.name,head->data.mobileNum,head->data.fixedNum,head->data.companyName);
}
printf("\n\n打印所有的通讯录信息\n");
printf("%10s %10s %10s %10s \n","name","mobileNum","fixedNum","companyName");
travalist(head);
puts("\n");
printf("通讯录联系人人数 %d\n\n",lenList(head));
printf("按姓名字母顺序排序\n");
printf("%10s %10s %10s %10s \n","name","mobileNum","fixedNum","companyName");
popSortList(head);
travalist(head);
printf("\n查找的姓名信息\n");
printf("%10s %10s %10s %10s \n","name","mobileNum","fixedNum","companyName");
ContactsNode *pfind=searchList(head,(char *)"yyt");
printf("您需要删除的联系人是 %s\n",pfind->data.name);
if(pfind==NULL)
printf("未发现\n\n");
else
{
printf("you find in list\n\n");
printf("删除后所有的通讯录信息\n");
deleteNodeofList(head,pfind);
}
printf("%10s %10s %10s %10s \n","name","mobileNum","fixedNum","companyName");
travalist(head);
destoryList(head);
return 0;
}
功能实现效果如下图