您的位置:首页 > 运维架构 > Linux

用链表实现的通讯录的功能

2017-07-27 10:36 375 查看
要求:制作一个电子通讯录,通过该通讯录能存入好友 ID 号、 姓名) (英文)、 手机号码、家庭住址、公司电话

模块

主界面:主要显示软件功能。

1. 添加好友信息。

2. 列表好友信息。(包含排序功能)

3. 搜索好友

4. 删除好友

  添加好友:

用户输入 1后,让用户输入好友信息。添加成功或失败都需要提示用户

  列表好友:

用户输入 2后,好友信息升序排列。

  搜索好友:

用户输入 3后,让用户输入将要搜索好友姓名查询。如果未搜索到请友好提示。如果搜索到,显示出该好友信息。

  删除好友:

用户输入 4后,让用输入将要删除好友姓名删除,如果存在同名的多个好友,则列表出,所有同名的好友信息,让用户通过输入 ID 号删除。提示用户删除成功。

#ifndef __LINKLIST_H__

#define __LINKLIST_H__

#define TRUE   1

#define FALSE -1

#define SIZE  50

typedef struct _people

{
int id;
char name[SIZE];
char tel[SIZE];
char address[SIZE];
char ctel[SIZE];

}LinkData;

typedef struct _node

{
LinkData data;
struct _node *next;

}Node;

int Quit();

int Sort_List(Node *h);

void Choice(Node *h);

void Search(Node *h);

void Add(Node *h);

void Delete(Node *h);

Node *Find_Element2(Node *h, LinkData d, int *x);

int Delete_Id(Node *h, int d);

int Find_nElement(Node *h, LinkData d);

void Display_Pos(Node *h, int x);

int Fun(int x, Node *h);

void MainInterface();

int Init(Node *h);

Node *Create_List();

void Display_List(Node *h);

int Insert_Last(Node *h, LinkData data);

int Insert_Head(Node *h,LinkData data);

int Insert_Pos(Node *h, int pos, LinkData data);

int Delete_Pos(Node *h, int pos);

int Delete_Data(Node *h, LinkData data);

int Find_Element(Node *h, LinkData d, int *x);

int Get_Element(Node *h, int pos, LinkData *x);

int Reverse_List(Node *h);

int Get_Len(Node *h);

int Clean_List(Node *h);

int Destroy(Node *h);

#endif

#include "LinkList.h"

#include <stdlib.h>

#include <stdio.h>

Node *Create_List()

{
Node *node = (Node *)malloc(sizeof(Node)/sizeof(char));
if(node == NULL)
return NULL;
node->next = NULL;
return node;

}

int Fun(int x,Node *h)

{

    MainInterface();
switch(x)
{
case 1:
//MainInterface();
Add(h);
break;
case 2:
//MainInterface();
Sort_List(h);
       Display_List(h);
break;
case 3:
//MainInterface();
Search(h);
break;
case 4:
//MainInterface();
Delete(h);
break;
case 5:
system("clear");
return FALSE;
//Quit();
default:
printf("输入错误,请重新输入\n");
break;

}
return TRUE;

}

int Sort_List(Node *h)

{
if(h == NULL || h->next == NULL || h->next->next == NULL)
return FALSE;
int n = Get_Len(h);
int i,j;
for(i=0; i<n-1; i++)
{
Node *pre = h->next;
Node *cur = h->next->next;
Node *tmp = h;
for(j=0; j<n-1-i; j++)
{
if(pre->data.id > cur->data.id)
{
pre->next = cur->next;
cur->next = pre;
tmp->next = cur;
tmp = cur;
cur = pre->next;
}
else
{
tmp = pre;
pre = cur;
cur = cur->next;
}
}
}
return TRUE;

}

int Init(Node *h)

{
if(h == NULL)
return FALSE;
LinkData p1 = {5, "liming", "13151587968", "南京市江宁区弘景大道66号", "025-543649"};
LinkData p2 = {4, "liyang", "13151585173", "上海市宝山区江阳大道67号", "025-785241"};
LinkData p3 = {3, "liyang", "13151584563", "南通市如东县光明大道68号", "025-555633"};
LinkData p4 = {2, "liyang", "13151587758", "无锡市富贵区明天大道70号", "025-123789"};
LinkData p5 = {1, "zhangsan", "13151581238", "南京市江宁区弘景大道45号", "025-542574"};

Insert_Last(h,p1);
Insert_Last(h,p2);
Insert_Last(h,p3);
Insert_Last(h,p4);
Insert_Last(h,p5);

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;
return TRUE;

}

void Display_List(Node *h)

{
if(h == NULL)
return;
int count = 0;
Node *tmp = h->next;
while(tmp)
{
printf("\tid: %d                     姓名:%s\n",tmp->data.id,tmp->data.name);
printf("\t个人电话:%s     家庭住址:%s\n",tmp->data.tel,tmp->data.address);
printf("\t公司电话:%s\n",tmp->data.ctel);
printf("\n");
//printf("\t%d,%s,%s,%s,%s\n",tmp->data.id,tmp->data.name,tmp->data.tel,tmp->data.address,tmp->data.ctel);
tmp = tmp->next;
}
printf("\n");

}

void MainInterface()

{

system("clear");
int i,j,k;
for(i=0; i<10; i++)
{
printf(" ");
}
for(i=0; i<50; i++)
{
if(i == 25)
printf("M E N U");
printf("*");
}
printf("\n");

for(j=0; j<11; j++)
{
if(j==1)
{
printf("%11c",'*');
printf("  1.添加好友信息");
printf("%40c",'*');
}
else if(j==3)
{
printf("%11c",'*');
printf("  2.列表好友信息");
printf("%40c",'*');
}
else if(j==5)
{
printf("%11c",'*');
printf("  3.搜索好友");
printf("%44c",'*');
}
else if(j==7)
{
printf("%11c",'*');
printf("  4.删除好友");
printf("%44c",'*');
}
else if(j==9)
{
printf("%11c",'*');
printf("  5.退出");
printf("%48c",'*');
}
else
{

for(i=0; i<10; i++)
{
printf(" ");
}
for(i=0; i<56; i++)
{
if(i==0 || i==55)
printf("*");
printf(" ");
}
}
printf("\n");
}
for(i=0; i<10; i++)
{
printf(" ");
}
for(i=0; i<57; i++)
{
printf("*");
}
printf("\n");
printf("  \t请输入指令:\n");
return;

}

int Find_Element(Node *h, LinkData d, int *x)

{
if(h == NULL)
return FALSE;
int count = 0;
Node *tmp = h->next;
while(tmp)
{
count++;
if(strcmp(tmp->data.name ,d.name) == 0)
break;
tmp = tmp->next;
}
if(tmp == NULL)
{
printf("没有该好友\n");
return FALSE;
}
else
{
*x = count;
return TRUE;
}

}

Node *Find_Element2(Node *h, LinkData d, int *x)

{
if(h == NULL)
return NULL;
int count = 0;
Node *tmp = h->next;
while(tmp)
{
count++;
if(strcmp(tmp->data.name ,d.name) == 0)
break;
tmp = tmp->next;
}
if(tmp == NULL)
{
printf("没有该好友\n");
return NULL;
}
else
{
*x = count;
return tmp;
}

}

int Find_nElement(Node *h, LinkData d)

{
if(h == NULL)
return FALSE;
int count = 0;
Node *tmp = h->next;
while(tmp)
{
if(strcmp(tmp->data.name, d.name) == 0)
count++;
tmp = tmp->next;
}
return count;

}

int Delete_Pos(Node *h, int pos)

{
if(h == NULL || pos < 1)
return FALSE;
Node *tmp = h;
int i;
for(i=0; i<pos-1; i++)
{
if(tmp->next == NULL)
break;
tmp = tmp->next;
}
if(tmp->next == NULL)
{
printf("删除越界\n");
return FALSE;
}
Node *p = tmp->next;
tmp->next = p->next;
free(p);
return TRUE;

}

int Delete_Id(Node *h, int d)

{
if(h == NULL)
return FALSE;

Node *tmp = h;
while(tmp->next)
{
if(tmp->next->data.id == d)
break;
tmp = tmp->next;
}
if(tmp->next == NULL)
{
printf("没有该好友\n");
return FALSE;
}
else
{
Node *p = tmp->next;
tmp->next = p->next;
free(p);
return TRUE;
}

}

void Display_Pos(Node *h, int x)

{
if(h == NULL)
return;
int i;
Node *tmp = h->next;
for(i=0; i<x-1; i++)
{
tmp = tmp->next;
}
printf("\tid: %d                     姓名:%s\n",tmp->data.id,tmp->data.name);
printf("\t个人电话:%s     家庭住址:%s\n",tmp->data.tel,tmp->data.address);
printf("\t公司电话:%s\n",tmp->data.ctel);
printf("\n");

}

void Add(Node *h)

{
printf("\t请输入好友信息:\n");
LinkData data;
printf("\t请输入id:\n");
scanf("%d",&data.id);
printf("\t请输入姓名:\n");
scanf("%s",data.name);
printf("\t请输入电话号码:\n");
scanf("%s",data.tel);
printf("\t请输入家庭地址:\n");
scanf("%s",data.address);
printf("\t请输入公司号码:\n");
scanf("%s",data.ctel);
if(Insert_Last(h,data) == TRUE)
printf("\t添加成功\n");
else
printf("\t添加失败\n");
return;

}

void Search(Node *h)

{
printf("\t请输入要查找好友的姓名:\n");
LinkData d1;
int a1 = -3;
scanf("%s",d1.name);
if(Find_Element(h, d1, &a1) == TRUE)
{
//Display_Pos(h,a1);
if(Find_nElement(h, d1) == 1)
{
Display_Pos(h,a1);
}
else
{
int j;
int n = Find_nElement(h, d1);

Node *p = h;
for(j=0; j<n; j++)
{
Node *q=Find_Element2(p, d1, &a1);
Display_Pos(p, a1);
p = q;
}
}
}
return;

}

void Delete(Node *h)

{
printf("\t请输入要删除的好友姓名:\n");
LinkData d2;
int a2 = -2;
scanf("%s",d2.name);
if(Find_Element(h, d2, &a2) == TRUE)
{
if(Find_nElement(h, d2) == 1)
{
Find_Element(h, d2, &a2);
Delete_Pos(h,a2);
printf("删除成功\n");
}
else
{
int j,id;
int n = Find_nElement(h, d2);

Node *p = h;
for(j=0; j<n; j++)
{
Node *q=Find_Element2(p, d2, &a2);
Display_Pos(p, a2);
p = q;
}
printf("\t请输入id: \n");
scanf("%d",&id);
if(Delete_Id(h,id) == TRUE)
printf("\t删除成功\n");
}
}
return;

}

int Get_Len(Node *h)

{
if(h == NULL)
return FALSE;
int count = 0;
Node *tmp = h;
while(tmp->next)
{
count++;
tmp = tmp->next;
}
return count;

}

int Quit()

{
return FALSE;

}

#include "LinkList.h"

#include <stdio.h>

int main()

{
Node *head = Create_List();
if(head == NULL)
{
printf("\t创建链表失败\n");
return -1;
}

Init(head);

MainInterface();

while(1)
{
int x;
scanf("%d",&x);
if(Fun(x,head) == FALSE)
break;
//Display_List(head);
}
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息