0% found this document useful (0 votes)
5 views5 pages

Midterm 3 Solution

The document outlines basic operations for three data structures: LinkedList, Stack, and Queue, detailing methods such as isEmpty, addHead, push, enqueue, and others. It includes problems that require implementing methods for list intersection, merging queues, and splitting stacks based on even and odd numbers. The document serves as a study guide for understanding and manipulating these data structures.

Uploaded by

111ghostandghist
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Midterm 3 Solution

The document outlines basic operations for three data structures: LinkedList, Stack, and Queue, detailing methods such as isEmpty, addHead, push, enqueue, and others. It includes problems that require implementing methods for list intersection, merging queues, and splitting stacks based on even and odd numbers. The document serves as a study guide for understanding and manipulating these data structures.

Uploaded by

111ghostandghist
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Basic operations for ADT

Basic operations for a LinkedList


isEmpty()//checks if the LinkedList is empty
addHead() //adds an element at the head
addTail //adds an element at the tail
countNodes()//returns the number of nodes
addInPlace() //adds an element while keeping the list sorted
insert() //inserts an element at a specified position
delete() //deletes an element at a specified position
deleteHead() //deletes the element at the head
clear()//deletes all the elements in the list
getHead() //returns a link to the head node
print()//prints all elements in the list
search()//search for an element in the list
equals()//check if two lists are equal
reverse()//reverses the elements in the list
copy()//copies all elements in the list to another list
mergeSorted()//merge two sorted list into a third sorted list

Basic operations for a Stack


isEmpty() //checks if the stack is empty
push()//adds an element at the top
pop()//removes the element at the top and returns it
top()//returns the element at the top without removing it

Basic operations for a Queue of integers


isEmpty()//checks if the queue is empty
enqueue()//adds an element at the tail
dequeue()//removes the element at the head and returns it
head() //returns the element at the head without removing it
tail() //returns the element at the tail without removing it

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 |||

Node curr=head; Suppose we start from position 1


while (curr.next!=null){
curr.next=curr.next.next;
curr=curr.next;
}

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

int i=0, value = 5;


q.enqueue(10);
while (q.head( )!=10){
if (q.head( ) <= value)
i++;
q.enqueue(q.tail());
q.dequeue( );
}
q.dequeue();

give like 1 or 2 pts if the final queue is not fine

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  4592

So the new list L3= 4 18 45

public static LinkedList Intersection ( LinkedList l1, LinkedList l2){


LinkedList l3 = new LinkedList( );2pt
Node curr1 = l1.getHead();2pt
Node curr2 = l2.getHead();2pt
while (curr1!=null&&curr2!=null) 4pts
{
if (curr1.getData()==curr2.getData())2pt// or use compareTo
{
l3.addTail(curr1.getData( )); 4pts
curr1 = curr1.getNext( );2 pt
curr2 = curr2.getNext( );2 pt
}else if (curr1.getData()<curr2.getData())2pt
curr1 = curr1.getNext( );2pt// advance in the list with the smaller value
else// if (curr1.getData() > curr2.getData())2pt
curr2 = curr2.getNext( );2pt// advance in the list with the smaller value
}// end while
return l3;2pt
}// end method

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

The method returns


1 7 3 2 25 19 Q3

public static void mergeMax (queue Q1, queue Q2){ //2pts


queue Q3=new queue() ; //2pts
while( !Q1.isEmpty() && !Q2.isEmpty()){//4pts
if(Q1.head()>Q2.head())//2pts
Q3.enqueue(Q1.head());//2pts
else
Q3.enqueue(Q2.head());//2pts
Q1.dequeue();//2pts
Q2.dequeue();//2pts
}
while( !Q1.isEmpty()){//2pts
Q3.enqueue(Q1.head());//2pts
Q1.dequeue();//2pts
}
while( !Q2.isEmpty()){//2pts
Q3.enqueue(Q2.head());//2pts
Q2.dequeue();//2pts
}
}

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.

Example: if the stack contains

6
7 7
2 After calling split we will have 3
4 4
3 2
6

public static Stack splitStack(Stack s){

Queue Q=new Queue(); 2pt


Stack finalST = new Stack();2pt
NodeData curr; 2pt

while(!s.isEmpty()){ 4pts
curr=s.pop(); 2pts
if(curr.getNum()%2==0) 2pts
finalST.push(curr); 4pts
else
Q.enqueue(curr); 4pts
}

//copy the odd elements now to


while(!Q.isEmpty()) 3pts
finalST.push(Q.dequeue());1pts

return finalST; 2pts


}

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)

You might also like