#include <stdio.h>
#include <stdlib.h>
#include <string.h>#define N 15struct contact// 瀹氫箟缁撴瀯浣?{char name[N];//濮撳悕char sex[N];// 鎬у埆char number[N];//鍙风爜char QQ[N];// qqchar addr[2*N];// 鍦板潃struct contact *next;
};typedef struct contact Contact;
typedef struct contact *Link;void creat_list(Link *head)//鍒涘缓閾捐〃
{*head = (Link)malloc(sizeof(Contact));if(!(*head)){printf("malloc error!n");exit(-1);}(*head)->next = NULL;
}int len_list(Link *head)//璁$畻閾捐〃闀垮害
{Link temp = (*head)->next;int len = 0;int i;while(temp != NULL){temp = temp->next;len++;}return len;
}void input(Link p)//杈撳叆鑺傜偣鍊?{printf("n璇疯緭鍏ヨ仈绯讳汉濮撳悕n");scanf("%s",p->name);printf("璇疯緭鍏ヨ仈绯讳汉鎬у埆n");scanf("%s",p->sex);printf("璇疯緭鍏ヨ仈绯讳汉鐢佃瘽n");scanf("%s",p->number);printf("璇疯緭鍏ヨ仈绯讳汉QQn");scanf("%s",p->QQ);printf("璇疯緭鍏ヨ仈绯讳汉鍦板潃n");scanf("%s",p->addr);
}void output(Link p)//杈撳嚭鑺傜偣鍊?{ printf("濮撳悕: %s",p->name);printf("n");printf("鎬у埆: %s",p->sex);printf("n");printf("鐢佃瘽: %s",p->number);printf("n");printf("QQ鍙? %s",p->QQ);printf("n");printf("鍦板潃: %s",p->addr);printf("n");
}void insert_tail(Link *head)//灏炬彃娉曞缓绔嬮摼琛?{Link temp = *head;Link p;int n;int i;int m;printf("input the number you want to add!n");scanf("%d",&n);for(i = 0; i < n; i++){p = (Link)malloc(sizeof(Contact));if(!p){printf("malloc error!n");exit(-1);}input(p);while(temp->next != NULL){temp = temp->next;}p->next = NULL;temp->next = p;}}void display(Link head)//閬嶅巻閾捐〃
{Link temp = head->next;if(head->next == NULL){printf("contact error!n");}while(temp != NULL){printf("濮撳悕: %s",temp->name);printf("n");printf("鎬у埆: %s",temp->sex);printf("n");printf("鐢佃瘽: %s",temp->number);printf("n");printf("QQ鍙? %s",temp->QQ);printf("n");printf("鍦板潃: %s",temp->addr);printf("n");temp = temp->next;}
}void find_name(Link head)//鎸夊鍚嶆煡鎵?{Link temp = head->next;char str[N];int i = 0;printf("input the name you want to search!n");scanf("%s",str);if(head->next == NULL){printf("contact empty!n");}while(temp != NULL){if(strcmp(temp->name,str) == 0){output(temp);i++;}temp = temp->next;}if(i == 0){printf("no such persion!n");}
}void find_number(Link head)//鎸夊彿鐮佹煡鎵?{Link temp = head->next;char str[N];int i = 0;printf("input the number you want to search!n");scanf("%s",str);if(head->next == NULL){printf("contact empty!n");}while(temp != NULL){if(strcmp(temp->number,str) == 0){output(temp);i++;}temp = temp->next;}if(i == 0){printf("no such persion!n");}
}void find_qq(Link head)//鎸塹q鍙锋煡鎵?{Link temp = head->next;char str[N];int i = 0;printf("input the QQ you want to search!n");scanf("%s",str);if(head->next == NULL){printf("contact empty!n");}while(temp != NULL){if(strcmp(temp->QQ,str) == 0){output(temp);i++;}temp = temp->next;}if(i == 0){printf("no such persion!n");}
}void find_addr(Link head)//鎸夊湴鍧€鏌ユ壘
{Link temp = head->next;char str[2*N];int i = 0;printf("input the addr you want to search!n");scanf("%s",str);if(head->next == NULL){printf("contact empty!n");}while(temp != NULL){if(strcmp(temp->addr,str) == 0){output(temp);i++;}temp = temp->next;}if(i == 0){printf("no such persion!n");}
}void search_node(Link head)//鏌ユ壘鍑芥暟
{//Link temp = head->next;int n;char pos;printf("n1:鎸夊鍚嶆煡鎵綷n2:鎸夌數璇濇煡鎵綷n3:鎸塓Q鍙锋煡鎵綷n4:鎸夊湴鍧€鏌ユ壘n");printf("璇疯緭鍏ヤ綘鐨勯€夋嫨!n");scanf("%d",&n);switch(n){case 1:find_name(head);while(1){printf("鏄惁缁х画鏌ユ壘[Y/N]?");scanf("%c",pos);if(pos == 'y' || pos == 'Y'){find_name(head);}else{break;}}break;case 2:find_number(head);while(1){printf("鏄惁缁х画鏌ユ壘[Y/N]?");scanf("%c",pos);if(pos == 'y' || pos == 'Y'){find_number(head);}else{break;}}break;case 3:find_qq(head);while(1){printf("鏄惁缁х画鏌ユ壘[Y/N]?");scanf("%c",pos);if(pos == 'y' || pos == 'Y'){find_qq(head);}else{break;}}break;case 4:find_addr(head);while(1){printf("鏄惁缁х画鏌ユ壘[Y/N]?");scanf("%c",pos);if(pos == 'y' || pos == 'Y'){find_addr(head);}else{break;}}break;default:break;}
}void delete_node(Link *head)//鍒犻櫎鍑芥暟
{Link temp = (*head)->next;Link p = *head;char str[N];char str1[N];int i = 0;printf("input the name you want to delete!n");scanf("%s",str);while(temp != NULL){if(strcmp(temp->name,str) == 0){output(temp);i++;}temp = temp->next;}temp = (*head)->next;if(i == 0){printf("no such persion!n");}else if(i == 1){while((strcmp(temp->name,str) != 0) && temp->next != NULL){p = temp;temp = temp->next;}if(strcmp(temp->name,str) == 0){p->next = temp->next;free(temp);}}else{printf("input the number you want to delete!n");scanf("%s",str1);while((strcmp(temp->number,str1) != 0) && temp->next != NULL){p = temp;temp = temp->next;}if(strcmp(temp->number,str1) == 0){p->next = temp->next;free(temp);}}// len_list(head);}void alter_name(Link p)//淇敼濮撳悕
{printf("璇疯緭鍏ヤ綘鎯虫敼鎴愮殑鍚嶅瓧!n");scanf("%s",p->name);output(p);
}void alter_sex(Link p)// 淇敼鎬у埆
{printf("璇疯緭鍏ヤ綘鎯虫敼鎴愮殑鎬у埆!n");scanf("%s",p->sex);output(p);
}void alter_number(Link p)//淇敼鍙风爜
{printf("璇疯緭鍏ヤ綘鎯虫敼鎴愮殑鍙风爜!n");scanf("%s",p->number);output(p);
}
void alter_qq(Link p)// 淇敼qq
{printf("璇疯緭鍏ヤ綘鎯虫敼鎴愮殑QQ!n");scanf("%s",p->QQ);output(p);
}void alter_addr(Link p)// 淇敼鍦板潃
{printf("璇疯緭鍏ヤ綘鎯虫敼鎴愮殑鍦板潃!n");scanf("%s",p->addr);output(p);
}void atler(Link head)// 淇敼鍑芥暟
{int n;char str[2*N];Link temp = head->next;printf("input the name you want to atler!n");scanf("%s",str);while((strcmp(temp->name,str) != 0) && temp->next != NULL){temp = temp->next;}if(strcmp(temp->name,str) == 0){output(temp);printf("n1:淇敼濮撳悕n2:淇敼鎬у埆n3:淇敼鍙风爜n4:淇敼QQ鍙穃n5:淇敼鍦板潃n");printf("input your choic!n");scanf("%d",&n);switch(n){case 1:alter_name(temp);break;case 2:alter_sex(temp);break;case 3:alter_number(temp);break;case 4:alter_qq(temp);break;case 5:alter_addr(temp);break;default:printf("input error!n");break;}}}void clear(Link *head)// 娓呯┖閾捐〃
{Link temp = (*head)->next;while(temp != NULL){(*head)->next = (temp->next);free(temp);temp = (*head)->next;}(*head)->next = NULL;
}int main()
{Link head;int n;char pos[5];creat_list(&head);system("clear");while(1){printf("n1:鍒涘缓閫氳褰昞n2閬嶅巻閫氳褰?n3:鏌ユ壘鑱旂郴浜篭n4:鍒犻櫎鑱旂郴浜篭n5:娣诲姞鑱旂郴浜篭n6:淇敼鑱旂郴浜篭n0:閫€鍑篭n");printf("璇疯緭鍏ヤ綘鐨勯€夋嫨:n");scanf("%d",&n);switch(n){case 1:insert_tail(&head);break;case 2:display(head);break;case 3:search_node(head);break;case 4:delete_node(&head);while(1){printf("鏄惁缁х画鍒犻櫎[Y/N]?");scanf("%s",pos);if(strcmp(pos,"y")){delete_node(&head);}else{break;}}break;case 5:insert_tail(&head);while(1){printf("鏄惁缁х画娣诲姞[Y/N]?");scanf("%s",pos);if(strcmp(pos,"y") == 0){insert_tail(&head);}else{break;}}break;case 6:atler(head);while(1){printf("鏄惁缁х画淇敼[Y/N]?");scanf("%s",pos);if(strcmp(pos,"y") == 0){atler(head);}else{break;}}break;case 7:clear(&head);break;case 0:exit(0);break;default:printf("input error!n");break;}}return 0;
}
本文发布于:2024-02-03 05:39:28,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170690996849008.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |