In: Computer Science
Comeputer science .This is java problem.
PROBLEM 1
Queue (Links to an external site.) is an abstract data type (ADT) consisting of a sequence of entities with a first-in-first-out (FIFO) (Links to an external site.) property. Queue has the following operations, in alignment with the Java Queue interface (Links to an external site.) in the Oracle's SDK:
Implement the above FOUR (4) operations in your Solution class as follows. You are responsible for implementing the area shown in red below. Note that you are given TWO (2) local Stacks to help you manage the above listed queue operations.
STARTER CODE
import java.util.Stack; public class HomeworkAssignment5_1 { public static void main(String[] args) { // just like any problems, whatever you need here, etc. } } class Solution { // YOUR STACK TO USE FOR THIS PROBLEM private Stack<Integer> pushStack = new Stack<Integer>(); private Stack<Integer> popStack = new Stack<Integer>(); /* ===================================== /* !!! DO NOT MODIFY ABOVE THIS LINE!!! /* ==================================== // YOUR STYLING DOCUMENTATION HERE public void add(int x) { // YOUR CODE HERE } // YOUR STYLING DOCUMENTATION HERE public int remove() { // YOUR CODE HERE } // YOUR STYLING DOCUMENTATION HERE public int peek() { // YOUR CODE HERE } // YOUR STYLING DOCUMENTATION HERE public boolean isEmpty() { // YOUR CODE HERE } }
EXAMPLES
// TEST CASE #1 Solution sol = new Solution(); sol.add(8); sol.add(1); sol.peek(); // 8 (if you use System.out.println(), for example) sol.remove(); // 8 sol.isEmpty(); // false sol.remove(); // 1 sol.isEmpty(); // true sol.add(2); sol.add(3); sol.peek(); // 2 // TEST CASE #2 // etc.
CONSTRAINTS / ASSUMPTIONS
import java.util.*;
import java.lang.*;
import java.io.*;
class Solution {
// YOUR STACK TO USE FOR THIS PROBLEM
private Stack<Integer> pushStack = new
Stack<Integer>();
private Stack<Integer> popStack = new
Stack<Integer>();
/* =====================================
/* !!! DO NOT MODIFY ABOVE THIS LINE!!!
/* ==================================== */
// This is going to add an elemnt at top of the stack
public void add(int x) {
while (!pushStack.isEmpty())
{
popStack.push(pushStack.pop());
}
pushStack.push(x);
while (!popStack.isEmpty())
{
pushStack.push(popStack.pop());
}
}
// This is going to remove an element from the top of the
stack
public int remove() {
// if first stack is empty
if (pushStack.isEmpty())
{
System.out.println("Q is Empty");
return -1;
}
// Return top of pushStack
int x = pushStack.peek();
pushStack.pop();
return x;
}
// This shows the top most element from the stack wothout popping
it
public int peek() {
Integer peek_element = (Integer) pushStack.lastElement();
return peek_element;
}
// isEmpty is not directly a part of the Stack but rather
implementation from
// Vector. Check if the element exist inside the Stack/Vector or
not.
public boolean isEmpty() {
return pushStack.isEmpty();
}
}
public class HomeworkAssignment5_1
{
public static void main (String[] args) throws
java.lang.Exception
{
// your code goes here
Solution sol = new
Solution();
sol.add(8);
sol.add(1);
System.out.println(sol.peek());
System.out.println(sol.remove());
System.out.println(sol.isEmpty());
System.out.println(sol.remove());
System.out.println(sol.isEmpty());
sol.add(2);
sol.add(3);
System.out.println(sol.peek());
}
}