fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. /* 第二周
  6.  
  7.   1. 請在第一週宣告的 data structure 中新增一 data structure pointer 欄位 (additional 4 bytes)
  8.  
  9.   2. 用 malloc + for loop 的方式, 新增 10 個 structure 並串為 list, 10 個 elements 的內容要有所不同
  10.  
  11.   3. 請將此 list 傳入一 function 並印出 list 內容
  12.  
  13.   4. 離開程式前, 請將 malloc 的記憶體 free 掉
  14.  
  15. */
  16.  
  17. struct data {
  18. char name[16];
  19. unsigned long height;
  20. unsigned short weight;
  21. struct data *next;
  22. }stu;
  23.  
  24.  
  25. typedef struct data Node;
  26.  
  27. void show(struct data *stu);
  28. Node *createList(char *arr[], unsigned long *arr1, unsigned short *arr2, int);
  29. Node printList(Node *);
  30. void freeList(Node *);
  31.  
  32. int main()
  33. {
  34.  
  35. struct data stu = {"Kerwin", 177, 77};
  36.  
  37. Node *first;
  38. char *arr[] = {"Ariza", "Bryant", "Clarkson", "Divac", "Ennis", "Fisher", "Gasol", "Horry", "Ingram", "Johnson"};
  39. unsigned long arr1[] = {203, 198, 196, 216, 191, 185, 213, 206, 206, 206};
  40. unsigned short arr2[] = {98, 96, 88, 110, 88, 91, 113, 106, 86, 98};
  41.  
  42. first = createList(arr, arr1, arr2, 10);
  43.  
  44. show(&stu);
  45.  
  46. printList(first);
  47. freeList(first);
  48. system("pause");
  49. return 0;
  50. }
  51.  
  52.  
  53. void show(struct data *stu)
  54. {
  55. printf("%8s\t%3d\t%7d\n\n", stu->name, stu->height, stu->weight);
  56. }
  57.  
  58. Node *createList(char *arr[], unsigned long *arr1, unsigned short *arr2, int len)
  59. {
  60. int i;
  61. Node *first = NULL,*current = NULL,*previous = NULL;
  62.  
  63. for (i=0;i<len;i++){
  64. current=(Node *) malloc(sizeof(Node));
  65. memset(current, 0, sizeof(Node));
  66. if (current){
  67. if (i == 0){
  68. first = current;
  69.  
  70. }else{
  71. previous->next = current;
  72. }
  73.  
  74. strcpy(current->name, arr[i]);
  75. current->height = arr1[i];
  76. current->weight = arr2[i];
  77. current->next = NULL;
  78. previous = current;
  79. }
  80. }
  81.  
  82. return first;
  83. }
  84.  
  85. Node printList(Node *first)
  86. {
  87. Node *data = first;
  88. if (first == NULL){
  89. printf("List is empty!\n");
  90. }else{
  91. while (data != NULL){
  92. printf("%8s\t%3d\t\t%3d\n", data->name, data->height, data->weight);
  93. data = data->next;
  94. }
  95. printf("\n");
  96. }
  97. }
  98.  
  99. void freeList(Node *first)
  100. {
  101. Node *current = NULL, *tmp = NULL;
  102. current = first;
  103. while (current != NULL){
  104. tmp = current;
  105. current = current->next;
  106. free(tmp);
  107. }
  108. }
Success #stdin #stdout #stderr 0s 4492KB
stdin
Standard input is empty
stdout
  Kerwin	177	     77

   Ariza	203		 98
  Bryant	198		 96
Clarkson	196		 88
   Divac	216		110
   Ennis	191		 88
  Fisher	185		 91
   Gasol	213		113
   Horry	206		106
  Ingram	206		 86
 Johnson	206		 98

stderr
sh: 1: pause: not found