Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. /* self-referential structure */
  4. struct Node
  5. {
  6. int *items;
  7. int lindx, rindx;
  8. struct Node *next;
  9. };
  10.  
  11. struct List
  12. {
  13. int ncap;
  14. struct Node *head;
  15. struct Node *tail;
  16. };
  17.  
  18. struct List SLL_new(int cap)
  19. {
  20. /* construct an empty list */
  21. struct List list;
  22. list.ncap = cap;
  23. list.head = NULL;
  24. list.tail = NULL;
  25. return list;
  26. }
  27.  
  28. struct Node *Node_new(int cap, int lft, int rgt)
  29. {
  30. struct Node *node = malloc(sizeof(struct Node));
  31. node->items = malloc(sizeof(int) * cap);
  32. node->rindx = rgt;
  33. node->lindx = lft;
  34. node->next = NULL;
  35. return node;
  36. }
  37.  
  38. int SLL_length(struct List *list)
  39. {
  40. /* return the number of items in the list */
  41. struct Node *p;
  42. int n = 0;
  43. for (p = list->head; p != NULL; p = p->next)
  44. {
  45. n += p->rindx - p->rindx;
  46. }
  47. return n;
  48. }
  49.  
  50. int SLL_empty(struct List *list)
  51. {
  52. /* return true if the list contains no items */
  53. return list->head == NULL;
  54. }
  55.  
  56. int SLL_contains(struct List *list, int item)
  57. {
  58. int i;
  59. /* return true if the list contains the item */
  60. struct Node *p;
  61. for (p = list->head; p != NULL; p = p->next)
  62. for (i = p->lindx; i < p->rindx; i++)
  63. if (p->items[i] == item)
  64. break;
  65. return p != NULL;
  66. }
  67.  
  68. int SLL_pop(struct List *list)
  69. {
  70. int item = 0;
  71. /* remove and return the first item of the list */
  72. struct Node *node = list->head;
  73. item = node->items[node->lindx];
  74. node->lindx++;
  75.  
  76. if (node->lindx > node->rindx)
  77. {
  78. list->head = node->next;
  79. free(node);
  80. }
  81.  
  82. if (SLL_empty(list))
  83. {
  84. list->tail = NULL;
  85. }
  86.  
  87. return item;
  88. }
  89.  
  90. void SLL_push(struct List *list, int item)
  91. {
  92. struct Node *node = list->head;
  93.  
  94. /* insert the item at the front of the list */
  95. if (SLL_empty(list) || node->lindx == 0)
  96. {
  97. node = Node_new(list->ncap, list->ncap, list->ncap - 1);
  98. node->next = list->head;
  99. if (SLL_empty(list))
  100. {
  101. list->tail = node;
  102. }
  103. list->head = node;
  104. }
  105.  
  106. node->lindx--;
  107. node->items[node->lindx] = item;
  108.  
  109. if (SLL_empty(list))
  110. {
  111. list->tail = node;
  112. }
  113.  
  114. list->head = node;
  115. }
  116.  
  117. void SLL_append(struct List *list, int item)
  118. {
  119. struct Node *node = list->tail;
  120.  
  121. /* append the item to the end of the list */
  122. if (SLL_empty(list))
  123. {
  124. SLL_push(list, item);
  125. }
  126. else
  127. {
  128. if (node->rindx == list->ncap - 1)
  129. {
  130. node = Node_new(list->ncap, 0, -1);
  131. list->tail->next = node;
  132. list->tail = node;
  133. }
  134.  
  135. node->rindx++;
  136. node->items[node->rindx] = item;
  137. }
  138. }
  139.  
  140. int main()
  141. {
  142. int i;
  143. struct List list = SLL_new(3);
  144. for (i = 1; i < 12; ++i)
  145. {
  146. SLL_push(&list, i);
  147. SLL_append(&list, i);
  148. }
  149. while (!SLL_empty(&list))
  150. {
  151. printf("%d ", SLL_pop(&list));
  152. }
  153. printf("\n");
  154. return 0;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement