Question
Assignment1: Lists-Stacks-Queues A queue is an abstract data type where elements are stored in order of insertion and removed in the same order. Because the
Assignment1: Lists-Stacks-Queues
A queue is an abstract data type where elements are stored in order of insertion and removed in the same order. Because the first element that is inserted is the first to be removed, it is often referred to as First-in First-out collection.
TwoStackQueue
You have seen in class, that a queue could be implemented using the java.util.LinkedList
What to do?
-First, you need to implement the MyQueueI
-Then you create the class TwoStackQueue
-Your class should include two constructors:
1. A default constructor TwoStackQueue() to create an empty Queue by creating two empty stacks. Make use of the java.util.Stack
2. TwoStackQueue(List L) to create a Queue from a List (linkedList or ArrayList) by creating an empty out-stack and an in-stack filled with the List elements.
-Implement the methods of the TwoStackQueue class by implementing all the abstract methods declared in the MyQueueI
-You can use the push() and pop() methods from the Stack
-In a main method, test your class thoroughly.
public interface MyQueueI
Interface to specify operations for a simple unbounded first-in-first-out (FIFO) queue.
Method Summary | |
public E | dequeue() Removes and returns the element at the front of this queue. |
public void | enqueue(E element) Adds the specified element to the back of this queue. |
public boolean | isEmpty() Returns true if this queue contains no elements. |
public E | peek() Returns, but does not remove, the element at the front of this queue. |
Coding: (Copy and Paste Source Code here)
public class UseTwoStackQueue {
public static void main(String[] args) {
List
listString.add("One");
listString.add("Two ");
listString.add("Three");
List
linkedString.add("end of linked list");
TwoStackQueue
TwoStackQueue
TwoStackQueue
TwoStackQueue
Q1.enqueue("First"); Q1.enqueue("Second"); Q1.enqueue("Third");
Q2.enqueue("Four");
System.out.println(" The element at the front of Q3" + Q3.peek());
Q4.enqueue(new Integer(200));Q4.enqueue(new Integer(100));
Q4.enqueue(new Integer(500)); Q4.enqueue(50);
System.out.println(" ******** Dequeue the Stack Q1");
while (!Q1.isEmpty()) System.out.print(" "+ Q1.dequeue());
System.out.println(" ******** The first element to dequeue in Q2");
System.out.print(" "+Q2.dequeue());
System.out.println(" ******** Dequeue the Stack Q3");
while (!Q3.isEmpty()) System.out.print(" "+ Q3.dequeue());
System.out.println(" ******** Dequeue the Stack Q4");
while (!Q4.isEmpty()) System.out.print(" "+ Q4.dequeue());
}
}
interface MyQueueI
// Complete and implement your interface here
class TwoStackQueue
// complete and implement your class here
queue dequeue0 enqueue front back
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started