用链表实现通讯录编程

来源:互联网 发布:颂党恩 知党史 促成长 编辑:程序博客网 时间:2024/06/06 02:46
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


struct node             //定义链表


{
char name[20];
char phone_number[20];
char home_address[20];
char com_number[20];
char id;
struct node *prior;
struct node *next;
};


typedef struct node Node;     //重命名
typedef Node *Dlink;


void init_dlink(Dlink* head)    //建立双向链表
{
*head = (Dlink)(malloc)(sizeof(Node));
(*head)->prior = *head;
(*head)->next = *head;
}


void insert_head_node(Dlink newnode,Dlink head)    //插入头指针
{
newnode->next = head->next;
head->next->prior = newnode;
newnode->prior = head;
head->next = newnode;
}
void insert_node(Dlink newnode,Dlink p)     //插入新节点
{
newnode->next = p->next;
p->next->prior = newnode;
newnode->prior = p;
p->next = newnode;
}


int del_node(char *name,Dlink head)     //删除节点
{
Dlink p = head->next;
while(p != head)
{
if(strcmp(p->name,name) == 0)    //验证符合所要删除的节点
{
p->prior->next = p->next;
p->next->prior = p->prior;
free(p);
p = NULL;
return  0;
}
p = p->next;
}
return -1;
}


int del_node_id(int id,Dlink head)
{
Dlink p = head->next;
while(p != head)
{
if(id == p->id);
{
p->prior->next = p->next;
p->next->prior = p->prior;
free(p);
p = NULL;
return  0;
}
p = p->next;
}
return -1;
}
void id_valuation(Dlink head)
{
int i = 0;
Dlink p = head->next;
while(p != head)
{
p->id = ++i;
p = p->next;
}
}




void select_people(char *dest,Dlink head)     //搜索匹配好友
{
int flag = 0;
Dlink p = head->next;
while(p != head)
{
if(strcmp(p->name,dest) == 0)
{
flag = 1;
printf("ID:       %d\n",p->id);
printf("姓名:     %s\n",p->name);
printf("手机号码: %s\n",p->phone_number);
printf("家庭住址: %s\n",p->home_address);
printf("公司电话: %s\n\n\n",p->com_number);
}
p = p->next;
}
if(0 == flag)
{
printf("\n无此姓名好友!!");
}
}


void my_select(Dlink head)      //自己定义的查找好友
{
system("clear");
char dest[20];
printf("输入要查找人姓名: ");
scanf("%s",dest);
select_people(dest,head);
printf("\n");
}


void add(Dlink head)                       //添加好友信息的操作
{
void again_add(Dlink head);
system("clear");
Dlink p;
Dlink q = head;
p = (Dlink)malloc(sizeof(Node));        //分配空间
printf("好友姓名: ");
scanf("%s",p->name);
printf("手机号码: ");
scanf("%s",p->phone_number);
printf("家庭住址: ");
scanf("%s",p->home_address);
printf("公司电话: ");
scanf("%s",p->com_number);
if(q->next == head)
{
insert_head_node(p,head);          
id_valuation(head);
printf("添加成功!\n");
again_add(head);
return ;
}
q = q->next;
if(q->prior == head && strcmp(p->name,q->name) < 0)
{
insert_node(p,q->prior);
id_valuation(head);
printf("添加成功!\n");
again_add(head);
return;
}
q = q->next;
while(q != head)
{
if(strcmp(p->name,q->prior->name) > 0 && strcmp(p->name,q->name) <= 0)
{
insert_node(p,q->prior);
id_valuation(head);
printf("添加成功!\n");
again_add(head);
return ;
}
q = q->next;
}
insert_node(p,q->prior);
id_valuation(head);
printf("添加成功!\n");
again_add(head);
return ;

}


void again_add(Dlink head)             //继续添加的函数
{
char symbol;
getchar();
printf("\n是否继续添加好友? Y/N\n");
scanf("%c",&symbol);
if(symbol == 'y')
{
add(head);
}
return ;
}


void del(Dlink head)
{
int id;
char dest[20];
int i = 0;
Dlink p = head->next;
printf("输入要删除人的名字:\n");
scanf("%s",dest);
while(p != head)
{
if(strcmp(p->name,dest) == 0)     //删除匹配好友信息
{
i++;
}
p = p->next;
}
if(0 == i)
{
printf("无此好友!");
return;
}
if(1 == i)
{
del_node(dest,head);
}
else
{
system("pause");
printf("你要删除的人有以下几人:\n");
select_people(dest,head);
printf("输入你要删除人的ID:");
scanf("%d",&id);
del_node_id(id,head);
}
printf("删除成功!");
id_valuation(head);
}


void display(Dlink head)    //输出好友信息
{
system("clear");
Dlink p = head->next;
while(p !=head)
{
printf("ID:       %d\n",p->id);
printf("姓名:     %s\n",p->name);
printf("手机号码: %s\n",p->phone_number);
printf("家庭住址: %s\n",p->home_address);
printf("公司电话: %s\n\n",p->com_number);
p = p->next;
}
}


void mod(Dlink head)
{
char dest[20];
Dlink p = head->next;
int id;
int flag = 0;


printf("输入要修改信息的好友名字\n");
scanf("%s",dest);
while(p != head)
{
if(strcmp(p->name,dest) == 0)
{
flag = 1;
printf("ID:       %d\n",p->id);
printf("姓名:     %s\n",p->name);
printf("手机号码: %s\n",p->phone_number);
printf("家庭住址: %s\n",p->home_address);
printf("公司电话: %s\n\n\n",p->com_number);
}
p = p->next;
}
if(0 == flag)
{
printf("\n无此姓名好友!!\n");
return ;
}
p = head->next;
printf("输入要修改好友的ID:");
scanf("%d",&id);
while(p != head)
{
if(id == p->id)              //修改好友信息,重新给指针定向
{
printf("\n请做修改:\n");
printf("手机号码: ");
scanf("%s",p->phone_number);
printf("家庭住址: ");
scanf("%s",p->home_address);
printf("公司电话: ");
scanf("%s",p->com_number);
return ;
}
p = p->next;
}
}


void menu(Dlink head)         //主目录界面
{
system("clear");
int order;
printf("\t\t\t***************************************************\n");
printf("\t\t\t|                                                 |\n");
printf("\t\t\t|                  电子通讯录                      |\n");
printf("\t\t\t***************************************************\n");
printf("\t\t\t|                                                 |\n");
printf("\t\t\t|-------------------------------------------------|\n");
printf("\t\t\t|      1:添加朋友信息        2:查看朋友信息        |\n");
printf("\t\t\t|-------------------------------------------------|\n");
printf("\t\t\t|      3:搜索查找朋友        4:删除朋友信息       |\n");
printf("\t\t\t|-------------------------------------------------|\n");
printf("\t\t\t|      5:修改还有信息        6:退出通讯录          |\n");
printf("\t\t\t***************************************************\n");
printf("\n\t\t\t            请输入需要的操作(1-5): ");




scanf("%d",&order);
switch(order)
{
case 1:;
{
add(head);
getchar();
menu(head);
break;
}
case 2:
{
display(head);
getchar();
getchar();
menu(head);
break;
}
case 3:
{
my_select(head);
getchar();
getchar();
menu(head);
break;
}
case 4:
{
del(head);
getchar();
getchar();
menu(head);
break;
}
case 5:
{
mod(head);
getchar();
getchar();
menu(head);
menu(head);
break;
}
case 6:
{
exit(1);
break;
}
default:
{
getchar();
printf("\n\t\t\t\t!!此序号无对应功能,按任意键回到主菜单!!\n");
getchar();
getchar();
getchar();
menu(head);
}
}
}






int main()
{
Dlink head;
init_dlink(&head);
menu(head);
return 0;
}
0 0