【数据结构】链式存储单链表

前端之家收集整理的这篇文章主要介绍了【数据结构】链式存储单链表前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


数据结构单链表的链式存储实现


  1. //======================================================================
  2. //
  3. // Copyright (C) 2014-2015 SCOTT
  4. // All rights reserved
  5. //
  6. // filename: List.c
  7. // description: a demo to display SeqList
  8. //
  9. // created by SCOTT at 01/28/2015
  10. // http://blog.csdn.net/scottly1
  11. //
  12. //======================================================================
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <malloc.h>
  17.  
  18. #define TRUE 1
  19. #define FALSE 1
  20.  
  21. typedef int Status;
  22. typedef int ElementType;
  23.  
  24. typedef struct _tag_Node
  25. {
  26. ElementType data;
  27. struct _tag_Node *next;
  28. }NODE,*PNODE;
  29.  
  30.  
  31. PNODE initList()
  32. {
  33. int i,j = 0;
  34. int nLen;
  35. PNODE pHead = NULL;
  36. PNODE pNode = NULL;
  37.  
  38. printf("Input The Length:");
  39. scanf("%d",&nLen);
  40.  
  41. pHead = (PNODE)malloc(sizeof(NODE));
  42. pHead->data = nLen; // 头结点数据域可用于存储如链表长度等信息。
  43. pHead->next = NULL;
  44.  
  45. for(i=0; i<nLen; ++i,++j)
  46. {
  47. pNode = (PNODE)malloc(sizeof(NODE));
  48. printf("Input The %d val:",nLen-j);
  49. scanf("%d",&(pNode->data));
  50.  
  51. pNode->next = pHead->next; // 注意链表的生成技巧!
  52. pHead->next = pNode;
  53. }
  54.  
  55. return pHead;
  56. }
  57.  
  58.  
  59. Status traverseList(PNODE list)
  60. {
  61. int i = 1;
  62. PNODE p = list->next;
  63. ElementType data;
  64.  
  65. while(p)
  66. {
  67. data = p->data;
  68. printf("The %d Data is:%d\n",i++,data);
  69. p = p->next;
  70. }
  71.  
  72. printf("The Length of List is %d\n\n",list->data);
  73.  
  74. return TRUE;
  75. }
  76.  
  77.  
  78. Status deleteNode(PNODE list,int pos,PNODE ret)
  79. {
  80. int i = 0;
  81. PNODE pHead = list;
  82. PNODE pTmp = NULL;
  83.  
  84. if(!pHead || pos <=0 || pos >pHead->data)
  85. {
  86. return FALSE;
  87. }
  88.  
  89. while(pHead->next && i<pos-1)
  90. {
  91. pHead = pHead->next;
  92. i++;
  93. }
  94.  
  95. if(!pHead || i>pos-1)
  96. {
  97. return FALSE;
  98. }
  99.  
  100. pTmp = pHead->next;
  101. pHead->next = pTmp->next;
  102. free(pTmp);
  103. --list->data;
  104.  
  105. return TRUE;
  106. }
  107.  
  108.  
  109. Status insertNode(PNODE list,PNODE node)
  110. {
  111. int i = 0;
  112. PNODE pHead = list;
  113.  
  114. if(!pHead || pos <=0) //pos >pHead->data
  115. {
  116. return FALSE;
  117. }
  118.  
  119. while(pHead->next && i<pos-1)
  120. {
  121. pHead = pHead->next;
  122. ++i;
  123. }
  124.  
  125. node->next = pHead->next;
  126. pHead->next = node;
  127. ++list->data;
  128.  
  129. return TRUE;
  130. }
  131.  
  132.  
  133. int main()
  134. {
  135. PNODE list;
  136. PNODE ret;
  137. PNODE node;
  138.  
  139. list = initList();
  140. traverseList(list);
  141.  
  142. // 删除测试
  143. deleteNode(list,2,ret);
  144. traverseList(list);
  145.  
  146. // 插入测试
  147. node = (PNODE)malloc(sizeof(NODE));
  148. node->data = 100;
  149. insertNode(list,node); // not success
  150. insertNode(list,1000,node); // insert in last
  151. traverseList(list);
  152.  
  153. return 0;
  154. }
  155. @H_502_33@

  156. 注:原创文章,转载请注明出处:http://blog.csdn.net/scottly1/article/details/43247465

猜你在找的数据结构相关文章