answer07.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. #include "pa07.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. /**
  5. * Prints a linked-list "head" into the output fie "out"
  6. *
  7. * NOTE: we have given the code for this function
  8. */
  9. void List_print(FILE * out, Node * head)
  10. {
  11. while(head != NULL)
  12. {
  13. fprintf(out, "%5d: %6d\n", head -> index, head -> value);
  14. head = head -> next;
  15. }
  16. printf("\n");
  17. }
  18. /**
  19. * Please fill in the code below
  20. */
  21. /**
  22. * Destroys a linked list.
  23. * Arguments:
  24. * head A pointer pointing to the first element of the linked list.
  25. *
  26. * Returns:
  27. * void
  28. *
  29. * Destroys (frees memory for) the whole linked list.
  30. * You can either use recursion or a while loop.
  31. */
  32. void List_destroy(Node * head)
  33. {
  34. Node *temp = head;
  35. while(head != NULL)
  36. {
  37. temp = temp->next;
  38. free(head);
  39. head = temp;
  40. }
  41. }
  42. /**
  43. * Create and initialize a linked list.
  44. *
  45. * Arguments:
  46. * value The "value" of the new node
  47. * index The "index" of the new node
  48. *
  49. * Returns:
  50. * Returns a newly constructed node. The node can be the head of a
  51. * linked list.
  52. *
  53. * You should allocate memory in this function.
  54. */
  55. Node * List_create(int value, int index)
  56. {
  57. if(value == 0 || index < 0)
  58. return NULL;
  59. Node *temp;
  60. temp = malloc(sizeof(Node));
  61. temp->value = value;
  62. temp->index = index;
  63. temp->next = NULL;
  64. return temp;
  65. }
  66. /**
  67. * Build a sparse array from the given indices and values with
  68. * specific length.
  69. *
  70. * Arguments:
  71. * value Array of values
  72. * index Array of indices
  73. * length Length of the above arrays
  74. *
  75. * Returns:
  76. * A sparse array.
  77. *
  78. * If a sparse array node has { value = 1000, index = 2 }, then that means that
  79. * the index "2" caries the value "1000". This is meant to convey an array of 1000
  80. * "2s", but instead of creating 1000 nodes in your linked list, you only create
  81. * 1 node, and that note conceptually has 1000 "copies" of it. Hence
  82. * each node in a sparse array has a "value" in addition to its "index".
  83. *
  84. * Note that an index can never carry the value of "0", because this would not make
  85. * any sense; however, negative values are fine. A negative value may seem odd
  86. * at first blush; however, this is like substraction, and makes sense for certain
  87. * cases.
  88. *
  89. * You need to insert nodes in ascending order by index.
  90. * See the notes to "List_insert_ascend"
  91. */
  92. void q_sort(int *arr0, int *arr1, int start, int end) // helper function for sort
  93. {
  94. if(end - start < 1) // condition to end recursion
  95. return;
  96. int pivot = start; // set initial pivot index
  97. int val0; // create temporary variable used to swap values
  98. int val1;
  99. int i = start; // incrementor variable
  100. int j = end; // decrementor variable
  101. while(i < j) // prevent i and j from over-lapping
  102. {
  103. while(arr0[i] < arr0[pivot] && i <= end)
  104. {
  105. i++; // find index of value less than pivot value
  106. }
  107. while (arr0[j] > arr0[pivot] && j >= start)
  108. {
  109. j--; // find index of value greater than pivot value
  110. }
  111. val0 = arr0[i]; // swap value at i with value at j
  112. arr0[i] = arr0[j];
  113. arr0[j] = val0;
  114. val1 = arr1[i];
  115. arr1[i] = arr1[j];
  116. arr1[j] = val1;
  117. } // repeat for all values i < j, i.e. one pass
  118. q_sort(arr0, arr1, start, j-1); // repeat for sub array of smaller indices
  119. q_sort(arr0, arr1, j+1, end); // repeat for sub array of larger indices
  120. }
  121. Node * List_build(int * value, int * index, int length)
  122. {
  123. if(length < 1)
  124. return NULL;
  125. q_sort(index, value, 0, length - 1);
  126. Node *head = NULL;
  127. Node *temp = NULL;
  128. int i;
  129. for(i = length - 1; i >= 0; i--)
  130. {
  131. if(value[i] != 0)
  132. {
  133. temp = List_create(value[i], index[i]);
  134. temp->next = head;
  135. head = temp;
  136. }
  137. }
  138. return head;
  139. }
  140. /**
  141. * Inserting "value" and "index" into the correct location in the
  142. * sparse array "head"
  143. *
  144. * Arguments:
  145. * head A pointer pointing to the first element of the linked list.
  146. * value The "value" of the value
  147. * index The "value" of the index
  148. *
  149. * Returns:
  150. * A sparse array
  151. *
  152. * This function inserts the node ["value", "index"] into the sparse
  153. * array "head", and ensures that the nodes remain in ascending order
  154. * by their "index".
  155. *
  156. * Before and after the call to this function, "head" must be in
  157. * ASCENDING order by the "index" of each node.
  158. */
  159. Node * List_insert_ascend(Node * head, int value, int index)
  160. {
  161. if(head == NULL)
  162. return NULL;
  163. Node *temp = NULL;
  164. Node *temp1 = head;
  165. while(temp1 != NULL)
  166. {
  167. if(index < temp1->index)
  168. {
  169. temp = List_create(value, index);
  170. temp->next = temp1;
  171. return temp;
  172. }
  173. if(index == temp1->index)
  174. {
  175. temp1->value = temp1->value + value;
  176. return head;
  177. }
  178. if(temp1->next == NULL)
  179. {
  180. temp = List_create(value, index);
  181. temp->next = temp1->next;
  182. temp1->next = temp;
  183. return head;
  184. }
  185. if(index > temp1->index && index < temp1->next->index)
  186. {
  187. temp = List_create(value, index);
  188. temp->next = temp1->next;
  189. temp1->next = temp;
  190. return head;
  191. }
  192. temp1 = temp1->next;
  193. }
  194. return head;
  195. }
  196. /**
  197. * This function deletes the node with the passed "index"
  198. *
  199. * Arguments:
  200. * head A pointer pointing to the first element of the sparse array.
  201. * index The index to be deleted
  202. *
  203. * Returns:
  204. * A sparse array with the node removed (the one with index)
  205. */
  206. Node * List_delete(Node * head, int index)
  207. {
  208. if(head == NULL)
  209. return NULL;
  210. Node *temp;
  211. temp = head;
  212. Node *temp1;
  213. temp1 = head;
  214. if (temp == NULL)
  215. return temp;
  216. if (head->index == index)
  217. {
  218. if(head->next == NULL)
  219. {
  220. free(head);
  221. return NULL;
  222. }
  223. else
  224. {
  225. head->index = head->next->index;
  226. head->value = head->next->value;
  227. temp = head->next;
  228. head->next = head->next->next;
  229. free(temp);
  230. temp1 = head->next;
  231. }
  232. }
  233. while(temp1->next != NULL)
  234. {
  235. if(temp1->next->index == index)
  236. {
  237. temp = temp1->next;
  238. temp1->next = temp1->next->next;
  239. free(temp);
  240. return head;
  241. }
  242. }
  243. return head;
  244. }
  245. /**
  246. * Copy a list
  247. *
  248. * Arguments:
  249. * head A pointer pointing to the first element of the sparse array
  250. *
  251. * Returns:
  252. * A copy sparse array
  253. *
  254. * This function will copy the sparse array that is passed to it. The
  255. * copy will be made into new memory.
  256. *
  257. * This is useful, for example, when we want to merge
  258. * two linked lists together. We can make a copy of one of the linked
  259. * lists, and then merge the second into the copy. In this way the
  260. * original copy of the list is not "mutated".
  261. */
  262. Node * List_copy(Node * head)
  263. {
  264. if(head == NULL)
  265. return NULL;
  266. Node *temp = head;
  267. Node *copy = malloc(sizeof(Node));
  268. copy->value = temp->value;
  269. copy->index = temp->index;
  270. copy->next = NULL;
  271. Node *const head1 = copy;
  272. temp = temp->next;
  273. while (temp != NULL)
  274. {
  275. copy = copy->next = malloc(sizeof(Node));
  276. copy->value = temp->value;
  277. copy->index = temp->index;
  278. copy->next = NULL;
  279. temp = temp->next;
  280. }
  281. return head1;
  282. }
  283. /**
  284. * Merged two linked list together
  285. *
  286. * Arguments:
  287. * head1 A pointer pointing to linked list 1
  288. * head2 A pointer pointing to linked list 2
  289. *
  290. * Returns:
  291. * A merged sparse array
  292. *
  293. * This function will merge two linked lists. Before merging, you
  294. * should make a copy of "head1" so that you will still have the
  295. * original array while modifying (merging) the second linked list.
  296. *
  297. * Please refer to the README file for detailed instructions on how to
  298. * merge two lists.
  299. *
  300. * This function should not modify either "head1" or "head2". You only
  301. * need to make a clone of "head1".
  302. */
  303. Node * List_merge(Node * head1, Node * head2)
  304. {
  305. if(head1 == NULL)
  306. return List_copy(head2);
  307. if(head2 == NULL)
  308. return List_copy(head1);
  309. Node *temp;
  310. temp = List_copy(head1);
  311. Node *temp1;
  312. temp1 = head2;
  313. while(temp1 != NULL)
  314. {
  315. temp = List_insert_ascend(temp, temp1->value, temp1->index);
  316. temp1 = temp1->next;
  317. }
  318. temp1 = temp;
  319. while(temp != NULL)
  320. {
  321. if(temp->value == 0)
  322. temp = List_delete(temp, temp->index);
  323. temp = temp->next;
  324. }
  325. return temp1;
  326. }