README 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. Assignment PA07: Linked list with Sparse Array
  2. Given below is the general information regarding Sparse Arrays:
  3. "Consider a data structure called a sparse array: most elements of a
  4. sparse array are 0. This means that most elements do not need to be
  5. stored in the array. Only the elements whose values are non-zero are
  6. stored. In a C program, a normal array of size N occupies a contiguous
  7. piece of memory allocated for indexes from 0 to N − 1. It would be
  8. inefficient to store a sparse array in a normal C array because most
  9. space is wasted. Instead, you can use a data structure that stores
  10. only non-zero elements."
  11. Credits given to Yuhao Chen for this general information
  12. /**
  13. * ASSIGNMENT DETAILS
  14. */
  15. YOU ARE EXPECTED TO MAKE YOUR OWN MAKEFILE
  16. For this programming assignment, you will be given two sparse arrays as input.
  17. You are tasked to build a linked list for these two arrays, and then merge them together.
  18. Each linked list node holds two integers: the "index", and the "value".
  19. The linked list nodes should be sorted by their "index"; however, when you read them off
  20. disk they are unsorted. The nodes are sorted by their "index", not their value.
  21. THIS IS IMPORTANT. Furthermore, the "index" should be unique in each list. If two nodes
  22. have the same "index", then you should combine them into one node by summing their "value"s
  23. together. A sparse array should NOT have any index that is negative.
  24. (There is an example following.) Finally, no node can have "value" zero. If
  25. you sum together two nodes and the resulting "value" is zero, then that node should be
  26. removed from the sparse array.
  27. An EXAMPLE will illustrate the concepts on what you are asked to do in this assignment.
  28. You will be given two inputs in the form of a pointer.
  29. each input would contain an array of index and value.
  30. The following example will have this format: (index, value)
  31. Array 1:
  32. (3, 8) (2, -5) (0, 5)
  33. Array 2:
  34. (3, 2) (1, 2) (2, 5)
  35. Then after reading these 2 arrays, you would have to sort them in an ASCENDING
  36. order, with their index as the "key".
  37. Array 1:
  38. (0, 5) (2, -5) (3, 8)
  39. Array 2:
  40. (1, 2) (2,5) (3, 2)
  41. After sorting them, you should make a copy of Array 1.
  42. The reason for this is so that when you merge the two arrays, you will still hold an
  43. original copy of Array 1.
  44. Array 1:
  45. (0, 5) (2, -5) (3, 8)
  46. Array 2:
  47. (1, 2) (2,5) (3, 2)
  48. Copy of Array 1:
  49. (0, 5) (2, -5) (3, 8)
  50. Now the final step is to merge the Array 2 with the Copy of Array 1.
  51. your final spare array should a linked list as follows:
  52. __________ __________ __________
  53. / \ / \ / \
  54. |index = 0| ==\ |index = 1| ==\|index = 3| ==\ NULL
  55. |value = 5| ==/ |value = 2| ==/|value = 10| ==/
  56. \__________/ \__________/ \__________/
  57. Notice that the nodes appear in increasing order by "index". Also, the two "index=3" inputs
  58. have been merged. The two "index=2" inputs were discarded because their "value" summed to zero.
  59. When you insert a new (index, value) pair into the spare-array, you must always preserve
  60. these two properites.
  61. (1) The list is always sorted ascending on "index"
  62. (2) No element has "value" zero. If an insert/merge results in a "value" of zero,
  63. then the node is omitted or removed.
  64. When completing this assignment, the last function that you should attempt is "List_merge".
  65. In this function, you merge two spare-arrays. The output spare-array must obey the two
  66. properties above.
  67. EXAMPLE:
  68. Consider the following scenario:
  69. link1:
  70. (1, 3) ==> (2, 2) ==> (3, -1) ==> (7, -9) ==> (8, -5) ==> (9, 3) ==> NULL
  71. link2:
  72. (0, 5) ==> (1, -3) ==> (2, -8) ==> (3, 1) ==> (4, 7) ==> (7, 9) ==> (9, 1) ==> NULL
  73. results of List_merge(link1, link2):
  74. (0, 5) ==> (2, -6) ==> (4, 7) ==> (8, -5) ==> (9, 4) ==> NULL
  75. Note the result is sorted.
  76. HINT:
  77. It is recommended that you write your own functions.
  78. NOTES:
  79. The input will be in the following format (example taken from input0_A):
  80. 3 --> The length of the array for both the index and value
  81. 1 ----\
  82. 2 ----= These 3 numbers corresponds to the index. They are not necessarily in this ascending order.
  83. 3 ----/
  84. 1 ----\
  85. 1 ----= These 3 numbers corresponds to the value in each respective index.
  86. 1 ----/
  87. This means for this array, it will produce the following (index, value) list:
  88. (1, 1) (2, 1) (3, 1).
  89. Another example is shown below to help better illustrate this process.
  90. Say in your input file you're given the following:
  91. 4
  92. 6
  93. 2
  94. 7
  95. 2
  96. 9
  97. 3
  98. 1
  99. 2
  100. Again, the first number corresponds to the length of the array for both index and values. This means
  101. 4 --> The length
  102. The indexes are:
  103. 6
  104. 2
  105. 7
  106. 2
  107. The values are:
  108. 9
  109. 3
  110. 1
  111. 2
  112. Now at index 6, the value corresponding this index is 9. For index 2, the value corresponding
  113. this index is 3, and so on. So for this input, the (index, value) list should yield:
  114. (6, 9) (2, 3) (7, 1) (2, 2)
  115. /**
  116. *WHAT TO SUBMIT
  117. **/
  118. You should only submit answer07.c.
  119. You are not expected to submit your git log or anything else.