简易版通讯录

来源:互联网 发布:java迷宫小游戏设计 编辑:程序博客网 时间:2024/05/24 05:57
#include "LinkList.h"#include <stdlib.h>#include <stdio.h>Node * Create_List()//建表{Node *list = (Node*)malloc(sizeof(Node)/sizeof(char));if (list == NULL)return NULL;list->next = NULL;   // 空表return list;}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(Node* h){    Node* node=Create_List();if (node == NULL)return FALSE;system("clear");printf("Please enter your buddy id:\n");    scanf("%d",&node->data.id);system("clear");printf("Please provide your buddy name:\n");scanf("%s",node->data.name);system("clear");printf("Please enter your buddy phone number:\n");scanf("%d",&node->data.pnum);system("clear");printf("Please provide your buddy address:\n");scanf("%s",node->data.addr);system("clear");printf("Please provide your buddy company phone:\n");scanf("%d",&node->data.cnum);system("clear");Insert_Head(h, node->data); free(node);return TRUE;}int Rank_List(Node* h)//按照id对朋友进行排序{if (h == NULL || h->next == NULL || h->next->next == NULL)return FALSE;Node* tmp = h->next;int i;int a=0;while(tmp){a++;tmp=tmp->next;}while(a-1){tmp=h->next;for(i=0;i<a-1;i++){if((tmp->data.id) > (tmp->next->data.id)){Inf ch=tmp->data;tmp->data=tmp->next->data;tmp->next->data=ch;}tmp = tmp->next;}a--;}return TRUE;}void Display(Node *h)//输出朋友的信息{if (h == NULL)return;int count = 0;Node *tmp = h->next;while (tmp){printf ("%8d", tmp->data.id);printf ("%8s", tmp->data.name);printf ("%8d", tmp->data.pnum);printf ("%8s", tmp->data.addr);printf ("%8d", tmp->data.cnum);printf ("\n");tmp = tmp->next;}printf ("\n");}void Puts(Node* h)//单个输出{if (h == NULL)return;Node *tmp = h;printf ("%8d", tmp->data.id);printf ("%8s", tmp->data.name);printf ("%8d", tmp->data.pnum);printf ("%8s", tmp->data.addr);printf ("%8d", tmp->data.cnum);printf ("\n");}int Find_Element(Node* h, char* ch)//寻找这个朋友有否{if (h == NULL)return FALSE;Node *tmp = h->next;int count = 0;while (tmp){if (strcmp(tmp->data.name,ch)==0){char *p="ID";printf ("%8s", p);p="NAME";printf ("%8s", p);p="PNUM";printf ("%8s", p);p="ADDR";printf ("%8s", p);p="CNUM";printf ("%8s", p);printf ("\n");break;}}while (tmp){if (strcmp(tmp->data.name,ch)==0){count++;Puts(tmp);}tmp = tmp->next;}if(count==0)return FALSE;return TRUE;}int Delete_Pos(Node* h, int pos)//插入到固定节点{if (h == NULL || pos < 1)return FALSE;// 找要插入位置的前一个结点Node *tmp = h;int i;for (i = 0; i < pos-1; i++){if (tmp->next == NULL)break;tmp = tmp->next;}Node *p = tmp->next;tmp->next = p->next;free(p);return TRUE;}int Delete_Data(Node* h, char* ch)//删除数据{if (h == NULL)return FALSE;Node *tmp = h;Node* node;int count=0;while (tmp->next){if (strcmp(tmp->next->data.name,ch)==0){node = tmp;count++;}tmp = tmp->next;}if (count==0){printf("Sorry , didn't find the good friend.\n");node = NULL;tmp  = NULL;free(node);free(tmp);return FALSE;}else if (count==1){Node *p = node->next;node->next = p->next;node = NULL;tmp  = NULL;free(node);free(tmp);free(p);return TRUE;}else{Find_Element(h,ch);int a;printf("Please enter the id you want to remove buddy.\n");scanf("%d",&a);Node *tmp = h->next;int b = 1;while (tmp){if ( tmp->data.id == a){break;}b++;tmp = tmp->next;}node = NULL;tmp  = NULL;free(node);free(tmp);free(tmp);Delete_Pos(h,b);return TRUE;}}

#include <stdio.h>#include <stdlib.h>#include <string.h>#include "LinkList.h"#include "AddList.h"void interface(char* str)//界面{char ch[100];system("clear");Node* head = Create_List();if (head == NULL){printf("创建链表失败\n");return ;}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("*              5、退出                           *\n");printf("*                                                *\n");printf("**************************************************\n");printf("\n");printf("Please enter your choice\n");scanf("%s",ch);strcpy(str,ch);}void Add_Friends(Node *h)//添加朋友{int a = Insert(h);if(a==FALSE){printf("Add buddy failure.\n");}else{Rank_List(h);printf("Add buddy successful.\n");}}void List_Friends(Node* h)//列表好友信息{system("clear");char *p="ID";printf ("%8s", p);p="NAME";printf ("%8s", p);p="PNUM";printf ("%8s", p);p="ADDR";printf ("%8s", p);p="CNUM";printf ("%8s", p);printf ("\n");Display(h);if(h->next==NULL)printf("There is no data here\n");}void Search_Friend(Node* h,char* ch)//查找朋友{system("clear");int a = Find_Element(h, ch);if(a==FALSE){printf("I'm sorry, did not find your friend's information.\n");return;} printf ("\n");}void Delete_Friend(Node* h,char* ch)//删除朋友的信息                                     {system("clear");int a = Delete_Data(h,ch);if(a==TRUE)printf("Successfully delete.\n");}

#include <stdio.h>#include <stdlib.h>#include <string.h>#include "LinkList.h"#include "AddList.h"int main(){char ch[100]={'0'};Node* head = Create_List();if (head == NULL){printf("创建链表失败\n");return -1;}while(1){char s;interface(ch);if(strcmp(ch,"INSERT")==0||strcmp(ch,"1")==0){Add_Friends(head);printf("Enter the enter to continue\n");getchar();getchar();}else if(strcmp(ch,"DISPLAY")==0||strcmp(ch,"2")==0){char *p="ID";printf ("%8s", p);p="NAME";printf ("%8s", p);p="PNUM";printf ("%8s", p);p="ADDR";printf ("%8s", p);p="CNUM";printf ("%8s", p);printf ("\n");List_Friends(head);printf("Enter the enter to continue\n");getchar();getchar();}else if(strcmp(ch,"SEARCH")==0||strcmp(ch,"3")==0){char s[100];system("clear");printf("Please enter your friend's name\n");scanf("%s",s);Search_Friend(head,s);printf("Enter the enter to continue\n");getchar();getchar();}else if(strcmp(ch,"DELETE")==0||strcmp(ch,"4")==0){char s[100];system("clear");printf("Please enter your friend's name\n");scanf("%s",s);Delete_Friend(head,s);printf("Enter the enter to continue\n");getchar();getchar();}else if(strcmp(ch,"EXIT")==0||strcmp(ch,"5")==0){system("clear");exit(0);}else{system("clear");printf("Input error\n");printf("Enter the enter to continue\n");getchar();getchar();}}return 0;}
电子通讯录的总体框架就是运用链表,链表的指针域指向下一个节点,而数据域用来存储你要存储的那些数据。里面涉及到的基本都是链表的使用,从链表的创建,链表的元素添加,链表的查找,链表的删除等等。熟练掌握这些,对于通讯录的界面就很简单,主函数使用了一个死循环,让这个通讯录不停的跑下去。