通讯录删除(by id or name or num)

来源:互联网 发布:sqlite数据库加密 编辑:程序博客网 时间:2024/06/15 19:32
int delete_id(node head, int* n, int i){node t = NULL;while(head->next->data.id != i){head = head->next;}t = head->next->next;free(head->next);head->next = t;while(head->next != NULL){head = head->next;head->data.id--;}--*n;return T;}int delete_name(node head, int* n){char s[15];printf("who you want to delete?\n");scanf("%s", s);while(head->next != NULL){if(strcmp(head->next->data.name, s) == 0){delete_id(head, n, head->next->data.id);}else{head = head->next;}}return T;}int delete_num(node head, int* n){char s[15];printf("which num you want to delete?\n");scanf("%s", s);while(head->next != NULL){if(strcmp(head->next->data.num, s) == 0){delete_id(head, n, head->next->data.id);}else{head = head->next;}}return T;}
主函数:
int init(node*);//初始化int interface(void);//功能列表type insert_tail(node);//从尾部插入int insert_id(node, int*);//由id插入int delete_id(node, int*, int);//根据id删除int delete_name(node, int*);//根据name删除int delete_num(node, int*);//根据num删除void print(node head);//打印int main(){node head;int ret = 0;int n = 0;int n1 = 0;int d = 0;int i = 0;init(&head);for(;ret != 8;){ret = interface();switch(ret){case 1:printf("how many contacts you want to put in?\n");scanf("%d", &n);n1 = n;for(;n1 > 0; n1--){insert_tail(head);}break;case 2:insert_id(head, &n);printf("%d", n);break;case 3:printf("You want to delete by 1.id or 2.name or 3.num\n");scanf("%d", &d);if(1 == d){printf("which id you want to delete?\n");scanf("%d", &i);delete_id(head, &n, i);}else if(2 == d){delete_name(head, &n);}else if(3 == d){delete_num(head, &n);}else {printf("Warning:wrong function!\n");}break;case 7:print(head);break;case 8:break;default:printf("Warning:wrong function!\n");break;}}return 0;}