In: Computer Science
Please provide the missng code(parts) for the given code in java. ASAP. import java.util.Stack; class StackQueue<T> implements Queue<T>{ private Stack<T> inStack; private Stack<T> outStack; private int size; public StackQueue(){ inStack = new Stack<T> (); outStack = PART(a); size = 0; } public int size(){ return size; } public boolean isEmpty(){ return size==0; } public T first(){ if (size == 0) return null;; if (outStack.empty()) while (!inStack.empty()) outStack.push(inStack.pop()); return PART(b); //return the element at the top of the outStack without removing it from the stack } public void enqueue(T x){ PART(c); //push x to inStack size++; } public T dequeue(){ if (size == 0) return PART(d); if (outStack.empty()) while (!inStack.empty()) outStack.push(inStack.pop()); size--; return PART(e); //Removes and returns the element at the top of the outStack } public String toString(){ Stack<T> temp = new Stack<>(); String ans = "StackQueue: "; if (isEmpty()) return ans;; if (outStack.empty()) while (!inStack.empty()) outStack.push(inStack.pop()); while (!outStack.empty()){ T x = outStack.pop(); ans += (x + " -> "); temp.push(x); } while (!inStack.empty()) outStack.push(inStack.pop()); while (!outStack.empty()){ T x = outStack.pop(); ans += (x + " -> "); temp.push(x); } while (!temp.empty()) outStack.push(temp.pop()); return ans; } public static void main(String[] args){ StackQueue<Integer> q = new StackQueue<>(); q.enqueue(0); System.out.println("first element: " + q.first()); q.enqueue(1); q.enqueue(2); q.dequeue(); System.out.println(q.toString()); System.out.println("first element: " + q.first()); q.enqueue(3); q.enqueue(4); q.enqueue(5); System.out.println(q.toString()); q.dequeue(); System.out.println(q.toString()); System.out.println("first element: " + q.first()); } } /** * Interface for a queue: a collection of elements that are inserted * and removed according to the first-in first-out principle. */ interface Queue<T> { /** * Inserts an element at the rear of the queue. * @param e the element to be inserted */ void enqueue(T e); /** * Removes and returns the first element of the queue. * @return element removed (or null if empty) */ T dequeue(); /** * Returns the number of elements in the queue. * @return number of elements in the queue */ int size(); /** * Tests whether the queue is empty. * @return true if the queue is empty, false otherwise */ boolean isEmpty(); /** * Returns, but does not remove, the first element of the queue. * @return the first element of the queue (or null if empty) */ T first(); }
/**
* Interface for a queue: a collection of elements that are inserted
* and removed according to the first-in first-out principle.
*/
import java.util.*;
interface Queue<T> {
/**
* Inserts an element at the rear of the queue.
* @param e the element to be inserted
*/
void enqueue(T e);
/**
* Removes and returns the first element of the queue.
* @return element removed (or null if empty)
*/
T dequeue();
/**
* Returns the number of elements in the queue.
* @return number of elements in the queue
*/
int size();
/**
* Tests whether the queue is empty.
* @return true if the queue is empty, false otherwise
*/
boolean isEmpty();
/**
* Returns, but does not remove, the first element of the queue.
* @return the first element of the queue (or null if empty)
*/
T first();
}
class StackQueue<T> implements Queue<T>{
private Stack<T> inStack;
private Stack<T> outStack;
private int size;
public StackQueue(){
inStack = new Stack<T> ();
outStack = inStack; //Set outStack as inStack at the time of construction
size = 0;
}
public int size(){
return size;
}
public boolean isEmpty(){
return size==0;
}
public T first(){
if (size == 0) return null;// if size is 0 then stack is empty
if (outStack.empty())
while (!inStack.empty())
outStack.push(inStack.pop());
return outStack.peek(); //PART(b) return the element at the top of the outStack without removing it from the stack
//peek()function returns the top element without deleting it
}
public void enqueue(T x){
inStack.push(x); //PART(c) push x to inStack
//Inserting at the end of stack using push() function
size++;
}
public T dequeue(){
if (size == 0) return null;//PART(d)
if (outStack.empty())
while (!inStack.empty())
outStack.push(inStack.pop());
size--;
return outStack.pop(); //PART(e) Removes and returns the element at the top of the outStack
}
public String toString(){
Stack<T> temp = new Stack<>();
String ans = "StackQueue: ";
if (isEmpty()) return ans;;
if (outStack.empty())
while (!inStack.empty())
outStack.push(inStack.pop());
while (!outStack.empty()){
T x = outStack.pop();
ans += (x + " -> ");
temp.push(x);
}
while (!inStack.empty())
outStack.push(inStack.pop());
while (!outStack.empty()){
T x = outStack.pop();
ans += (x + " -> ");
temp.push(x);
}
while (!temp.empty())
outStack.push(temp.pop());
return ans;
}
public static void main(String[] args){
StackQueue<Integer> q = new StackQueue<>();
q.enqueue(0);
System.out.println("first element: " + q.first());
q.enqueue(1);
q.enqueue(2);
q.dequeue();
System.out.println(q.toString());
System.out.println("first element: " + q.first());
q.enqueue(3);
q.enqueue(4);
q.enqueue(5);
System.out.println(q.toString());
q.dequeue();
System.out.println(q.toString());
System.out.println("first element: " + q.first());
}
}
Sample Output
Missing parts
outStack = inStack; //part a
return outStack.peek(); //PART(b) return the element at the top of the outStack without removing it from the stack
//peek()function returns the top element without deleting it
inStack.push(x); //PART(c) push x to inStack
//Inserting at the end of stack using push() function
if (size == 0) return null;//PART(d)
return outStack.pop(); //PART(e) Removes and returns the element at the top of the outStack