通訊錄(建在堆裏面)
阿新 • • 發佈:2017-08-02
ddr name eat sca -- troy div amp clas
#ifndef __AddressBook_H__ #define __AddressBook_H__ #define FALSE 0 #define TRUE 1 typedef struct data { int id; char name[20]; int num; char addr[20]; int cnum; }Data; typedef Data LinkData; typedef struct _node { LinkData data; struct _node * next; }Node; //管理主界面 voidAddressBook(Node* h); //數據插入結構體,再傳給鏈表; void Insert(Node *h); //排序 void rank(Node* h); //展示 void Display(Node* h); //查找 int Search(Node* h); //刪除好友 int Distroy(Node* h); int Get_Len(Node *h); // 創建鏈表 Node * Create_List(); // 頭插 int Insert_Head(Node *h, LinkData data); //尾插 int Insert_Last(Node *h, LinkData data);#endif ------------------------------------------------------ #include "AddressBook.h" #include <stdio.h> #include <stdlib.h> //主界面 void AddressBook(Node *h) { int a; //system("cls"); //system("color f4"); printf(" |--------------------------------------------------|\n"); printf(" | |\n"); printf(" |-----------------------AddressBook----------------|\n"); printf(" | |\n"); printf(" |==================================================|\n"); printf(" | |\n"); printf(" |---------------------1、添加好友列表--------------|\n"); printf(" | |\n"); printf(" |---------------------2、列表好友信息--------------|\n"); printf(" | |\n"); printf(" |---------------------3、搜索好友------------------|\n"); printf(" | |\n"); printf(" |---------------------4、刪除好友------------------|\n"); printf(" | |\n"); printf(" |---------------------0、退出操作------------------|\n"); printf(" | |\n"); printf(" |==================================================|\n"); while(1) { printf("請輸入需要的功能\n",a); scanf("%d",&a); switch(a) { case 0: break; case 1: Insert(h); Insert(h); break; case 2: Display(h); break; case 3: Search(h); break; case 4: Distroy(h); break; } int i; printf("輸入數字1退出,其它數字繼續操作:\n"); scanf("%d",&i); if(i == 1) break; } } //創建表; Node * Create_List() { Node *list = (Node*)malloc(sizeof(Node)/sizeof(char)); if (list == NULL) return NULL; list->next = NULL; // 空表 return list; } #if 0 void Insert(Node *h) { Node *node = Create_List(); printf("輸入ID:\n"); scanf("%d",&node->data.id); printf("輸入姓名:\n"); scanf("%s",node->data.name); printf("輸入號碼:\n"); scanf("%d\n",&node->data.num); printf("輸入地址:\n"); scanf("%s",node->data.addr); printf("輸入公司電話:\n"); scanf("%d",&node->data.cnum); Insert_Last(h, node->data); free(node); } #endif void Insert(Node *h) { Node * node = Create_List(); printf("請輸入ID:\n"); scanf("%d",&node->data.id); printf("請輸入姓名:\n"); scanf("%s",node->data.name); printf("請輸入手機號碼:\n"); scanf("%d",&node->data.num); printf("請輸入地址:\n"); scanf("%s",node->data.addr); // printf("輸入公司電話:\n"); // scanf("%d",node->data.cnum); //起初輸入三個之後崩潰,重新copy了一份?輸入五個會有段錯誤 Insert_Last(h, node->data); free(node); } //鏈表長度; int Get_Len(Node *h) { if(h == NULL) return 0; Node* tmp = h; int count = 0; while(tmp->next) { count++; tmp = tmp->next; } return count; } //排序 void rank(Node* h) { int res = Get_Len(h); Node* tmp = h->next; int i; while(res-1) { tmp=h->next; for(i = 0;i < res-1;i++) { if(tmp->data.id > tmp->next->data.id) { LinkData k = tmp->data; tmp->data = tmp->next->data; tmp->next->data = k; } tmp = tmp->next; } res--; } } //展示 void Display(Node* h) { if (h == NULL) return; rank(h); int count = 0; Node *tmp = h->next; while (tmp) { if (count++ % 4 == 0) printf ("\n"); printf ("%8d", tmp->data.id); printf ("%8s", tmp->data.name); printf ("%8d", tmp->data.num); printf ("%8s", tmp->data.addr); printf("\n"); tmp = tmp->next; } printf ("\n"); } #if 0 //查找 int Search(Node* h) { int pos; printf("輸入pos:\n"); scanf("%d",&pos); if(h == NULL||pos < 1) return FALSE; int i; Node* tmp = h; for(i = 0;i < pos;i++) { if(tmp == NULL) break; tmp = tmp->next; } if(tmp == NULL) return FALSE; else printf ("%8d", tmp->data.id); printf ("%8s", tmp->data.name); printf ("%8d", tmp->data.num); printf ("%8s", tmp->data.addr); printf("\n"); } #endif int Search(Node* h) { if(h == NULL) return FALSE; Node* tmp = h->next; int i; printf("輸入要查找的id號:"); scanf("%d",&i); while(tmp) { if(tmp->data.id == i) { printf ("%8d", tmp->data.id); printf ("%8s", tmp->data.name); printf ("%8d", tmp->data.num); printf ("%8s", tmp->data.addr); printf("\n"); break; } tmp = tmp->next; } } //刪除好友 int Distroy(Node* h) { char name[20]; printf("請輸入名字:"); scanf("%s",name); if(h == NULL) return 0; Node* tmp = h; int count = 0; while(tmp->next) { if(strcmp(tmp->next->data.name,name) == 0) { count++; printf ("%8d", tmp->data.id); printf ("%8s", tmp->data.name); printf ("%8d", tmp->data.num); printf ("%8s", tmp->data.addr); printf("\n"); } tmp = tmp->next; } if(count == 0) { printf(""); return FALSE; } else if(count == 1) { tmp = h; while(tmp->next) { if(strcmp(tmp->next->data.name,name) == 0) break; tmp = tmp->next; } if(tmp->next == NULL) return FALSE; //刪除操作 Node* p = tmp->next; tmp->next = p->next; free(p); p = NULL; } else { int id; printf("有同名,請輸入id刪除:"); scanf("%d",&id); tmp = h; while(tmp->next) { if(strcmp(tmp->next->data.name,name) == 0 && 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); p = NULL; } return TRUE; } int Insert_Head(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 = h->next; h->next = node; 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; //三句話 node->date=date,node->next=NULL,tmp_last->next=node; return TRUE; } -------------------------------------------------------------------- #include "AddressBook.h" #include <stdio.h> int main() { Node *head = Create_List();//head指向創建的空表; if(head == NULL) { printf("創建鏈表失敗\n"); return -1; } AddressBook(head); return 0; }
通訊錄(建在堆裏面)