#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define TRUE 1000 // 操作成功
#define FALSE -1000 // 操作失败
typedef char Name;
typedef long Phone;
typedef struct com
{
int id; // 人的ID
Name *name; //姓名
char phone; // 手机号
int adr; // 家庭住址
Phone comp_num; //公司号码
struct com next;
}Com;
// 创建顺序表
Com* CreatePerson();
int Insert(Com* comm);
int search(Com* comm);
int Del(Com* comm);
void sort(Com* comm);
void Print();
void Choose(Com* comm);
// 主函数
int main()
{
Com* comm = CreatePerson();
if (comm == NULL)
{
return -1;
}
Mm();
Choose(comm);
return 0;
}
//创建
Com* CreatePerson()
{
Com* comm = (Com*)malloc(sizeof(Com)/sizeof(char));
if (comm == NULL)
return NULL;
comm->name = (Name*)malloc(sizeof(Name)/sizeof(char)*10);
comm->phone = (char )malloc(sizeof(char) 13);
if (comm->name == NULL || comm->phone == NULL)
{
free(comm);
printf (“输入失败\n”);
return NULL;
}
comm->id = 0;
comm->adr = 0;
comm->comp_num = 0;
comm->next = NULL;
return comm;
}
//删除
int Del(Com* comm)
{
if (comm == NULL)
{
printf (“链表为空\n”);
return FALSE;
}
int count = 0;
printf (“请输入要删除的名字\n”);
char name[10];
scanf ("%s", name);
Com* p1;
Com* person = comm;
while (person->next)
{
if (strcmp(person->next->name,name) == 0)
{
if (count == 0)
p1 = person->next;
if (count >= 1)
{
printf (“ID\t姓名\t手机号\t\t家庭住址\t公司号码\n”);
printf ("%d\t%s\t%s\t%d\t%ld\n",person->next->id, person->next->name, person->next->phone, person->next->adr, person->next->comp_num);
printf ("%d\t", p1->id);
printf ("%s\t", p1->name);
printf ("%s\t", p1->phone);
printf ("%d\t", p1->adr);
printf ("%ld\n", p1->comp_num);
}
count++;
}
person = person->next;
}
person = comm;
while (person->next)
{
if (count == 1 && strcmp(person->next->name,name) == 0)
{
Com* p = person->next;
person->next = p->next;
free(p->phone);
free(p->name);
free(p);
printf ("删除成功\n");
printf ("请输入命令\n");
return TRUE;
}
else
{
printf ("请输入ID\n");
int id;
scanf ("%d", &id);
if (person->next->id = id)
{
Com* p = person->next;
person->next = p->next;
free(p->name);
free(p->phone);
free(p);
printf ("删除成功\n");
printf ("请输入命令\n");
return TRUE;
}
}
person = person->next;
}
printf ("该好友不存在\n");
printf ("请输入命令\n");
return FALSE;
}
//查询
int search(Com* comm)
{
if (comm == NULL)
{
printf (“通讯录为空\n”);
return FALSE;
}
Name name[10];
printf ("请输入要查找的名字\n");
scanf ("%s",name);
Com* person = comm;
while (person)
{
if (strcmp(person->name, name) == 0)
{
printf ("ID\t姓名\t手机号\t\t家庭住址\t公司号码\n");
printf ("%d\t", person->id);
printf ("%s\t", person->name);
printf ("%s\t", person->phone);
printf ("%d\t", person->adr);
printf ("%ld\n", person->comp_num);
printf ("请输入命令\n");
return TRUE;
}
person = person->next;
}
printf ("查无此人\n");
printf ("请输入命令\n");
return FALSE;
}
//头插法
int Insert(Com* comm)
{
if (comm == NULL)
return FALSE;
int id; // 人的ID
char phone[13]; // 手机号
int adr; // 家庭住址
Phone comp_num;
char name[10];
Com* person = CreatePerson();
printf ("请输入:\nID\t姓名\t手机号\t\t家庭住址\t公司号码\n");
scanf ("%d%s%s%d%ld", &id, name, phone, &adr, &comp_num);
if (person == NULL)
return FALSE;
//赋值
person->id = id;
strcpy(person->name, name);
strcpy(person->phone , phone);
person->adr = adr;
person->comp_num = comp_num;
person->next = comm->next;
comm->next = person;
printf ("输入成功\n");
printf ("请输入命令\n");
return TRUE;
}
//冒泡排序
void sort(Com* comm)
{
if (comm == NULL || comm->next == NULL || comm->next->next == NULL)
return ;
int count = 0;
Com* person = comm->next;
while (person)
{
count++;
person = person->next;
}
//将最大的数放在最后
while(count != 0)
{
Com* p1 = comm;
Com* p2 = p1->next;
Com* p3 = p2->next;
while(p3)
{
if (p2->id > p3->id)
{
p2->next = p3->next;
p1->next = p3;
p3->next = p2;
p1 = p2;
p2 = p3;
p3 = p2->next;
}
else
{
p1 = p2;
p2 = p3;
p3 = p3->next;
}
}
count--;
}
}
//显示
void Display(Com* comm)
{
if (comm == NULL)
return;
sort(comm);
printf (“ID\t姓名\t手机号\t\t家庭住址\t公司号码\n”);
Com* p = comm->next;
while §
{
printf ("%d\t", p->id);
printf ("%s\t", p->name);
printf ("%s\t\t", p->phone);
printf ("%d\t\t", p->adr);
printf ("%ld\n", p->comp_num);
p = p->next;
}
printf ("请输入命令\n");
return;
}
// 显示命令
void Print()
{
printf (" 通讯录\n");
printf ("********************************************\n");
printf (“1、插入命令 :INSERT\n”);
printf (“2、删除命令 :DEL\n”);
printf (“3、查找命令 :SEARCH\n”);
printf (“4、显示命令 :DISPLAY\n”);
printf (“5、退出命令 :BREAK\n”);
printf ("*******************************************\n");
printf (“请输入命令\n”);
}