链表|链表 的 增删改查(源码分析)

【链表|链表 的 增删改查(源码分析)】链表 的 增删改查(源码分析)
Code:

  1. #include
  2. #include
  3. #include
  4. struct Node
  5. {
  6. char *City;
  7. int Temp;
  8. struct Node *Next;
  9. };
  10. typedef struct Node *Link;
  11. Link Head;
  12. int NodeCount;
  13. int AddNodeAscend(Link);
  14. int DuplicateNode(Link,Link);
  15. void CreateList(void);
  16. int DeleteNode(Link);
  17. void FreeNode(Link);
  18. void ShowNodes(void);
  19. int NodeCmp(Link,Link);
  20. /* -----function definition*/
  21. int AddNodeAscend(Link to_add)
  22. {
  23. Link pn,
  24. prev,
  25. curr;
  26. struct Node dummy;
  27. int i;
  28. pn = (Link)malloc(sizeof(struct Node));
  29. if(NULL== pn)
  30. {
  31. return 0;
  32. }
  33. memcpy(pn,to_add,sizeof(struct Node));
  34. /*--set up a dummy node to simply logic*/
  35. dummy.Next = Head;
  36. prev = &dummy;
  37. curr = Head;
  38. /* ---insert node pn--*/
  39. for( ; ; prev = curr,curr = curr->Next)
  40. {
  41. if(NULL == curr)
  42. break;
  43. i = NodeCmp(pn,curr);
  44. if(i<=0)
  45. break;
  46. }
  47. if(0==curr && i)
  48. if(DuplicateNode(curr,pn)==0)
  49. return (1);
  50. prev->Next = pn;
  51. pn->Next = curr;
  52. Head = dummy.Next;
  53. return (1);
  54. }
  55. int DuplicateNode(Link inlist,Link duplicate)
  56. {
  57. FreeNode(duplicate);
  58. return (0);
  59. }
  60. int DeleteNode(Link to_delete)
  61. {
  62. Link curr,
  63. prev;
  64. int i;
  65. /*Is there anything in the list?----*/
  66. if(NULL == Head)
  67. return (0);
  68. for(prev = NULL,curr = Head;
  69. curr !=NULL&&(i=NodeCmp(to_delete,curr))>0;
  70. prev =curr,curr = curr->Next)
  71. /*Find a match ,so delete it ---*/
  72. if(curr !=NULL&&i==0)
  73. {
  74. if(prev)
  75. prev->Next = curr->Next;
  76. else
  77. Head = curr->Next;
  78. FreeNode(curr);
  79. NodeCount -=1;
  80. return (1);
  81. }
  82. return (0);
  83. }
  84. int NodeCmp(Link a,Link b)
  85. {
  86. /*returns 1,0,-1 ,depending on whether
  87. *the data in a is
  88. *greater,equal,or less than b
  89. *
  90. */
  91. if(a->Temp!=b->Temp)
  92. return (a->Temp-b->Temp);
  93. else
  94. return strcmp(a->City,b->City);
  95. }
  96. void CreateList(void)
  97. {
  98. Head =NULL;
  99. NodeCount = 0;
  100. }
  101. void FreeNode(Link n)
  102. {
  103. free(n->City);
  104. free(n);
  105. }
  106. void ShowNodes(void)
  107. {
  108. Link pn;
  109. int count,median;
  110. for(count=0,pn=Head; pn; pn= pn->Next)
  111. count+=1;
  112. //compute the median Node;
  113. median =count/2+1;
  114. if(count)
  115. {
  116. count = 0;
  117. for(pn=Head; pn ; pn=pn->Next)
  118. {
  119. printf("%-20s:%3d",pn->City,pn->Temp);
  120. count+=1;
  121. if(count == median)
  122. printf("---Median---");
  123. printf("/n");
  124. }
  125. }
  126. else
  127. {
  128. printf("Empty list /n");
  129. }
  130. }
  131. int main(int argc,char *argv[])
  132. {
  133. FILE *fin;
  134. char buffer[120];
  135. struct Node n;
  136. if(argc !=2)
  137. {
  138. fprintf(stderr,"Usage:citytemp filename.ext/n");
  139. exit(EXIT_FAILURE);
  140. }
  141. fin = fopen(argv[1],"rt");
  142. if(fin == NULL)
  143. {
  144. fprintf(stderr,"Cannot open/find %s/n",argv[2]);
  145. exit(EXIT_FAILURE);
  146. }
  147. while (!feof(fin))
  148. {
  149. if(fgets(buffer,127,fin)==NULL)
  150. break;
  151. buffer[strlen(buffer-1)]='/0';
  152. n.City = strdup(buffer+3);
  153. buffer[3] = '/0';
  154. n.Temp = atoi(buffer);
  155. if(AddNodeAscend(&n)==0)
  156. {
  157. fprintf(stderr,"Error adding node.abborting/n");
  158. exit(EXIT_FAILURE);
  159. }
  160. }
  161. ShowNodes();
  162. printf("/n");
  163. DeleteNode(Head);
  164. ShowNodes();
  165. while(Head && Head->Next)
  166. {
  167. printf("/n");
  168. DeleteNode(Head->Next);
  169. ShowNodes();
  170. }
  171. printf("/n");
  172. DeleteNode(Head);
  173. ShowNodes();
  174. fclose(fin);
  175. return (EXIT_SUCCESS);
  176. }


    推荐阅读