用連結串列實現通訊錄程式設計
阿新 • • 發佈:2019-02-08
#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;
}
#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;
}