简易通讯录

1、addressbook.h部分

#ifndef _ADDRESSBOOK_H__
#define _ADDRESSBOOK_H__

#define  FALSE  0
#define  TRUE   1

typedef struct _message
{
int id;
int tel;
int com_tel;
char name[50];
char address[50];
}Data;

typedef struct _node
{
Data data;
struct _node *next;
}Node;

// 创建链表
Node* Create_List();

// 主界面
void interface();

// 尾插
int Insert_Last(Node* h,Data N);

// 搜索好友
int Find_Element(Node *h);

// 显示好友信息
int Display (Node *h);

// 删除指定好友
int Delete_Data(Node *h);

#endif


2、addressbook.c   部分

#include 
#include "addressbook.h"
#include 

int count = 1;

Node *Create_List()
{
Node *list = (Node*)malloc(sizeof(Node)/sizeof(char));
if (list == NULL)
return NULL;

list->next = NULL;  // 空表

return list;
}

int information(Node *h)
{
if (h == NULL)
{
return FALSE;
}
Node* node = Create_List();
system ("clear");
node->data.id = count++;
printf ("请输入您想添加的名字(英文)");
scanf ("%s",node->data.name);
printf ("请输入您想添加的电话:");
scanf ("%d",&node->data.tel);
printf ("请输入您想添加的地址");
scanf ("%s",node->data.address);
printf ("请输入您想添加的公司电话:");
scanf ("%d",&node->data.com_tel);

Insert_Last(h,node->data

return TRUE;
}

int Insert_Last(Node *h, Data 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;
}

int Display(Node *h)
{
if (h == NULL || h->next == NULL)
return FALSE;

system("clear");
while(h->next)
{
printf("姓名:%s\n",h->next->data.name);
printf("id:%d\n",h->next->data.id);
printf("电话:%d\n",h->next->data.tel);
printf("地址:%s\n",h->next->data.address);
printf("公司电话:%d\n",h->next->data.com_tel);
h = h->next;
}
return TRUE;
}

int Find_Element(Node *h)
{
if (h == NULL)
return FALSE;
int count = 0;
char name[30];
printf ("请输入您想找的人的名字:");
scanf ("%s",name);
Node *tmp = h->next;

while (tmp)
{
if (strcmp(tmp->data.name,name)== 0)
{
printf("姓名:%s\n",tmp->data.name);
printf("id:%d\n",tmp->data.id);
printf("电话:%d\n",tmp->data.tel);
printf("地址:%s\n",tmp->data.address);
printf("公司电话:%d\n",tmp->data.com_tel);
}
tmp = tmp->next;
count++;
}
if(count)
return TRUE;
else
return FALSE;
}

int Delete_Data(Node *h)
{
if (h == NULL || h->next == NULL)
return FALSE;

int count1 = 0;
char name[30];
Node *tmp = h;   //找前一个
printf ("请输入您想删除的名字:\n");
scanf ("%s",name);

while (tmp)
{
if (strcmp(tmp->data.name,name) == 0)
{
printf("姓名:%s\n",tmp->data.name);
printf("id:%d\n",tmp->data.id);
printf("电话:%d\n",tmp->data.tel);
printf("地址:%s\n",tmp->data.address);
printf("公司电话:%d\n",tmp->data.com_tel);
count1++;
}
tmp = tmp->next;
}
Node *pre = h;
if (count1 == 0)
return FALSE;
if (count1 == 1)
{
while(pre->next)
{
if (strcmp(pre->next->data.name,name) == 0)
{
Node *p = pre->next;
pre->next = p->next;
free(p);
return TRUE;
}
pre = pre->next;
}

}
Node *cur = h; 
if (count1 > 1)
{
int  id;
printf ("请输入id号:");
scanf ("%d",&id);
while(cur->next)
{
if (cur->next->data.id == id)
break;
cur = cur->next;
}
Node *p = cur->next;
cur->next = p->next;
free(p);
}
return TRUE;
}


void interface()
{
system("clear");
printf("\t*******************************************\n");
printf("\t*                                                                     *\n");
printf("\t*   1.添加好友          2.列表好友信息         *\n");
printf("\t*                                                                     *\n");
printf("\t*******************************************\n");
printf("\t*                                                                    *\n");
printf("\t*   3.搜索好友          4.删除好友                 *\n");
printf("\t*                                                                    *\n");
printf("\t*             0.退出程序                                    *\n");
printf("\t*******************************************\n");


}



3、主函数部分

#include 
#include "addressbook.h"
#include 




int main ()
{
int x;
Node *head = Create_List();

while(1)
{ 
interface();
printf("请输入数字0~4 :");
scanf ("%d",&x);

switch (x)
{
case 1:
if (information (head))
{printf("信息输入成功\n");

}
else
printf("信息输入失败\n");
break;

case 2:
Display(head);
break;

case 3:
system("clear");
if (!Find_Element(head))
{
printf("未搜索到目标\n");
}
break;
case 4:
system("clear");
if (Delete_Data(head))
{ 
printf ("删除成功\n");
}
else
{
printf("未找到指定好友\n");
}
break;
case 0:
exit(0);
break;
}
printf ("请随便输入一个字符返回主界面\n");
char ch;
scanf ("%c",&ch);
scanf ("%c",&ch);
}
return 0;


}

简易通讯录_第1张图片 简易通讯录_第2张图片 简易通讯录_第3张图片 简易通讯录_第4张图片 简易通讯录_第5张图片



你可能感兴趣的:(简易通讯录)