Midterm 3 Solution
Midterm 3 Solution
Page 1 of 5
This study source was downloaded by 100000898874437 from CourseHero.com on 06-01-2025 00:11:37 GMT -05:00
https://www.coursehero.com/file/235370434/Basic-Operations-for-LinkedList-Stack-and-Queue/
Problem 1 [10 points]
Question 1(5 pts.) Given the below Linked List. Show the content of the linked list after the
code is executed and describe in details in the box what the code does.
head 3 6 7 2 9 5 3 |||
Question 2 (5 pts) Given the following queue q, trace (in the box) step by step how the content of the
queue changes in memory after every enqueue and dequeue operation in the code fragment below
as well as how the head (h) and tail (t) move. What is the output of the code?
3 7 1
0 1 2 3 4 5
h t
Page 2 of 5
This study source was downloaded by 100000898874437 from CourseHero.com on 06-01-2025 00:11:37 GMT -05:00
https://www.coursehero.com/file/235370434/Basic-Operations-for-LinkedList-Stack-and-Queue/
Problem2 [30 pts]
Given two linked lists sorted in increasing order, write a method intersection to return a new list
representing the intersection of the two lists. The new list should be made with its own memory — the
original lists should not be changed.
The method will be called like this: LinkedList list3 = list1.intersection(list2);
Example:
If L1 = 4 15 18 23 45 78 102
And L2 = 3 4 18 20 4592
Page 3 of 5
This study source was downloaded by 100000898874437 from CourseHero.com on 06-01-2025 00:11:37 GMT -05:00
https://www.coursehero.com/file/235370434/Basic-Operations-for-LinkedList-Stack-and-Queue/
Problem3 [30 pts]
Write a static method mergeMax that takes two queues Q1 and Q2 and returns a new queue containing
the highest values when comparing the numbers in the heads of the two queues Q1 and Q2. Note that
Q1 and Q2 may not have the same number of elements. More precisely, you need to compare the
elements in Q1 and Q2 one by one and you store the max in the new queue.
In the below example, the head of Q1 contains 9 while the head of Q2 contains 19.
1 7 3 2 5 9 Q1
1 0 25 19 Q2
Page 4 of 5
This study source was downloaded by 100000898874437 from CourseHero.com on 06-01-2025 00:11:37 GMT -05:00
https://www.coursehero.com/file/235370434/Basic-Operations-for-LinkedList-Stack-and-Queue/
Problem 4 [30 pts]
Write a method splitStack that takes a stack of integers as a parameter and splits it (in a new
Stack) into even and odd. The original stack remains unchanged. The numbers in the new stack should
be rearranged so that all the even numbers appear on the bottom of the stack and all the odd numbers
appear on the top. You should use only one single queue as a temporary storage.
6
7 7
2 After calling split we will have 3
4 4
3 2
6
while(!s.isEmpty()){ 4pts
curr=s.pop(); 2pts
if(curr.getNum()%2==0) 2pts
finalST.push(curr); 4pts
else
Q.enqueue(curr); 4pts
}
Page 5 of 5
This study source was downloaded by 100000898874437 from CourseHero.com on 06-01-2025 00:11:37 GMT -05:00
https://www.coursehero.com/file/235370434/Basic-Operations-for-LinkedList-Stack-and-Queue/
Powered by TCPDF (www.tcpdf.org)