解题思路:
如下
注意事项:
1.数组的长度设置
2.插入和删除的循环注意是否遍历一遍
3.格式要求,数字前面有空格,分割线****是20个以及只设show的下方
参考代码:
#include#include#include#define SIZE 11 typedef struct NodeName { char ar[8]; int next; }NodeName; void MyCreateArrList(NodeName * str, int m) { int i; str[0].next = 2; str[1].next = 0; for (i = 2; i < m - 1; i++) { str[i].next = i + 1; } str[m - 1].next = 0; for (int j = 0; j < m; j++) { str[j].ar[0] = '\0'; } } int DeleteArrList(NodeName* str, int n, int m) { int i; int temp1 = 1; //头节点,数组最后 for (i = 0; i < n - 1; i++) { temp1 = str[temp1].next; } int temp2 = str[temp1].next;// 1-2-3,此处为2 str[temp1].next = str[temp2].next; //下面是free str[temp2].next = str[0].next; str[0].next = temp2; return 1; } void InsertArrList(NodeName* str, int n, int m) { NodeName p; int temp0 = str[0].next; //temp0是空位的位置,也是p的编号 //上面是malloc int temp1 = 1; //头节点,数组最后 for (int i = 0; i < n - 1; i++) { temp1 = str[temp1].next; } scanf("%s", p.ar); p.next = str[temp1].next; str[temp1].next = temp0; str[temp0] = p; //把新的结构体放入空位 } int SearchArrList(NodeName* str, char * search, int m) { int i; int temp = 1; //头节点,数组最后 for (i = 0; i < m ; i++) { temp = str[temp].next; if (!strcmp(str[temp].ar, search)) break; } return temp; } void ShowArrList(NodeName* str, int m) { int i; for (i = 0; i < m; i++) { if (str[1].next == 10) str[0].next = 0; printf("%-8s%2d\n", str[i].ar, str[i].next); } } void PrintList(NodeName* str, int m) { char check[7]; char check_search[7]; int a; int count = 0; while(scanf("%s", check) != EOF) { if (!strcmp(check, "delete")) { scanf("%d", &a); DeleteArrList(str, a, SIZE); } else if(!strcmp(check, "insert")) { scanf("%d", &a); InsertArrList(str, a, m); str[0].next++; } else if (!strcmp(check, "search")) { scanf("%s", check_search); printf("%2d\n", SearchArrList(str, check_search, m)); printf("********************\n"); } else if (!strcmp(check, "show")) { ShowArrList(str, m); printf("********************\n"); } } } int main(void) { NodeName str[SIZE]; MyCreateArrList(str, SIZE); PrintList(str, SIZE); return 0; }
0.0分
1 人评分
C语言程序设计教程(第三版)课后习题6.3 (C语言代码)浏览:466 |
2003年秋浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:793 |
C语言程序设计教程(第三版)课后习题8.6 (C语言代码)浏览:631 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:485 |
C语言程序设计教程(第三版)课后习题8.2 (C语言代码)浏览:5275 |
printf基础练习2 (C语言代码)浏览:796 |
【蟠桃记】 (C语言代码)浏览:1084 |
C语言程序设计教程(第三版)课后习题11.1 (C语言代码)浏览:651 |
回文数字 (C语言代码)浏览:2539 |
大家好,我是验题君浏览:604 |