Professional Documents
Culture Documents
Algo (2024) Homework 03
Algo (2024) Homework 03
Algo (2024) Homework 03
Question 2. Design an algorithm that takes as its inputs two non-empty 1 singly linked lists
L1 and L2 with m and n elements respectively, and returns as its output a new singly linked
list L with m + n elements, satisfying the following properties:
• For each k satisfying 1 ::; k ::; min {m, n} , the key value of the ( 2k - 1)-th element of L
is the maximum key value among the first k elements of L 1, while the key value of the
(2k)-th element of L is the maximum key value among the first k elements of L2 .
• If m > n , then for each k satisfying 1 ::; k ::; (m - n) , the key value of the (m + n + l - k )-th
element is the maximum key value among the first (m + 1 - k) elements of L 1.
• If n > m , then for each k satisfying 1 ::; k ::; (n - m) , the key value of the (m + n + l - k )-th
element is the maximum key value among the first (n + l - k) elements of L 2.
You may assume that each element of the input singly linked lists L1 and L2 has a numerical
key value. Please present your algorithm in pseudocode. For full credit , your algorithm must
access the key value of each element of L1 at most once, and must access the key value of each
element of L 2 at most once. In your algorithm, you should NOT create any new array, and
you should NOT be appending any values to an array. Furthermore, your output L should be
a new singly linked list . The input singly linked lists L 1 and L 2 should still remain t he same
after running your algorithm. Your algorithm should not "destroy" L 1 or L 2. [10 points]
Hint 1: For example, if the singly linked list L1 has 2 elements a, b (starting with the first
element Li.head = a) , whose key values are 5, 2 respectively, and if the singly linked list L2
has 5 elements c, d, e, f, g (st arting with the first element L2.head = c), whose key values are
3, 4, 1, 6, 5 respectively, then your algorithm should return a new linked list L whose elements
(starting with the first element L.head) have key values 5, 3, 5, 4, 4, 6, 6 respectively.
Hint 2: You may directly use the operation SINGLY _LIST _INSERT(L, x ), whose pseudocode is
given below.
Hint 3: Be VERY careful! The SINGLY _LIST _INSERT(L, x) operation inserts a new element x as
the new head of L , not at the "end" of L.
1 A singly linked list is called non-empty if it has at least one element. Remember: A linked list L is called a
singly linked list if every element x of L has attributes x .key and x .next, but in contrast t o a doubly linked list,
there is no x .prev.
1
function SINGLY _LIST _INSERT(£, X)
Require: L is a singly linked list
Require: x is an object with x.key value specified
1: x.next +--- £.head
2: £.head +--- x
Question 3. A doubly linked list L is called sorted in ascending order if for all elements x of
L satisfying x .next-/=- NIL, we always have x.key:::; x. next .key. Similarly, a doubly linked list L
is called sorted in descending order if for all elements x of L satisfying x .next -/=- NIL , we always
have x. key 2 x .next.key. Note that if L is a doubly linked list with at most 1 element, then L
is already sorted in ascending order, as well as sorted in descending order .
Design an algorithm that takes as its inputs two doubly linked list £ 1 and L2 , each of which is
sorted in ascending order, and that returns as its output a doubly linked lists L that is sorted in
descending order, such that the elements in L are exactly all the elements in £ 1 and £ 2 . (So if
L1 has m elements and L2 has n elements, then L must have m + n elements.) Your algorithm
should still work if L1 or L2 or both is/are empty. Please present your algorithm in pseudocode.
Your algorithm must have time complexity O(m + n), where m and n represent the number of
elements in the input doubly linked lists L1 and L2, respectively. Also , your algorithm must
use at most one for/while loop. (To be clear , this means that if part of your pseudocode already
uses a for-loop , then to get full credit, you cannot use another for-loop or while-loop. Similarly,
if part of your pseudocode already uses a while-loop, then to get full credit, you cannot use
another for-loop or while-loop. A solution receiving full credit will not use any iterated for-loops
or iterated while-loops.) Moreover, your algorithm should not use any arrays. [10 points]
Hint 1: It is possible to design an algorithm that uses exactly one while-loop and no for-loops.
Hint 2: It is okay for your algorithm to "destroy" L1 and L2 in the creation of the output
doubly linked list L.
Question 4. Initialize a hash t able A with 5 slots, whose hash function is created using the
division method. Assume that collisions are resolved by chaining. Also , assume that the load
factor cannot exceed 'Y = 0. 7, and that re-hashing is done by doubling. Suppose we insert
the following twelve integer key values into A : 3, 8, 5, 10, 22 , 27, 33, 26, 21 , 34, 42, 1 (in this given
order).
(i) Draw A immediately before A is re-hashed for the second time. [5 points]
(ii) Draw the final hash table after the insertion of all twelve key values. [5 points]
Hint: When drawing a hash table, you should draw all slots, even if some slots contain the NIL
object. Remember also to indicate the indices for your hash table.