Advertisement
Guest User

Untitled

a guest
Aug 26th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. struct list {
  2. list* prev;
  3. list* next;
  4. }
  5.  
  6. struct element {
  7. int value;
  8. struct list* link;
  9. }
  10.  
  11. void list_insert(list* list, list* element) {
  12. element->prev = list;
  13. element->next = list->next;
  14. list->next = element;
  15. element->next->prev = element;
  16. }
  17.  
  18. // Initialize empty list
  19. struct list mylist;
  20. mylist.next = &mylist;
  21. mylist.prev = &mylist;
  22.  
  23. // Initialize element
  24. struct element e;
  25. e.value = 42;
  26.  
  27. // Add element to list
  28. list_insert(&mylist, &e);
  29.  
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33.  
  34. // element within list
  35. typedef struct element {
  36. int value; // data value
  37. struct element *next; // pointer to next element in list
  38. struct element *prev; // pointer to previous element in list
  39. } element_t;
  40.  
  41. // list of elements
  42. typedef struct list {
  43. element_t *head; // pointer to first element in list
  44. element_t *tail; // pointer to last element in list
  45. } list_t;
  46.  
  47. // new_element -- get new element
  48. element_t *
  49. new_element(int value)
  50. {
  51. element_t *newptr;
  52.  
  53. newptr = calloc(1,sizeof(element_t));
  54.  
  55. if (newptr == NULL) {
  56. printf("new_element: malloc failuren");
  57. exit(1);
  58. }
  59.  
  60. newptr->value = value;
  61.  
  62. return newptr;
  63. }
  64.  
  65. // list_insert -- insert before list head
  66. void
  67. list_insert(list_t *list,element_t *newptr)
  68. {
  69. element_t *head;
  70.  
  71. head = list->head;
  72.  
  73. newptr->prev = NULL;
  74. newptr->next = head;
  75.  
  76. // insert at head of empty list
  77. if (head == NULL)
  78. list->tail = newptr;
  79.  
  80. // insert at head of non-empty list
  81. else {
  82. newptr->next = head;
  83. head->prev = newptr;
  84. }
  85.  
  86. // make new element the head of the list
  87. list->head = newptr;
  88. }
  89.  
  90. // list_insert_unique -- insert before list head
  91. void
  92. list_insert_unique(list_t *list,element_t *newptr)
  93. {
  94. element_t *curptr;
  95.  
  96. // search for pre-existing element with the same value
  97. for (curptr = list->head; curptr != NULL; curptr = curptr->next) {
  98. if (curptr->value == newptr->value)
  99. break;
  100. }
  101.  
  102. // only insert if we did _not_ find one
  103. if (curptr == NULL)
  104. list_insert(list,newptr);
  105. else
  106. free(newptr);
  107. }
  108.  
  109. // list_print -- print the list
  110. void
  111. list_print(list_t *list)
  112. {
  113. element_t *curptr;
  114.  
  115. printf("list_print:");
  116.  
  117. // search for pre-existing element with the same value
  118. for (curptr = list->head; curptr != NULL; curptr = curptr->next)
  119. printf(" %d",curptr->value);
  120.  
  121. printf("n");
  122. }
  123.  
  124. // list_print -- print the list in reverse order
  125. void
  126. list_rprint(list_t *list)
  127. {
  128. element_t *curptr;
  129.  
  130. printf("list_rprint:");
  131.  
  132. // search for pre-existing element with the same value
  133. for (curptr = list->tail; curptr != NULL; curptr = curptr->prev)
  134. printf(" %d",curptr->value);
  135.  
  136. printf("n");
  137. }
  138.  
  139. // main -- main program
  140. int
  141. main(void)
  142. {
  143. list_t mylist;
  144. element_t *e;
  145.  
  146. // initialize list
  147. mylist.head = NULL;
  148. mylist.tail = NULL;
  149.  
  150. // Add unique element to list
  151. e = new_element(42);
  152. list_insert_unique(&mylist,e);
  153.  
  154. e = new_element(23);
  155. list_insert_unique(&mylist,e);
  156.  
  157. e = new_element(17);
  158. list_insert_unique(&mylist,e);
  159.  
  160. // try to insert a duplicate
  161. e = new_element(17);
  162. list_insert_unique(&mylist,e);
  163.  
  164. // try to insert a duplicate
  165. e = new_element(42);
  166. list_insert_unique(&mylist,e);
  167.  
  168. // print list in order [checks the head/next pointers]
  169. list_print(&mylist);
  170.  
  171. // print list in reverse order [checks the tail/prev pointers]
  172. list_rprint(&mylist);
  173.  
  174. return 0;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement