Advertisement
Guest User

stuff

a guest
Jan 22nd, 2019
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.35 KB | None | 0 0
  1. #include "linkedList.h"
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9. #include <stdio.h> /* REMOVE LATER OR FAIL */
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19. /*
  20. What is a linked list?
  21. A linked list is a set of dynamically allocated nodes, arranged in
  22. such a way that each node contains one value and one pointer.
  23. The pointer always points to the next member of the list.
  24. If the pointer is NULL, then it is the last node in the list.
  25.  
  26. A linked list is held using a local pointer variable which
  27. points to the first item of the list. If that pointer is also NULL,
  28. then the list is considered to be empty.
  29. ----------------------------- -------------------------------
  30. | | | \ | | |
  31. | DATA | NEXT |--------------| DATA | NEXT |
  32. | | | / | | |
  33. ------------------------------ ------------------------------
  34. */
  35.  
  36. void insertFront(List_t* list, void* valref) {
  37. node_t** head = &(list->head);
  38.  
  39. node_t* new_node;
  40. new_node = malloc(sizeof(node_t));
  41.  
  42. new_node->value = valref;
  43. new_node->next = *head;
  44.  
  45. *head = new_node;
  46.  
  47. /* bug here: increment length by 1 after adding */
  48. list->length++;
  49. }
  50.  
  51. void insertRear(List_t* list, void* valref) {
  52. node_t* head = list->head;
  53. node_t* current = head;
  54. while (current->next != NULL) {
  55. current = current->next;
  56. }
  57.  
  58. current->next = malloc(sizeof(node_t));
  59. current->next->value = valref;
  60. current->next->next = NULL;
  61. list->length++;
  62. }
  63.  
  64. void insertInOrder(List_t* list, void* valref) {
  65. if (list->length == 0) {
  66. insertFront(list, valref);
  67. return;
  68. }
  69.  
  70. node_t** head = &(list->head);
  71. node_t* new_node;
  72. new_node = malloc(sizeof(node_t));
  73. new_node->value = valref;
  74. new_node->next = NULL;
  75.  
  76. if (list->comparator(new_node->value, (*head)->value) <= 0) {
  77. new_node->next = *head;
  78. *head = new_node;
  79. } else {
  80. node_t* prev = *head;
  81. node_t* current = prev->next;
  82. while (current != NULL) {
  83. if (list->comparator(new_node->value, current->value) > 0) {
  84. if (current->next != NULL) {
  85. prev = current;
  86. current = current->next;
  87. } else {
  88. current->next = new_node;
  89. break;
  90. }
  91. } else {
  92. prev->next = new_node;
  93. new_node->next = current;
  94. break;
  95. }
  96. }
  97. }
  98. list->length++;
  99. }
  100.  
  101. void* removeFront(List_t* list) {
  102. node_t** head = &(list->head);
  103. void* retval = NULL;
  104. node_t* next_node = NULL;
  105.  
  106. if (list->length == 0) {
  107. return NULL;
  108. }
  109.  
  110. next_node = (*head)->next;
  111. retval = (*head)->value;
  112. list->length--;
  113.  
  114. /* bug here: must free the head before reassigning it */
  115. free(*head);
  116.  
  117. *head = next_node;
  118.  
  119. return retval;
  120. }
  121.  
  122. void* removeRear(List_t* list) {
  123. if (list->length == 0) {
  124. return NULL;
  125. } else if (list->length == 1) {
  126. return removeFront(list);
  127. }
  128.  
  129. void* retval = NULL;
  130. node_t* head = list->head;
  131. node_t* current = head;
  132.  
  133. /* bug here: added another ->next to current->next->next */
  134. while (current->next->next != NULL) {
  135. current = current->next;
  136. }
  137.  
  138. retval = current->next->value;
  139. free(current->next);
  140. current->next = NULL;
  141. list->length--;
  142. return retval;
  143. }
  144.  
  145. /* indexed by 0 */
  146. void* removeByIndex(List_t* list, int index) {
  147. if (list->length <= index) {
  148. return NULL;
  149. }
  150.  
  151. node_t** head = &(list->head);
  152. void* retval = NULL;
  153. node_t* current = *head;
  154. node_t* prev = NULL;
  155. int i = 0;
  156.  
  157. if (index == 0) {
  158. return removeFront(list);
  159. /*retval = (*head)->value;
  160. *head = current->next;
  161. free(current);
  162. list->length--;
  163. return retval; - not a bug but can remove this...*/
  164. }
  165.  
  166. /* bug here: changed ++i to i++ */
  167. while (i++ != index) {
  168. prev = current;
  169. current = current->next;
  170. }
  171.  
  172. prev->next = current->next;
  173. retval = current->value;
  174. free(current);
  175. list->length--;
  176.  
  177. return retval;
  178. }
  179.  
  180. void deleteList(List_t* list) {
  181. node_t* head = list->head;
  182.  
  183. while (head != NULL) {
  184.  
  185. /* bug here: remove the element at the front and move the head */
  186. removeFront(list);
  187. head = list->head;
  188. }
  189.  
  190. list->length = 0;
  191. }
  192.  
  193. void printList(List_t* list, char mode) {
  194. node_t* head = list->head;
  195. node_t* current = head;
  196.  
  197. if (list->length == 0) {
  198. printf("List is empty");
  199. return;
  200. }
  201.  
  202. /* bug here: changed current->next to current */
  203. while (current != NULL) {
  204. switch (mode) {
  205. case 0:
  206. printf("%d ", (int)(current->value));
  207. break;
  208. case 1:
  209. printf("%s ", (char*)(current->value));
  210. break;
  211. default:
  212. fprintf(stderr, "Unsupported Mode\n");
  213. exit(1);
  214. }
  215. current = current->next;
  216. }
  217. return;
  218. }
  219.  
  220. int intComparator(void* p, void* q) {
  221. return ((int)p - (int)q);
  222. }
  223.  
  224. /* returns -1 if str1 < str2, 1 if str1 > str2, 0 if str1 == str2 */
  225. int strComparator(void* str1, void* str2) {
  226.  
  227. int i = 0;
  228. char *tmp = str1;
  229. char *tmp2 = str2;
  230.  
  231. /* iterate until a null terminator is reached */
  232. while (*(tmp + i) != '\0' || *(tmp2 + i) != '\0') {
  233.  
  234. if (*(tmp + i) < *(tmp2 + i)) return -1;
  235. if (*(tmp + i) > *(tmp2 + i)) return 1;
  236. i++;
  237. }
  238.  
  239. if (*(tmp + i) == '\0' && *(tmp2 + i) == '\0') return 0;
  240. if (*(tmp + i) == '\0') return -1;
  241. if (*(tmp2 + i) == '\0') return 1;
  242.  
  243. /* should never get here */
  244. return 0;
  245. }
  246.  
  247. void sortElement(List_t* list, void* value) {
  248.  
  249. node_t * current = list->head;
  250. node_t * prev = list->head;
  251.  
  252. /* iterate until you get to a value that is bigger than you */
  253. while (current) {
  254.  
  255. /* handle first node case */
  256. if (list->comparator(value, list->head->value) < 0) {
  257. insertFront(list, value);
  258. return;
  259. }
  260.  
  261. /* if the value < current->value, add it before */
  262. else if (list->comparator(value, current->value) < 0) {
  263.  
  264. /* create a new node */
  265. node_t* new_node;
  266. new_node = malloc(sizeof(node_t));
  267.  
  268. new_node->value = value;
  269. new_node->next = current;
  270.  
  271. prev->next = new_node;
  272. list->length++;
  273.  
  274. return;
  275.  
  276. }
  277.  
  278. /* add to the end of the list */
  279. if (current->next == NULL) {
  280. insertRear(list, value);
  281. return;
  282. }
  283. else {
  284. prev = current;
  285. current = current->next;
  286. }
  287. }
  288.  
  289.  
  290.  
  291. return;
  292. }
  293.  
  294. void sortList(List_t* list) {
  295.  
  296. /* create the new sorted list */
  297. List_t * sortedList = malloc(sizeof(List_t));
  298. sortedList->length = 0;
  299. sortedList->comparator = list->comparator;
  300.  
  301. /* insert the first node */
  302. insertFront(sortedList, (void*)list->head->value);
  303.  
  304. /* start with the 2nd node since the first was added already */
  305. node_t * current = list->head->next;
  306.  
  307. /* iterate through the list and create the new, sorted one */
  308. while (current) {
  309.  
  310. sortElement(sortedList, current->value);
  311. free(current);
  312. current = current->next;
  313. }
  314.  
  315. //deleteList(list);
  316. list->head = sortedList->head;
  317.  
  318. return;
  319. }
  320.  
  321. int removeDups(List_t* list) {
  322.  
  323. /* keeps track of the ptr that moves across the list */
  324. node_t * current = list->head;
  325.  
  326. /* keeps track of the prev node */
  327. node_t * prev = NULL;
  328.  
  329. /* keeps track of the current dup value to test against */
  330. node_t * tmp = list->head;
  331.  
  332. /* iterate through the list */
  333. while (tmp != NULL) {
  334.  
  335. if (current == NULL) {
  336. /* reset the iterator ptr to point at the beginning */
  337. current = list->head;
  338.  
  339. /* move the tmp pointer to the next value */
  340. tmp = tmp->next;
  341. }
  342.  
  343. /* check if value is a duplicate and remove it */
  344. if (tmp->value == current->value
  345. && tmp != current) {
  346.  
  347. prev->next = current->next;
  348. free(current);
  349. list->length--;
  350.  
  351. current = prev->next;
  352.  
  353. // remove duplicates
  354.  
  355. }
  356.  
  357. /* move prev ptr to the current node */
  358. prev = current;
  359.  
  360. /* move the current ptr to the next position */
  361. current = current->next;
  362.  
  363. }
  364.  
  365. return 0; // Change this as needed!
  366. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement