记得好久之前写的,今天一个学弟问我,我才想起来,主要是依靠链表完成的,细节方面做的还需要改进,
主题架构就是这样了
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
typedef struct _add
{
int ID;
char Name[20];
char Address[30];
char Company_Phone[30];
char Mobile_Phone[30];
}ADD;
typedef ADD LinkData;
typedef struct _stu
{
LinkData data;
struct _stu *next;
}Node;
Node * Create_List()
{
Node *list = (Node*)malloc(sizeof(Node)/sizeof(char));
if (list == NULL)
return NULL;
list->next = NULL; // 空表
return list;
}
int Add_Friend(Node* h)
{
Node* head = Create_List();
printf ("\t请输入好友的ID:");
scanf ("%d", &head->data.ID);
printf ("\n");
printf ("\t请输入好友的名字:");
scanf ("%s", head->data.Name);
printf ("\n");
printf ("\t请输入好友的手机号:");
scanf ("%s", head->data.Mobile_Phone);
printf ("\n");
printf ("\t请输入好友的家庭住址:");
scanf ("%s", head->data.Address);
printf ("\n");
printf ("\t请输入好友的公司电话:");
scanf ("%s", head->data.Company_Phone);
printf ("\n");
Insert_Last(h, head->data);
free(head);
}
int Display()
{
printf ("\t*****************************************\n");
printf ("\t~ 欢迎使用通讯录 ~\n");
printf ("\t~ ~\n");
printf ("\t~ 1 >>>>>>>>> 添加好友信息 ~\n");
printf ("\t~ 2 >>>>>>>>> 列表好友信息 ~\n");
printf ("\t~ 3 >>>>>>>>> 搜索好友 ~\n");
printf ("\t~ 4 >>>>>>>>> 删除好友 ~\n");
printf ("\t~ 5 >>>>>>>>> 退出 ~\n");
printf ("\t~ ~\n");
printf ("\t~ ~\n");
printf ("\t~ 作者:李孟龙 ~\n");
printf ("\t*****************************************\n");
printf (" \n");
printf (" \n");
printf ("\t请输入对应数字选择相应功能:");
}
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;
}
int Friend_ifo(Node* h)
{
if (h == NULL )
return FALSE;
printf ("\tID\t姓名\t\t手机号\t\t住址\t\t\t公司电话\n");
Node *tmp = h -> next;
while(tmp)
{
printf ("\t%d\t%s\t\t%s\t\t%s\t\t\t%s\n",tmp->data.ID, tmp->data.Name,
tmp->data.Mobile_Phone, tmp->data.Address, tmp->data.Company_Phone);
printf("\n");
tmp = tmp -> next;
}
return TRUE;
}
int Search_Friend(Node* h, char Name[])
{
if (h == NULL)
return FALSE;
int count = 0;
Node *tmp = h->next;
printf ("\tID\t姓名\t\t手机号\t\t住址\t\t\t公司电话\n");
while (tmp)
{
if (strcmp(tmp->data.Name ,Name) == 0)
{
printf ("\t%d\t%s\t\t%s\t\t%s\t\t\t%s\n",tmp->data.ID, tmp->data.Name,
tmp->data.Mobile_Phone, tmp->data.Address, tmp->data.Company_Phone);
count = count++;
}
tmp = tmp->next;
}
if(count == 0)
{
printf("查无此人,谢谢使用\n");
}
return FALSE;
}
int Delete_Friend(Node* h, char Name[])
{
if (h == NULL)
return FALSE;
printf ("\tID\t姓名\t\t手机号\t\t住址\t\t\t公司电话\n");
Node * tmp = h;
int count = 0;
while (tmp->next)
{
if (strcmp(tmp->next->data.Name, Name) == 0)
{
printf ("\t%d\t%s\t\t%s\t\t%s\t\t\t%s\n",tmp->next->data.ID, tmp->next->data.Name,
tmp->next->data.Mobile_Phone, tmp->next->data.Address, tmp->next->data.Company_Phone);
count++;
// break;
}
tmp = tmp->next;
}
if(count == 0)
{
printf("查无此人,谢谢使用\n");
}
// if (tmp->next == NULL)
// return FALSE;
if(count == 1)
{
Node *p = tmp->next;
tmp->next = p->next;
free(p);
}
if(count >1)
{
printf("输入你要删除的ID:");
int a;
scanf("%d",&a);
Delete_Friend2(h,a);
}
return TRUE;
}
int Delete_Friend2(Node* h, int ID)
{
if (h == NULL)
return FALSE;
Node * tmp = h;
while (tmp->next)
{
if (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);
return TRUE;
}
int main()
{
system ("clear");
int i=0;
Node* head = Create_List();
if (head == NULL)
{
printf("创建链表失败\n");
return -1;
}
while(1)
{
char Name[20];
Display();
int j=0;
scanf("%d",&j);
switch (j)
{
case 1:
j = 0;
Add_Friend(head);
system ("clear");
break;
case 2:
j = 0;
system ("clear");
Friend_ifo(head);
break;
case 3:
j = 0;
system ("clear");
printf ("\t********************查找好友********************\n");
printf ("\t请输入你要查找好友的姓名: ");
scanf ("%s", Name);
printf ("\n");
Search_Friend(head, Name);
break;
case 4:
system ("clear");
j = 0;
printf ("\t********************删除好友********************\n");
printf ("\t请输入要删除好友的姓名:");
scanf ("%s", Name);
printf ("\n");
Delete_Friend(head,Name);
break;
case 5:
j = 0;
system ("clear");
exit(0);
break;
default:
system ("clear");
printf("输入有误,重新输入");
printf ("\n");
break;
}
}
return 0;
}