当前位置:   article > 正文

C语言链表常见用法_link with -lrt. c语言

link with -lrt. c语言

           链表是一种数据结构序列,它通过链环连接在一起。链环包含不同数据。每个链环包含有对其它链环的链接。链表是除数组之外使用最广的数据结构,其常见用法示例如下:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. struct node
  6. {
  7. int data; int key;
  8. struct node * next;
  9. };
  10. struct node * head = NULL;
  11. struct node * current = NULL;
  12. //display the list
  13. void printList()
  14. {
  15. struct node * ptr = head;
  16. printf("\n[ ");
  17. //start from the beginning
  18. while (ptr != NULL)
  19. {
  20. printf("(%d,%d) ", ptr->key, ptr->data);
  21. ptr = ptr->next;
  22. }
  23. printf(" ]");
  24. }
  25. //insert link at the first location
  26. void insertFirst(int key, int data)
  27. {
  28. //create a link
  29. struct node * link = (struct node*) malloc(sizeof(struct node));
  30. link->key = key;
  31. link->data = data;
  32. //point it to old first node
  33. link->next = head;
  34. //point first to new first node
  35. head = link;
  36. }
  37. //delete first item
  38. struct node* deleteFirst()
  39. {
  40. //save reference to first link
  41. struct node * tempLink = head;
  42. //mark next to first link as first
  43. head = head->next;
  44. //return the deleted link
  45. return tempLink;
  46. }
  47. //is list empty
  48. bool isEmpty()
  49. {
  50. return head == NULL;
  51. }
  52. int length()
  53. {
  54. int length = 0;
  55. struct node * current;
  56. for (current = head; current != NULL; current = current->next)
  57. {
  58. length++;
  59. }
  60. return length;
  61. }
  62. //find a link with given key
  63. struct node* find(int key) {
  64. //start from the first link
  65. struct node* current = head;
  66. //if list is em pty
  67. if (head == NULL)
  68. {
  69. return NULL;
  70. }
  71. //navigate through list
  72. while (current->key != key) {
  73. //if it is last node
  74. if (current->next == NULL) {
  75. return NULL;
  76. }
  77. else {
  78. //go to next link
  79. current = current->next;
  80. }
  81. }
  82. //if data found, return the current Link
  83. return current;
  84. }
  85. //delete a link with given key
  86. struct node* delete(int key) {
  87. //start from the first link
  88. struct node* current = head;
  89. struct node* previous = NULL;
  90. //if list is em pty
  91. if (head == NULL) {
  92. return NULL;
  93. }
  94. //navigate through list
  95. while (current->key != key) {
  96. //if it is last node
  97. if (current->next == NULL) {
  98. return NULL;
  99. }
  100. else {
  101. //store reference to current link
  102. previous = current;
  103. //move to next link
  104. current = current->next;
  105. }
  106. }
  107. //found a match, update the link
  108. if (current == head) {
  109. //change first to point to next link
  110. head = head->next;
  111. }
  112. else {
  113. //bypass the current link
  114. previous->next = current->next;
  115. }
  116. return current;
  117. }
  118. void sort() {
  119. int i, j, k, tempKey, tempData;
  120. struct node * current;
  121. struct node * next;
  122. int size = length();
  123. k = size;
  124. for (i = 0; i < size - 1; i++, k--) {
  125. current = head;
  126. next = head->next;
  127. for (j = 1; j < k; j++) {
  128. if (current->data > next->data) {
  129. tempData = current->data;
  130. current->data = next->data;
  131. next->data = tempData;
  132. tempKey = current->key;
  133. current->key = next->key;
  134. next->key = tempKey;
  135. }
  136. current = current->next;
  137. next = next->next;
  138. }
  139. }
  140. }
  141. void reverse(struct node* * head_ref) {
  142. struct node* prev = NULL;
  143. struct node* current = *head_ref;
  144. struct node* next;
  145. while (current != NULL) {
  146. next = current->next;
  147. current->next = prev;
  148. prev = current;
  149. current = next;
  150. }
  151. *head_ref = prev;
  152. }
  153. int main() {
  154. insertFirst(1, 10);
  155. insertFirst(2, 20);
  156. insertFirst(3, 30);
  157. insertFirst(4, 1);
  158. insertFirst(5, 40);
  159. insertFirst(6, 56);
  160. printf("Original List: ");
  161. //print list
  162. printList();
  163. while (!isEmpty()) {
  164. struct node * temp = deleteFirst();
  165. printf("\nDeleted value:");
  166. printf("(%d,%d) ", temp->key, temp->data);
  167. }
  168. printf("\nList after deleting all item s: ");
  169. printList();
  170. insertFirst(1, 10);
  171. insertFirst(2, 20);
  172. insertFirst(3, 30);
  173. insertFirst(4, 1);
  174. insertFirst(5, 40);
  175. insertFirst(6, 56);
  176. printf("\nRestored List: ");
  177. printList();
  178. printf("\n");
  179. struct node * foundLink = find(4);
  180. if (foundLink != NULL) {
  181. printf("Elem ent found: ");
  182. printf("(%d,%d) ", foundLink->key, foundLink->data);
  183. printf("\n");
  184. }
  185. else {
  186. printf("Elem ent not found.");
  187. }
  188. delete(4);
  189. printf("List after deleting an item : ");
  190. printList();
  191. printf("\n");
  192. foundLink = find(4);
  193. if (foundLink != NULL) {
  194. printf("Elem ent found: ");
  195. printf("(%d,%d) ", foundLink->key, foundLink->data);
  196. printf("\n");
  197. }
  198. else {
  199. printf("Elem ent not found.");
  200. }
  201. printf("\n");
  202. sort();
  203. printf("List after sorting the data: "); printList();
  204. reverse(&head);
  205. printf("\nList after reversing the data: "); printList();
  206. return 0;
  207. }

编译并运行以上程序,结果如下:

  1. Original List:
  2. [ (6,56) (5,40) (4,1) (3,30) (2,20) (1,10) ] Deleted value:(6,56)
  3. Deleted value:(5,40)
  4. Deleted value:(4,1)
  5. Deleted value:(3,30)
  6. Deleted value:(2,20)
  7. Deleted value:(1,10)
  8. List after deleting all items:
  9. [ ]
  10. Restored List:
  11. [ (6,56) (5,40) (4,1) (3,30) (2,20) (1,10) ]
  12. Element found: (4,1)
  13. List after deleting an item:
  14. [ (6,56) (5,40) (3,30) (2,20) (1,10) ]
  15. Element not found.
  16. List after sorting the data:
  17. [ (1,10) (2,20) (3,30) (5,40) (6,56) ]
  18. List after reversing the data:
  19. [ (6,56) (5,40) (3,30) (2,20) (1,10) ]


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/645895
推荐阅读
相关标签
  

闽ICP备14008679号