Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. //
  2. // DoubleLink.c
  3. // DataStructureAndAlgorithm
  4. //
  5. // Created by wanyakun on 2016/10/27.
  6. // Copyright © 2016年 com.ucaiyuan. All rights reserved.
  7. //
  8.  
  9. #include "DoubleLink.h"
  10. #include <stdlib.h>
  11.  
  12. //typedef struct ElementType ElemType;
  13.  
  14. typedef struct DoubleLinkNode {
  15. struct DoubleLinkNode *preNode;
  16. struct DoubleLinkNode *nextNode;
  17.  
  18. void *data;
  19. } DLNode;
  20.  
  21.  
  22. /// 表头。不存放元素值
  23. static DLNode *head = NULL;
  24. /// 节点个数
  25. static int count = 0;
  26.  
  27. static DLNode * createNode(void *data) {
  28. DLNode *node = NULL;
  29. node = (DLNode *)malloc(sizeof(node));
  30. if (!node) {
  31. printf("create node error!\n");
  32. return NULL;
  33. }
  34.  
  35. // 默认node的前一节点和后一节点都指向自身
  36. node->preNode = node->nextNode = node;
  37. // 节点的值为data
  38. node->data = data;
  39.  
  40. return node;
  41. }
  42.  
  43. int createDLink() {
  44. head = createNode(NULL);
  45. if (!head) {
  46. return -1;
  47. }
  48.  
  49. count = 0;
  50.  
  51. return 0;
  52. }
  53.  
  54. int destroyDLink() {
  55. if (!head) {
  56. printf("%s failed! DLink is null!\n", __func__);
  57. return -1;
  58. }
  59.  
  60. DLNode *node = head->nextNode;
  61. DLNode *temp = NULL;
  62. while (node != head) {
  63. temp = node;
  64. node = node->nextNode;
  65. free(temp);
  66. }
  67.  
  68. free(head);
  69. head = NULL;
  70. count = 0;
  71.  
  72. return 0;
  73. }
  74.  
  75. int isDLinkEmpty() {
  76. return count == 0;
  77. }
  78.  
  79. int dLinkSize() {
  80. return count;
  81. }
  82.  
  83. static DLNode * _getNode(int index) {
  84. if (index < 0 || index >= count) {
  85. printf("%s failed! index out of bound!\n", __func__);
  86. return NULL;
  87. }
  88.  
  89. if (index <= count/2) {
  90. int i = 0;
  91. DLNode *node = head->nextNode;
  92. while ((i++) < index) {
  93. node = node->nextNode;
  94. }
  95. return node;
  96. }
  97.  
  98. int j = 0;
  99. int rIndex = count - index - 1;
  100. DLNode *node = head->preNode;
  101. while ((j++) < rIndex) {
  102. node = node->preNode;
  103. }
  104. return node;
  105. }
  106.  
  107. void* getNodeData(int index) {
  108. DLNode *node = _getNode(index);
  109. if (!node) {
  110. printf("%s failed!\n", __func__);
  111. return NULL;
  112. }
  113.  
  114. return node->data;
  115. }
  116.  
  117. void* firstNodeData() {
  118. return getNodeData(0);
  119. }
  120.  
  121. void* lastNodeData() {
  122. return getNodeData(count-1);
  123. }
  124.  
  125. int insertData(int index, void *data) {
  126. if (index == 0) {
  127. return insertFirst(data);
  128. }
  129.  
  130. DLNode *indexNode = _getNode(index);
  131. if (!indexNode) {
  132. return -1;
  133. }
  134.  
  135. DLNode *node = createNode(data);
  136. if (!node) {
  137. return -1;
  138. }
  139.  
  140. node->preNode = indexNode->preNode;
  141. node->nextNode = indexNode;
  142. indexNode->preNode->nextNode = node;
  143. indexNode->preNode = node;
  144.  
  145. count++;
  146.  
  147. return 0;
  148. }
  149.  
  150. int insertFirst(void *data) {
  151. DLNode *node = createNode(data);
  152. if (!node) {
  153. return -1;
  154. }
  155.  
  156. node->preNode = head;
  157. node->nextNode = head->nextNode;
  158. head->nextNode->preNode = node;
  159. head->nextNode = node;
  160.  
  161. count++;
  162.  
  163. return 0;
  164. }
  165.  
  166. int appendData(void *data) {
  167. DLNode *node = createNode(data);
  168. if (!node) {
  169. return -1;
  170. }
  171.  
  172. node->nextNode = head;
  173. node->preNode = head->preNode;
  174. head->preNode->nextNode = node;
  175. head->preNode = node;
  176.  
  177. count++;
  178.  
  179. return 0;
  180. }
  181.  
  182. int deleteNode(int index) {
  183. DLNode *node = _getNode(index);
  184. if (!node) {
  185. printf("%s failed! the index is out of bound!\n", __func__);
  186. return -1;
  187. }
  188.  
  189. node->nextNode->preNode = node->preNode;
  190. node->preNode->nextNode = node->nextNode;
  191. free(node);
  192. count--;
  193.  
  194. return 0;
  195. }
  196.  
  197. int deleteFirstNode() {
  198. return deleteNode(0);
  199. }
  200.  
  201. int deleteLastNode() {
  202. return deleteNode(count-1);
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement