/*头文件*/
#ifndef __AddressBook_H__
#define __AddressBook_H__
#define FALSE 0
#define TRUE 1
typedef struct data
{
int id;
char name[20];
int num;
char addr[20];
int cnum;
}Data;
typedef Data LinkData;
typedef struct _node
{
LinkData data;
struct _node * next;
}Node;
//管理主界面
void AddressBook(Node* h);
//数据插入结构体,再传给链表;
void Insert(Node *h);
//排序
void rank(Node* h);
//展示
void Display(Node* h);
//查找
int Search(Node* h);
//删除好友
int Distroy(Node* h);
int Get_Len(Node *h);
// 创建链表
Node * Create_List();
// 头插
int Insert_Head(Node *h, LinkData data);
//尾插
int Insert_Last(Node *h, LinkData data);
#endif
/*功能函数文件*/
------------------------------------------------------
#include "AddressBook.h"
#include
#include
//主界面
void AddressBook(Node *h)
{
int a;
//system("cls");
//system("color f4");
printf(" |--------------------------------------------------|\n");
printf(" | |\n");
printf(" |-----------------------AddressBook----------------|\n");
printf(" | |\n");
printf(" |==================================================|\n");
printf(" | |\n");
printf(" |---------------------1、添加好友列表--------------|\n");
printf(" | |\n");
printf(" |---------------------2、列表好友信息--------------|\n");
printf(" | |\n");
printf(" |---------------------3、搜索好友------------------|\n");
printf(" | |\n");
printf(" |---------------------4、删除好友------------------|\n");
printf(" | |\n");
printf(" |---------------------0、退出操作------------------|\n");
printf(" | |\n");
printf(" |==================================================|\n");
while(1)
{
printf("请输入需要的功能\n",a);
scanf("%d",&a);
switch(a)
{
case 0:
break;
case 1:
Insert(h);
Insert(h);
break;
case 2:
Display(h);
break;
case 3:
Search(h);
break;
case 4:
Distroy(h);
break;
}
int i;
printf("输入数字1退出,其它数字继续操作:\n");
scanf("%d",&i);
if(i == 1)
break;
}
}
//创建表;
Node * Create_List()
{
Node *list = (Node*)malloc(sizeof(Node)/sizeof(char));
if (list == NULL)
return NULL;
list->next = NULL; // 空表
return list;
}
#if 0
void Insert(Node *h)
{
Node *node = Create_List();
printf("输入ID:\n");
scanf("%d",&node->data.id);
printf("输入姓名:\n");
scanf("%s",node->data.name);
printf("输入号码:\n");
scanf("%d\n",&node->data.num);
printf("输入地址:\n");
scanf("%s",node->data.addr);
printf("输入公司电话:\n");
scanf("%d",&node->data.cnum);
Insert_Last(h, node->data);
free(node);
}
#endif
void Insert(Node *h)
{
Node * node = Create_List();
printf("请输入ID:\n");
scanf("%d",&node->data.id);
printf("请输入姓名:\n");
scanf("%s",node->data.name);
printf("请输入手机号码:\n");
scanf("%d",&node->data.num);
printf("请输入地址:\n");
scanf("%s",node->data.addr);
// printf("输入公司电话:\n");
// scanf("%d",node->data.cnum);
//起初输入三个之后崩溃,重新copy了一份?输入五个会有段错误
Insert_Last(h, node->data);
free(node);
}
//链表长度;
int Get_Len(Node *h)
{
if(h == NULL)
return 0;
Node* tmp = h;
int count = 0;
while(tmp->next)
{
count++;
tmp = tmp->next;
}
return count;
}
//排序
void rank(Node* h)
{
int res = Get_Len(h);
Node* tmp = h->next;
int i;
while(res-1)
{
tmp=h->next;
for(i = 0;i < res-1;i++)
{
if(tmp->data.id > tmp->next->data.id)
{
LinkData k = tmp->data;
tmp->data = tmp->next->data;
tmp->next->data = k;
}
tmp = tmp->next;
}
res--;
}
}
//展示
void Display(Node* h)
{
if (h == NULL)
return;
rank(h);
int count = 0;
Node *tmp = h->next;
while (tmp)
{
if (count++ % 4 == 0)
printf ("\n");
printf ("%8d", tmp->data.id);
printf ("%8s", tmp->data.name);
printf ("%8d", tmp->data.num);
printf ("%8s", tmp->data.addr);
printf("\n");
tmp = tmp->next;
}
printf ("\n");
}
#if 0
//查找
int Search(Node* h)
{
int pos;
printf("输入pos:\n");
scanf("%d",&pos);
if(h == NULL||pos < 1)
return FALSE;
int i;
Node* tmp = h;
for(i = 0;i < pos;i++)
{
if(tmp == NULL)
break;
tmp = tmp->next;
}
if(tmp == NULL)
return FALSE;
else
printf ("%8d", tmp->data.id);
printf ("%8s", tmp->data.name);
printf ("%8d", tmp->data.num);
printf ("%8s", tmp->data.addr);
printf("\n");
}
#endif
int Search(Node* h)
{
if(h == NULL)
return FALSE;
Node* tmp = h->next;
int i;
printf("输入要查找的id号:");
scanf("%d",&i);
while(tmp)
{
if(tmp->data.id == i)
{
printf ("%8d", tmp->data.id);
printf ("%8s", tmp->data.name);
printf ("%8d", tmp->data.num);
printf ("%8s", tmp->data.addr);
printf("\n");
break;
}
tmp = tmp->next;
}
}
//删除好友
int Distroy(Node* h)
{
char name[20];
printf("请输入名字:");
scanf("%s",name);
if(h == NULL)
return 0;
Node* tmp = h;
int count = 0;
while(tmp->next)
{
if(strcmp(tmp->next->data.name,name) == 0)
{
count++;
printf ("%8d", tmp->data.id);
printf ("%8s", tmp->data.name);
printf ("%8d", tmp->data.num);
printf ("%8s", tmp->data.addr);
printf("\n");
}
tmp = tmp->next;
}
if(count == 0)
{
printf("");
return FALSE;
}
else if(count == 1)
{
tmp = h;
while(tmp->next)
{
if(strcmp(tmp->next->data.name,name) == 0)
break;
tmp = tmp->next;
}
if(tmp->next == NULL)
return FALSE;
//删除操作
Node* p = tmp->next;
tmp->next = p->next;
free(p);
p = NULL;
}
else
{
int id;
printf("有同名,请输入id删除:");
scanf("%d",&id);
tmp = h;
while(tmp->next)
{
if(strcmp(tmp->next->data.name,name) == 0 && tmp->next->data.id == id)
break;
tmp = tmp->next;
}
if(tmp->next == NULL)
return FALSE;
//删除操作
Node* p = tmp->next;
tmp->next = p->next;
free(p);
p = NULL;
}
return TRUE;
}
int Insert_Head(Node *h, LinkData data)
{
if (h == NULL)
return FALSE;
Node *node = (Node*)malloc(sizeof(Node)/sizeof(char));
if (node == NULL)
{
return FALSE;
}
node->data = data;
node->next = h->next;
h->next = node;
return TRUE;
}
//尾部插入
int Insert_Last(Node *h, LinkData data)
{
if(h == NULL)
return FALSE;
Node *node = (Node*)malloc(sizeof(Node)/sizeof(char));
if(node == NULL)
return FALSE;
node->data = data;
node->next = NULL;
Node* tmp = h;
while(tmp->next)
{
tmp = tmp->next;
}
tmp->next = node; //三句话 node->date=date,node->next=NULL,tmp_last->next=node;
return TRUE;
}
/*主函数*/
--------------------------------------------------------------------
#include "AddressBook.h"
#include
int main()
{
Node *head = Create_List();//head指向创建的空表;
if(head == NULL)
{
printf("创建链表失败\n");
return -1;
}
AddressBook(head);
return 0;
}
实现功能如下: