Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. ArbitraryQueue Linked List We discussed LIFO and FIFO queues in the lecture s

ID: 3752505 • Letter: 1

Question

1. ArbitraryQueue Linked List We discussed LIFO and FIFO queues in the lecture slides, calling them Stacks and Queues, respectively. For this problem, you will implement a merged stack/ queue data structure that allows for arbitrary insertion at the front or back of the data structure We'll call this the ArbitraryQueue data structure. Your data structure must generalize for any type of object. It will need to support these types of functions a. i. Constructor ArbitraryQueue(), to initialize the queue ii. Push(Object object), inserts at the head of the ArbitraryQueue iii. Enqueue(Object object), inserts at the tail of the ArbitraryQueue iv. Pop(), removes the Object at the head of the ArbitraryQueue and returns it v. Dequeue), removes the Object at the tail of the ArbitraryQueue and returns it vi. Traverse(int index), which retrieves an Object at the required location and returns it Implement error handling with try/catch blocks for potential problems in the queue Make considerations for what can potentially cause errors when a programmer interacts with your APl and do the following: b. i. Use an appropriate Exception that describes the error 1. Example: Accessing a null value should return a NulIPointerException in Java/C++/Python/etc. ii. Print out an error message describing the problem to the user. iii. Important note: The program execution should not fail. We should be able to continue run-time execution regardless of a thrown exception

Explanation / Answer


Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you

ArbitraryQueue.java
-----------------
public class ArbitraryQueue {
class Node{
Object data;
Node next;
Node(Object o){
data = o;
next = null;
}
Node(Object o, Node n){
data = o;
next = n;
}
}
private Node head, tail;
public ArbitraryQueue(){
head = null;
tail = null;
}
public void Push(Object o){
Node n = new Node(o, head);
head = n;
if(tail == null)
tail = n;
}
public void Enqueue(Object o){
Node n = new Node(o);
if(head == null)
head = tail = n;
else
{
tail.next = n;
tail = n;
}
}
public Object Pop(){
if(head == null)
throw new NullPointerException("Queue empty, can't pop");
Object val = head.data;
head = head.next;
if(head == null)
tail = null;
return val;
}
public Object Dequeue(){
if(head == null)
throw new NullPointerException("Queue empty, can't dequeue");
Node prev= null, curr = head;
while(curr != tail){
prev = curr;
curr = curr.next;
}
Object val = tail.data;
if(prev == null) //is it the single node in list? i.e. first node
head = null;
else
prev.next = null;

tail = prev;
return val;
}
public Object Traverse(int index){
Node curr = head;
for(int i = 0; curr != null && i < index; i++){
curr = curr.next;
}
if(curr == null)
throw new IndexOutOfBoundsException("Index " + index + " out of range");
return curr.data;
}
public String toString(){
String s = "{";
Node n = head;
if(n != null){
s += n.data;
n = n.next;
while(n!= null){
s += ", " + n.data;
n = n.next;
}
}
s += "}";
return s;
}
}
TestArbitraryQueue.java
------------
import java.util.Scanner;
public class TestArbitaryQueue {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int choice = 0;
Object s;
ArbitraryQueue queue = new ArbitraryQueue();
int index;
System.out.println("Testing arbitrary queue of strings");
while(choice != 7){
System.out.println("1. Push to head");
System.out.println("2. Pop from head");
System.out.println("3. Enqueue at tail");
System.out.println("4. Deque from tail");
System.out.println("5. Get item at index");
System.out.println("6. Display queue");
System.out.println("7. Quit");
System.out.print("Enter your choice: ");
choice = keyboard.nextInt();
try{
switch(choice){
case 1:
System.out.print("Enter string to push: ");
s = keyboard.next();
queue.Push(s);
break;
case 2:
s = queue.Pop();
System.out.println("Popped " + s);
break;
case 3:
System.out.print("Enter string to enqueue: ");
s = keyboard.next();
queue.Enqueue(s);
break;
case 4:
s = queue.Dequeue();
System.out.println("Dequeued " + s);
break;
case 5:
System.out.print("Enter index of item to fetch: ");
index = keyboard.nextInt();
s = queue.Traverse(index);
System.out.println("Item at index " + index + " is " + s);
break;
case 6:
System.out.println("Queue contains " + queue);
break;
case 7:
break;
default:
System.out.println("Invalid Choice!");
}
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
}


output
-----
Testing arbitrary queue of strings
1. Push to head
2. Pop from head
3. Enqueue at tail
4. Deque from tail
5. Get item at index
6. Display queue
7. Quit
Enter your choice: 1
Enter string to push: apple
1. Push to head
2. Pop from head
3. Enqueue at tail
4. Deque from tail
5. Get item at index
6. Display queue
7. Quit
Enter your choice: 1
Enter string to push: mango
1. Push to head
2. Pop from head
3. Enqueue at tail
4. Deque from tail
5. Get item at index
6. Display queue
7. Quit
Enter your choice: 6
Queue contains {mango, apple}
1. Push to head
2. Pop from head
3. Enqueue at tail
4. Deque from tail
5. Get item at index
6. Display queue
7. Quit
Enter your choice: 3
Enter string to enqueue: watermelon
1. Push to head
2. Pop from head
3. Enqueue at tail
4. Deque from tail
5. Get item at index
6. Display queue
7. Quit
Enter your choice: 6
Queue contains {mango, apple, watermelon}
1. Push to head
2. Pop from head
3. Enqueue at tail
4. Deque from tail
5. Get item at index
6. Display queue
7. Quit
Enter your choice: 5
Enter index of item to fetch: 8
Index 8 out of range
1. Push to head
2. Pop from head
3. Enqueue at tail
4. Deque from tail
5. Get item at index
6. Display queue
7. Quit
Enter your choice: 3
Enter string to enqueue: orange
1. Push to head
2. Pop from head
3. Enqueue at tail
4. Deque from tail
5. Get item at index
6. Display queue
7. Quit
Enter your choice: 6
Queue contains {mango, apple, watermelon, orange}
1. Push to head
2. Pop from head
3. Enqueue at tail
4. Deque from tail
5. Get item at index
6. Display queue
7. Quit
Enter your choice: 2
Popped mango
1. Push to head
2. Pop from head
3. Enqueue at tail
4. Deque from tail
5. Get item at index
6. Display queue
7. Quit
Enter your choice: 6
Queue contains {apple, watermelon, orange}
1. Push to head
2. Pop from head
3. Enqueue at tail
4. Deque from tail
5. Get item at index
6. Display queue
7. Quit
Enter your choice: 4
Dequeued orange
1. Push to head
2. Pop from head
3. Enqueue at tail
4. Deque from tail
5. Get item at index
6. Display queue
7. Quit
Enter your choice: 6
Queue contains {apple, watermelon}
1. Push to head
2. Pop from head
3. Enqueue at tail
4. Deque from tail
5. Get item at index
6. Display queue
7. Quit
Enter your choice: 5
Enter index of item to fetch: 1
Item at index 1 is watermelon
1. Push to head
2. Pop from head
3. Enqueue at tail
4. Deque from tail
5. Get item at index
6. Display queue
7. Quit
Enter your choice: 7