通讯录程序(未运行成功)
1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 5 typedef struct{ 6 char num[5]; 7 char name[9]; 8 char sex[3]; 9 char phone[13]; 10 char addr[31]; 11 }DataType; 12 13 typedef struct node{ 14 DataType data; 15 struct node*next; 16 }ListNode; 17 18 typedef ListNode*LinkList; 19 ListNode*p; 20 LinkList head; 21 22 int menu_select(); 23 LinkList CreateList(void); 24 void InsertNode(LinkList head, ListNode*p); 25 ListNode *LinkFind(LinkList head); 26 void DelNode(LinkList head); 27 void PrintList(LinkList head); 28 29 void main() 30 { 31 for (;;){ 32 switch (menu_select()) 33 { 34 case 1: 35 printf("通讯录的建立\n"); 36 head = CreateList(); 37 break; 38 case 2: 39 printf("通讯录结点的插入\n"); 40 p = (ListNode*) malloc(sizeof(ListNode)); 41 scanf("%s%s%s%s%s", p->data.num, p->data.name, p->data.sex, p->data.phone, p->data.addr); 42 InsertNode(head, p); 43 break; 44 case 3: 45 printf("通讯录结点的查询\n"); 46 p = LinkFind(head); 47 if (p != NULL) 48 { 49 printf("编号 姓名 性别 联系电话 地址\n"); 50 printf("%s%s%s%s%s", p->data.num, p->data.name, p->data.sex, p->data.phone, p->data.addr); 51 } 52 else 53 printf("没查到要查询的通讯者!\n"); 54 break; 55 case 4: 56 printf("通讯录结点的删除\n"); 57 DelNode(head); 58 break; 59 case 5: 60 printf("通讯录链表的输出\n"); 61 PrintList(head); 62 break; 63 case 0: 64 printf("再见!\n"); 65 return; 66 } 67 } 68 } 69 70 int menu_select() 71 { 72 int sn; 73 printf(" 通讯录管理系统\n"); 74 printf("==================\n"); 75 printf(" 1,通讯录的建立\n"); 76 printf(" 2,通讯录结点的插入\n"); 77 printf(" 3,通讯录结点的查询\n"); 78 printf(" 4,通讯录结点的删除\n"); 79 printf(" 5,通讯录链表的输出\n"); 80 printf(" 0,退出管理系统\n"); 81 printf("==================\n"); 82 printf("请选择0---5\n"); 83 for (;;) 84 { 85 scanf("%d", &sn); 86 if (sn < 0 || sn>5) 87 printf("\n\t输入错误。重选0--5"); 88 else 89 break; 90 } 91 return sn; 92 } 93 94 LinkList CreateList(void) 95 { 96 LinkList head = (ListNode*) malloc(sizeof(ListNode)); 97 ListNode *p, *rear; 98 int flag = 0; 99 rear = head; 100 while (flag == 0) 101 { 102 p = (ListNode*) malloc(sizeof(ListNode)); 103 printf("编号(4),姓名(8),性别,电话(11),地址(31)\n"); 104 scanf("%s%s%s%s%s", p->data.num, p->data.name, p->data.sex, p->data.phone, p->data.addr); 105 rear->next = p; 106 rear = p; 107 printf("继续输入吗?(1/0):"); 108 getchar(); 109 scanf("%d", &flag); 110 } 111 rear->next = NULL; 112 return head; 113 } 114 115 void InsertNode(LinkList head, ListNode*p) 116 { 117 ListNode*p1, *p2; 118 p1 = head; 119 p2 = p1->next; 120 while (p2 != NULL && strcmp(p2->data.num, p->data.num) < 0) 121 { 122 p1 = p2; 123 p2 = p2->next; 124 } 125 p1->next = p; 126 p->next = p2; 127 } 128 129 ListNode *LinkFind(LinkList head) 130 { 131 ListNode*p; 132 char num[5]; 133 char name[9]; 134 int xz; 135 printf("===========\n"); 136 printf("1,按编号查询\n"); 137 printf("2,按姓名查询\n"); 138 printf("===========\n"); 139 printf("请选择:"); 140 p = head->next; 141 scanf("%d", &xz); 142 if (xz == 1) 143 { 144 printf("请输入查找者的编号:"); 145 scanf("%s", num); 146 while (p && strcmp(p->data.num, num) < 0) 147 p = p->next; 148 if (p == NULL && strcmp(p->data.num, num) > 0) 149 p = NULL; 150 } 151 else 152 if (xz == 2){ 153 printf("请输入要查找者的姓名:\n"); 154 scanf("%s", name); 155 while (p && strcmp(p->data.name, name) != 0); 156 p = p->next; 157 } 158 return p; 159 } 160 161 void DelNode(LinkList head) 162 { 163 char jx; 164 ListNode *p, *q; 165 p = LinkFind(head); 166 if (p == NULL){ 167 printf("没有查到想要删除的通讯者\n"); 168 return; 169 } 170 printf("真的要删除该结点吗?(y/n):"); 171 scanf("%c", &jx); 172 if (jx == 'y' || jx == 'Y') 173 { 174 q = head; 175 while (q != NULL && q->next != p) 176 q = q->next; 177 q->next = p->next; 178 free(p); 179 printf("通讯者已被删除\n"); 180 } 181 } 182 183 void PrintList(LinkList head) 184 { 185 ListNode*p; 186 p = head->next; 187 printf("编号 姓名 性别 联系电话 地址\n"); 188 printf("-----------------------------\n"); 189 while (p != NULL) 190 { 191 printf("%s,%s,%s,%s,%s\n", p->data.num, p->data.name, p->data.sex, p->data.phone, p->data.addr); 192 printf("-------------------------\n"); 193 p = p->next; 194 } 195 }
1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 5 typedef struct{ 6 char num[5]; 7 char name[9]; 8 char sex[3]; 9 char phone[13]; 10 char addr[31]; 11 }DataType; 12 13 typedef struct node{ 14 DataType data; 15 struct node*next; 16 }ListNode; 17 18 typedef ListNode*LinkList; 19 ListNode*p; 20 LinkList head; 21 22 int menu_select(); 23 LinkList CreateList(void); 24 void InsertNode(LinkList head, ListNode*p); 25 ListNode *LinkFind(LinkList head); 26 void DelNode(LinkList head); 27 void PrintList(LinkList head); 28 29 void main() 30 { 31 while(1) { 32 33 switch (menu_select()) 34 { 35 case 1: 36 printf("通讯录的建立\n"); 37 head = CreateList(); 38 break; 39 case 2: 40 printf("通讯录结点的插入\n"); 41 p = (ListNode*) malloc(sizeof(ListNode)); 42 scanf("%s %s %s %s %s", &p->data.num, &p->data.name, &p->data.sex, &p->data.phone, &p->data.addr); 43 InsertNode(head, p); 44 break; 45 case 3: 46 printf("通讯录结点的查询\n"); 47 p = LinkFind(head); 48 if (p != NULL) 49 { 50 printf("编号 姓名 性别 联系电话 地址\n"); 51 printf("%s %s %s %s %s", p->data.num, p->data.name, p->data.sex, p->data.phone, p->data.addr); 52 } 53 else 54 printf("没查到要查询的通讯者!\n"); 55 break; 56 case 4: 57 printf("通讯录结点的删除\n"); 58 DelNode(head); 59 break; 60 case 5: 61 printf("通讯录链表的输出\n"); 62 PrintList(head); 63 break; 64 case 0: 65 printf("再见!\n"); 66 return; 67 } 68 } 69 70 } 71 72 int menu_select() 73 { 74 int sn; 75 printf(" 通讯录管理系统\n"); 76 printf("==================\n"); 77 printf(" 1,通讯录的建立\n"); 78 printf(" 2,通讯录结点的插入\n"); 79 printf(" 3,通讯录结点的查询\n"); 80 printf(" 4,通讯录结点的删除\n"); 81 printf(" 5,通讯录链表的输出\n"); 82 printf(" 0,退出管理系统\n"); 83 printf("==================\n"); 84 printf("请选择0---5\n"); 85 scanf("%d",&sn); 86 87 return sn; 88 } 89 LinkList CreateList(void) 90 { 91 LinkList head = (ListNode*) malloc(sizeof(ListNode)); 92 ListNode *p, *rear; 93 int flag = 1; 94 rear = head; 95 while (flag) 96 { 97 p = (ListNode*) malloc(sizeof(ListNode)); 98 printf("编号(4),姓名(8),性别,电话(11),地址(31)\n"); 99 scanf("%s %s %s %s %s", &p->data.num, &p->data.name, &p->data.sex, &p->data.phone, &p->data.addr); 100 rear->next = p; 101 rear = p; 102 103 printf("继续输入吗?(1/0):"); 104 scanf("%d", &flag); 105 } 106 rear->next = NULL; 107 return head; 108 } 109 110 void InsertNode(LinkList head, ListNode*p) 111 { 112 ListNode*p1, *p2; 113 p1 = head; 114 p2 = p1->next; 115 while (p2 != NULL && strcmp(p2->data.num, p->data.num) < 0) 116 { 117 p1 = p2; 118 p2 = p2->next; 119 } 120 p1->next = p; 121 p->next = p2; 122 } 123 124 ListNode *LinkFind(LinkList head) 125 { 126 ListNode*p; 127 char num[5]; 128 char name[9]; 129 int xz; 130 printf("===========\n"); 131 printf("1,按编号查询\n"); 132 printf("2,按姓名查询\n"); 133 printf("===========\n"); 134 printf("请选择:"); 135 p = head->next; 136 scanf("%d", &xz); 137 if (xz == 1) 138 { 139 printf("请输入查找者的编号:"); 140 scanf("%s", &num); 141 while (p && strcmp(p->data.num,num) < 0) 142 p = p->next; 143 if (p == NULL && strcmp(p->data.num, num) > 0) 144 p = NULL; 145 } 146 else 147 if (xz == 2){ 148 printf("请输入要查找者的姓名:\n"); 149 scanf("%s", &name); 150 while (p && strcmp(p->data.name, name) != 0); 151 p = p->next; 152 } 153 return p; 154 } 155 156 void DelNode(LinkList head) 157 { 158 char jx; 159 ListNode *p, *q; 160 p = LinkFind(head); 161 if (p == NULL){ 162 printf("没有查到想要删除的通讯者\n"); 163 return; 164 } 165 printf("真的要删除该结点吗?(y/n):"); 166 scanf("%c", &jx); 167 if (jx == 'y' || jx == 'Y') 168 { 169 q = head; 170 while (q!=NULL && q->next != p) 171 q = q->next; 172 q->next = p->next; 173 free(p); 174 printf("通讯者已被删除\n"); 175 } 176 } 177 178 void PrintList(LinkList head) 179 { 180 ListNode*p; 181 p = head->next; 182 printf("编号 姓名 性别 联系电话 地址\n"); 183 printf("-----------------------------\n"); 184 while (p != NULL) 185 { 186 printf("%s,%s,%s,%s,%s\n", p->data.num, p->data.name, p->data.sex, p->data.phone, p->data.addr); 187 printf("-------------------------\n"); 188 p = p->next; 189 } 190 }