Queues and Stacks
Purpose: To review queues and stacks in Java, and to use the built-in Stack class and Queue interface.
The Stack class is a generic class containing these methods:
public void push(E value) - pushes a new value onto the Stack
public E pop() - pops the next value off of the stack and returns it; throws EmptyStackExceptionl if stack is empty
public E peek() - returns the next value on the Stack but does not pop it off of the stack; throws EmptyStackExceptionl if stack is empty
public boolean empty() - returns true if the stack is empty; false if not empty.
You can declare a stack with a statement such as:
Stack<Integer> stack1 = new Stack<>();
You can print a stack with the toString() method.
More information on the Stack class may be found at:
https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html
Create a driver program that implements and tests the following method:
public static Stack<Integer> copy(Stack<Integer> s) - The copy method should non-destructively copy a stack of integers into a new stack, and return that new stack.
Test your method by adding code to the main method that creates a stack, pushes items onto the stack, and then calls your copy method. Still within the main method, print both the original stack and the copied stack to verify that the copy method worked as expected.
NOTE: TO RECEIVE FULL CREDIT YOU MAY NOT USE THE BUILT IN COPY METHOD, YOU SHOULD IMPLEMENT IT USING THE ABOVE METHODS
Example:
Before the call to copy, stack1 contains 1, 3, 5
Call the copy method: Stack<Integer> stack2 = copy(stack1);
After call to copy, stack1 contains 1, 3, 5 and stack2 (the copied stack) contains 1, 3, 5
(note that the order of stack2 is the same as stack1)
The Queue interface extends java.util.Collection with the following methods:
public void add(E e) - adds a new value onto the Queue; throws IllegalStateException if no space is currently available
public boolean offer(E e) - adds a new value onto the Queue; returns true if e was added to the queue, returns false if not
public <E> remove() – removes and returns the head of the Queue; throws NoSuchElementException if queue is empty
public <E> poll() – removes and returns the head of the Queue; returns null if queue is empty
public <E> element() – retrieves, but does not remove, the head of the queue; throws NoSuchElementException if queue is empty
You can print a queue with the toString() method.
Declaring a queue: Because the queue is an interface, it may not be instantiated. However, you can create a Queue object and then instantiate it with a LinkedList:
Queue<Integer> myQueue = new LinkedList<>();
More information may be found at: https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html
In your driver program, implement and test the following method:
public static Queue<Integer> evenOddMerge(Queue<Integer> q1, Queue<Integer> q2)
The evenOddMerge method should destructively merge 2 pre sorted queues into a new larger sorted queue such that all the evens are in order then all of the odds are in order. You may assume that the elements in q1 and q2 are in order then the merge method is called.
In the main method of your driver program, create two queues with several items each, print the queues, then call the merge method. Then print the original and merged queues.
Example:
Before the call to merge, q1 contains 1, 2, 4, 6 and q2 contains 0, 1, 2, 3, 5
Call the merge method: Queue<Integer> q3 = evenOddMerge(q1, q2);
After the call to merge, q1 and q2 are empty and q3 contains 0, 2, 2, 4, 6, 1, 1, 3, 5
The project must compile in order to get any credit. Make sure the project is named Week9_Practice_LnameFname then export your Eclipse project to a zip file (File->Export-> General-> Archive File->name the archive Week9_Practice_LnameFname) and upload it to the dropbox in Pilot. Make sure that the correct project in the Project Explorer view is selected when you do the export. You can right-click on it instead of going through the File menu to be sure.
############# StackDriver.java ##############
import java.util.Stack;
/**
* The Class StackDriver.
*/
public class StackDriver {
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
Stack<Integer> s = new
Stack<>();
s.push(1);
s.push(3);
s.push(5);
s.push(9);
System.out.println("Before copy: "
+ s.toString());
Stack<Integer> copyOfStack =
copy(s);
System.out.println("After copy: " +
copyOfStack.toString());
}
/**
* Copy.
*
* @param s the s
* @return the stack
*/
public static Stack<Integer>
copy(Stack<Integer> s) {
Stack<Integer> newStack = new
Stack<>();
for (int i = 0; i < s.size();
i++) {
newStack.push(s.get(i));
}
return newStack;
}
}

############# QueueDriver.java ###############
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
/**
* The Class QueueDriver.
*/
public class QueueDriver {
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
Queue<Integer> q1 = new
LinkedList<>();
q1.add(1);
q1.add(2);
q1.add(4);
q1.add(6);
Queue<Integer> q2 = new
LinkedList<>();
q2.add(0);
q2.add(1);
q2.add(2);
q2.add(3);
q2.add(5);
System.out.println("Before the call
to merge, q1 contains" + q1.toString() + " and q2 contains " +
q2.toString());
Queue<Integer> q3 =
evenOddMerge(q1, q2);
System.out.println("After the call
to merge, q1 and q2 are empty and q3 contains " +
q3.toString());
}
/**
* Even odd merge.
*
* @param q1 the q 1
* @param q2 the q 2
* @return the queue
*/
public static Queue<Integer>
evenOddMerge(Queue<Integer> q1, Queue<Integer> q2)
{
PriorityQueue<Integer> even =
new PriorityQueue<>();
PriorityQueue<Integer> odd =
new PriorityQueue<>();
while (!q1.isEmpty()) {
Integer value =
q1.poll();
if (value % 2 ==
0) {
even.add(value);
}
else {
odd.add(value);
}
}
while (!q2.isEmpty()) {
Integer value =
q2.poll();
if (value % 2 ==
0) {
even.add(value);
}
else {
odd.add(value);
}
}
Queue<Integer> result = new
LinkedList<>();
while (!even.isEmpty()) {
result.add(even.poll());
}
while (!odd.isEmpty()) {
result.add(odd.poll());
}
return result;
}
}

Queues and Stacks Purpose: To review queues and stacks in Java, and to use the built-in...
JAVA Implement a MyQueue class which implements a queue using two stacks. private int maxCapacity = 4; private Stack stack1; private Stack stack2; Note: You can use library Stack but you are not allowed to use library Queue and any of its methods Your Queue should not accept null or empty String or space as an input You need to implement the following methods using two stacks (stack1 & stack2) and also you can add more methods as well: public...
The class pictured below is designed to implement an integer
queue using two stacks. Assume the stack methods all work as
desired (though their implementations are not shown).
.(a) Trace what happens in the following situation, showing
intermediate steps (values of variables, what is in the stacks, and
what is in the queue at various points in the methods, not just the
results of the methods).
• Start with an empty queue. Enqueue 5, then 16, then 7. Dequeue
twice....
2. Stacks and Queues Reinernber: Consider the following function: void systery( int num) { int current: /* create Stack stk and Queue que / Stacks: the function push places the given parameter on the top of the stack, the function pop removes and returns the top of the stack, and the function empty returns true (i.c., 1) if the stack is empty or false (i.e., 0) otherwise. Queues: the function insert places the given parameter on the end of the...
Q1. Add the following operation to the class stackType: void reverseStack (stackType<Type> &otherStack); This operation copies the elements of a stack in reverse order onto another stack. Consider the following statements: stackType<int> stackl; stackType<int> stack2; The statement stackl.reverseStack (stack2); copies the elements of stackl onto stack2 in reverse order. That is, the top element of stackl is the bottom element of stack2, and so on. The old contents of stack2 are destroyed and stack1 is unchanged. Write the definition of...
Create a new Java Application that test MyStack by having the following methods in main class: 1. A method to generate a number of element between two given values and save them in a stack 2. A method to print a stack (10 elements per line). The original stack should remain as is after the print 3. A method to exchange the first element and the last element in a stack 4. A Boolean method to search for a value...
using: class MyQueue<T> { private java.util.LinkedList<T> list; public MyQueue() { list = new java.util.LinkedList<T>(); } public void enqueue(T data) { list.add(data); } public T dequeue() { return list.remove(0); } public T peek() { return list.get(0); } public int size() { return list.size(); } public boolean isEmpty() { return list.isEmpty(); } } class MyQueueTest { public static void main(String[] args) { MyQueue<Integer> queue = new MyQueue<Integer>(); queue.enqueue(3); queue.enqueue(2); queue.enqueue(7); queue.enqueue(1); while (!queue.isEmpty()) { System.out.println(queue.dequeue()); } } } please solve the following:...
Create a Java code that includes all the methods from the
Lecture slides following the ADTs
LECTURE SLIDES
Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS: 4) Queue ADT that uses a linked list internally (call it LQueue) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm....
Description: \\ implement in Java Based on Chapter 6, programming project 1 Hewlett-Packard has a tradition of creating stack-based calculators. Rather than using standard algebraic notation ( 1 + 1 = ), the user would enter the values, then the operator. The calculator had an “enter” key to push each value onto a stack, then pop the stack when an operation key (e.g. “+” or “-”) was pressed. Assignment: Create a Calculator class that implements the provided StackCalculator interface Requirement Constructor: + Calculator() ...
Java.
Must not use Java API java.util.Stack
/**
A class of stacks whose entries are stored in an array.
Implement all methods in ArrayStack class using resizable
array strategy, i.e. usedoubleArray()
Must throw StackException during exception events in methods:
peek(), pop(), ArrayStack(int initialCapacity)
Do not change or add data fields
Do not add new methods
*/
import java.util.Arrays; public class Arraystack«Т> implements Stack!nterface«T> private TI stack;// Array of stack entries private int topIndex; /7 Index of top entry private static...
Java.
Must not use Java API java.util.Stack
/**
A class of stacks whose entries are stored in an array.
Implement all methods in ArrayStack class using resizable
array strategy, i.e. usedoubleArray()
Must throw StackException during exception events in methods:
peek(), pop(), ArrayStack(int initialCapacity)
Do not change or add data fields
Do not add new methods
*/
import java.util.Arrays; public class Arraystack«Т> implements Stack!nterface«T> private TI stack;// Array of stack entries private int topIndex; /7 Index of top entry private...