”txu.h"文件
#ifndef __TXU_H__
#define __TXU_H__#define FALSE 0
#define TRUE 1
typedef struct _friend
{
int id;
char name[20];
char numb[20];
char address[20];
char cnumb[20];
}Friend;
typedef struct _node
{
Friend friend;
struct _node *next;
}Node;
// 创建链表
Node * Create_List();
//显示菜单
void menu();
//添加好友
int add_Friend(Node *h);
//列表好友
int show_Friend(Node *h);
//查找好友
int Find_Friend(Node* h);
//删除好友
int Delete_Friend(Node* h);
#endif
“txu.c"文件
#include"txu.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
Node * Create_List()
{
Node *list = (Node*)malloc(sizeof(Node)/sizeof(char));
if (list == NULL)
return NULL;
list->next = NULL; // 空表
return list;
}
void menu()
{
printf("\t\t\t***************************************************\n");
printf("\t\t\t| |\n");
printf("\t\t\t| 电子通讯录 |\n");
printf("\t\t\t***************************************************\n");
printf("\t\t\t| |\n");
printf("\t\t\t|-------------------------------------------------|\n");
printf("\t\t\t| 1:添加朋友信息 2:列表朋友信息 |\n");
printf("\t\t\t|-------------------------------------------------|\n");
printf("\t\t\t| 3:搜索查找朋友 4:删除朋友信息 |\n");
printf("\t\t\t|-------------------------------------------------|\n");
printf("\t\t\t***************************************************\n");
}
int add_Friend(Node *h)
{
if(h==NULL)
return FALSE;
Node *node=(Node*)malloc(sizeof(Node)/sizeof(char));
if (node == NULL)
{
return FALSE;
}
printf("请输入id:");
scanf("%d",&(node->friend.id));
printf("请输入name:");
scanf("%s",node->friend.name);
printf("请输入numb:");
scanf("%s",node->friend.numb);
printf("请输入address:");
scanf("%s",node->friend.address);
printf("请输入cnumb:");
scanf("%s",node->friend.cnumb);
node->next=NULL;
Node* tmp=h;
while (tmp->next)
{
tmp = tmp->next;
}
tmp->next = node;
return TRUE;
}
int show_Friend(Node *h)
{
if(h==NULL)
return FALSE;
int i=0;
Node *pre=h->next;
Node *cur=h->next->next;
Node *tmpl;
Node *p=h;
while(1)
{
if(h==NULL|| h->next == NULL || h->next->next == NULL)
break;
pre=h->next;
cur=h->next->next;
p=h;
i=0;
while(cur!=NULL)
{
tmpl=cur->next;
if((pre->friend.id)>(cur->friend.id))
{
cur->next=pre;
pre->next=tmpl;
p->next=cur;
p=p->next;
cur=tmpl;
i++;
}
else
{
p=pre;
pre=cur;
cur=tmpl;
}
}
if(i==0)
{
break;
}
}
Node *tmp=h->next;
while(tmp!=NULL)
{
printf("id=%d\t",tmp->friend.id);
printf("name=%s\t",tmp->friend.name);
printf("numb=%s\t",tmp->friend.numb);
printf("address=%s\t",tmp->friend.address);
printf("cnumb=%s\n",tmp->friend.cnumb);
tmp=tmp->next;
}
printf("\n");
}
int Find_Friend(Node* h)
{
if (h == NULL)
return FALSE;
char *name;
printf("请输入一个名字:");
scanf("%s",name);
Node *tmp=h->next;
while(tmp)
{
if(strcmp(tmp->friend.name,name)==0)
{
printf("id=%d\t",tmp->friend.id);
printf("name=%s\t",tmp->friend.name);
printf("numb=%s\t",tmp->friend.numb);
printf("address=%s\t",tmp->friend.address);
printf("cnumb=%s\n",tmp->friend.cnumb);
}
tmp=tmp->next;
}
return FALSE;
}
int Delete_Friend(Node* h)
{
if (h == NULL)
return FALSE;
Node *tmp=h;
char *name;
printf("请输入一个名字:");
scanf("%s",name);
int bn=0;
while(tmp->next)
{
if(strcmp(tmp->next->friend.name,name)==0)
{
printf("name:%s\n",tmp->next->friend.name);
printf("id:%d\n",tmp->next->friend.id);
bn++;
}
tmp=tmp->next;
}
if(bn>1)
{
tmp=h;
int id;
printf("请输入一个id:\n");
scanf("%d",&id);
while(tmp->next)
{
if(tmp->next->friend.id==id)
{
Node *p=tmp->next;
tmp->next=p->next;
free(p);
printf("\n删除成功\n");
return 0;
}
}
}
else
{
tmp=h;
while(tmp->next)
{
if(strcmp(tmp->next->friend.name,name)==0)
{
Node *p=tmp->next;
tmp->next=p->next;
free(p);
printf("删除成功\n");
}
}
}
return TRUE;
}
"main.c"主函数文件
#include <stdio.h>
#include"txu.h"
int main()
{
Node* head = Create_List();
if (head == NULL)
{
printf("创建链表失败\n");
return -1;
}
while(1)
{
menu();
int choice;
//system("clear");
printf("请输入你的选择:");
scanf("%d", &choice);
switch(choice)
{
case 1:add_Friend(head);break;
case 2:show_Friend(head);break;
case 3:Find_Friend(head);break;
case 4:Delete_Friend(head);break;
}
}
return 0;
}