fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. /* 第三週:
  6.  
  7.   based on 第二週的題目
  8.  
  9.   1. 請確定 list 中的 10 elements 分別有一 index 欄位用來識別他是第幾個 entries
  10.  
  11.   2. 請刪除 index 5 , 7 的 entries
  12.  
  13.   3. 請在 list 的結尾新增一 index 11 的 entries
  14.  
  15.   4. 請將此 list 印出
  16.  
  17. */
  18.  
  19. struct data {
  20. char name[16];
  21. unsigned long height;
  22. unsigned short index;
  23. struct data *next;
  24. }stu;
  25.  
  26.  
  27. typedef struct data Node;
  28.  
  29. void show(struct data *stu);
  30. Node *create_list(char **arr, unsigned long *arr1, unsigned short *arr2, int);
  31. Node print_list(Node *);
  32. void free_list(Node *);
  33. Node *search_node(Node *, int);
  34. Node *delete_node(Node *, Node *);
  35. void *insert_node(Node *node, char *arr3, unsigned long *arr1, int item);
  36.  
  37. int main()
  38. {
  39.  
  40. struct data stu = {"Kerwin", 177, 77};
  41.  
  42. Node *first, *node;
  43. char *arr[] = {"Ariza", "Bryant", "Clarkson", "Divac", "Ennis", "Fisher", "Gasol", "Horry", "Ingram", "Johnson"};
  44. unsigned long arr1[] = {203, 198, 196, 216, 191, 185, 213, 206, 206, 206};
  45. unsigned short arr2[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  46.  
  47.  
  48. first = create_list(arr, arr1, arr2, 10);
  49.  
  50. node = search_node(first, 5);
  51. first = delete_node(first, node);
  52.  
  53. node = search_node(first, 7);
  54. first = delete_node(first, node);
  55.  
  56.  
  57. node = search_node(first, 10);
  58. insert_node(node, "Kuzma", 206, 11);
  59.  
  60. print_list(first);
  61. free_list(first);
  62. system("pause");
  63. return 0;
  64. }
  65.  
  66.  
  67. void show(struct data *stu)
  68. {
  69. printf("%8s\t%3d\t%7d\n\n", stu->name, stu->height, stu->index);
  70. }
  71.  
  72. Node *create_list(char **arr, unsigned long *arr1, unsigned short *arr2, int len)
  73. {
  74. int i;
  75. Node *first = NULL,*current = NULL,*previous = NULL;
  76.  
  77. for (i=0;i<len;i++) {
  78. current=(Node *) malloc(sizeof(Node));
  79. memset(current, 0, sizeof(Node));
  80. if (current){
  81. if (i == 0) {
  82. first = current;
  83.  
  84. }else {
  85. previous->next = current;
  86. }
  87.  
  88. strcpy(current->name, arr[i]);
  89. current->height = arr1[i];
  90. current->index = arr2[i];
  91. current->next = NULL;
  92. previous = current;
  93. }
  94. }
  95.  
  96. return first;
  97. }
  98.  
  99. Node print_list(Node *first)
  100. {
  101. Node *data = first;
  102. if (first == NULL){
  103. printf("List is empty!\n");
  104. }else{
  105. while (data != NULL) {
  106. printf("%8s\t%3d\t\t%3d\n", data->name, data->height, data->index);
  107. data = data->next;
  108. }
  109. printf("\n");
  110. }
  111. }
  112.  
  113. void free_list(Node *first)
  114. {
  115. Node *current = NULL, *tmp = NULL;
  116. current = first;
  117. while (current != NULL) {
  118. tmp = current;
  119. current = current->next;
  120. free(tmp);
  121. }
  122. }
  123.  
  124. Node *search_node(Node *first, int item)
  125. {
  126. Node *node = first;
  127. while (node != NULL) {
  128. if (node->index == item) {
  129. return node;
  130. }else{
  131. node = node->next;
  132. }
  133. }
  134. return NULL;
  135. }
  136.  
  137. Node *delete_node(Node *first, Node *node)
  138. {
  139. Node *ptr = first;
  140. if (first == NULL) {
  141. printf("Nothing to delete!\n");
  142. return NULL;
  143. }
  144. if (node == first) {
  145. first = first->next;
  146. }else{
  147. while (ptr->next != node) {
  148. ptr = ptr->next;
  149. }
  150. ptr->next = node->next;
  151. }
  152. free(node);
  153. return first;
  154. }
  155.  
  156. void *insert_node(Node *node, char *arr3, unsigned long *arr1, int item)
  157. {
  158. Node *newnode;
  159. newnode = (Node *) malloc(sizeof(Node));
  160. strcpy(newnode->name, arr3);
  161. newnode->height = arr1;
  162. newnode->index = item;
  163. newnode->next = node->next;
  164. node->next = newnode;
  165. }
  166.  
Success #stdin #stdout #stderr 0s 4380KB
stdin
Standard input is empty
stdout
   Ariza	203		  1
  Bryant	198		  2
Clarkson	196		  3
   Divac	216		  4
  Fisher	185		  6
   Horry	206		  8
  Ingram	206		  9
 Johnson	206		 10
   Kuzma	206		 11

stderr
sh: 1: pause: not found