answer07.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. }
  35. /**
  36. * Create and initialize a linked list.
  37. *
  38. * Arguments:
  39. * value The "value" of the new node
  40. * index The "index" of the new node
  41. *
  42. * Returns:
  43. * Returns a newly constructed node. The node can be the head of a
  44. * linked list.
  45. *
  46. * You should allocate memory in this function.
  47. */
  48. Node * List_create(int value, int index)
  49. {
  50. return NULL;
  51. }
  52. /**
  53. * Build a sparse array from the given indices and values with
  54. * specific length.
  55. *
  56. * Arguments:
  57. * value Array of values
  58. * index Array of indices
  59. * length Length of the above arrays
  60. *
  61. * Returns:
  62. * A sparse array.
  63. *
  64. * If a sparse array node has { value = 1000, index = 2 }, then that means that
  65. * the index "2" caries the value "1000". This is meant to convey an array of 1000
  66. * "2s", but instead of creating 1000 nodes in your linked list, you only create
  67. * 1 node, and that note conceptually has 1000 "copies" of it. Hence
  68. * each node in a sparse array has a "value" in addition to its "index".
  69. *
  70. * Note that an index can never carry the value of "0", because this would not make
  71. * any sense; however, negative values are fine. A negative value may seem odd
  72. * at first blush; however, this is like substraction, and makes sense for certain
  73. * cases.
  74. *
  75. * You need to insert nodes in ascending order by index.
  76. * See the notes to "List_insert_ascend"
  77. */
  78. Node * List_build(int * value, int * index, int length)
  79. {
  80. return NULL;
  81. }
  82. /**
  83. * Inserting "value" and "index" into the correct location in the
  84. * sparse array "head"
  85. *
  86. * Arguments:
  87. * head A pointer pointing to the first element of the linked list.
  88. * value The "value" of the value
  89. * index The "value" of the index
  90. *
  91. * Returns:
  92. * A sparse array
  93. *
  94. * This function inserts the node ["value", "index"] into the sparse
  95. * array "head", and ensures that the nodes remain in ascending order
  96. * by their "index".
  97. *
  98. * Before and after the call to this function, "head" must be in
  99. * ASCENDING order by the "index" of each node.
  100. */
  101. Node * List_insert_ascend(Node * head, int value, int index)
  102. {
  103. return NULL;
  104. }
  105. /**
  106. * This function deletes the node with the passed "index"
  107. *
  108. * Arguments:
  109. * head A pointer pointing to the first element of the sparse array.
  110. * index The index to be deleted
  111. *
  112. * Returns:
  113. * A sparse array with the node removed (the one with index)
  114. */
  115. Node * List_delete(Node * head, int index)
  116. {
  117. return NULL;
  118. }
  119. /**
  120. * Copy a list
  121. *
  122. * Arguments:
  123. * head A pointer pointing to the first element of the sparse array
  124. *
  125. * Returns:
  126. * A copy sparse array
  127. *
  128. * This function will copy the sparse array that is passed to it. The
  129. * copy will be made into new memory.
  130. *
  131. * This is useful, for example, when we want to merge
  132. * two linked lists together. We can make a copy of one of the linked
  133. * lists, and then merge the second into the copy. In this way the
  134. * original copy of the list is not "mutated".
  135. */
  136. Node * List_copy(Node * head)
  137. {
  138. return NULL;
  139. }
  140. /**
  141. * Merged two linked list together
  142. *
  143. * Arguments:
  144. * head1 A pointer pointing to linked list 1
  145. * head2 A pointer pointing to linked list 2
  146. *
  147. * Returns:
  148. * A merged sparse array
  149. *
  150. * This function will merge two linked lists. Before merging, you
  151. * should make a copy of "head1" so that you will still have the
  152. * original array while modifying (merging) the second linked list.
  153. *
  154. * Please refer to the README file for detailed instructions on how to
  155. * merge two lists.
  156. *
  157. * This function should not modify either "head1" or "head2". You only
  158. * need to make a clone of "head1".
  159. */
  160. Node * List_merge(Node * head1, Node * head2)
  161. {
  162. return NULL;
  163. }