In: Computer Science
JAVA
public class StringQueue {
//You may NOT add any more fields to this class.
private Stack<String> stack1;
private Stack<String> stack2;
/**
* Initializes an empty queue.
*/
public StringQueue() {
// TODO
throw new RuntimeException("Not
Implemented");
}
/**
* Returns true if this queue is empty.
*
* @return {@code true} if this queue is empty; {@code
false} otherwise
*/
public boolean isEmpty() {
// TODO
throw new RuntimeException("Not
Implemented");
}
/**
* Returns the number of items in this queue.
*
* @return the number of items in this queue
*/
public int size() {
// TODO
throw new RuntimeException("Not
Implemented");
}
/**
* Adds the item to this queue.
*
* @param item the item to add
*/
public void enqueue(String item) {
// TODO
throw new RuntimeException("Not
Implemented");
}
/**
* Removes and returns the item on this queue that was
least recently added.
*
* @return the item on this queue that was least
recently added
* @throws NoSuchElementException if the queue is
empty
*/
public String dequeue() throws NoSuchElementException
{
// TODO
throw new RuntimeException("Not
Implemented");
}
StringQueue.java :
import java.util.*; //import this line to use Stack in code.
public class StringQueue {
//You may NOT add any more fields to this class.
private Stack<String> stack1;
private Stack<String> stack2;
/**
* Initializes an empty queue.
*/
public StringQueue() {
// TODO
//Initializing both stacks
stack1 = new Stack<String>();
stack2 = new Stack<String>();
}
/**
* Returns true if this queue is empty.
*
* @return {@code true} if this queue is empty; {@code false} otherwise
*/
public boolean isEmpty() {
// TODO
/*
if stack1 is empty then queue is empty
*/
if(stack1.isEmpty()) {
return true;
}
return false;
}
/**
* Returns the number of items in this queue.
*
* @return the number of items in this queue
*/
public int size() {
// TODO
/*
stack1 size is the total size of queue
get size of stack1 using sixe() method (Stack built in method to get size of stack)
*/
return stack1.size();
}
/**
* Adds the item to this queue.
*
* @param item the item to add
*/
public void enqueue(String item) {
// TODO
/*
For enqueue we are going to remove all element from stack1 and push all removed elements
in stack2 then push ne item to stack1 and again push back removed element in stack1.
*/
// Move all elements from stack1 to stack2
while (!stack1.isEmpty())
{
stack2.push(stack1.pop());
}
// Push item into Stack stack1
stack1.push(item);
// Push everything back to stack1
while (!stack2.isEmpty())
{
stack1.push(stack2.pop());
}
}
/**
* Removes and returns the item on this queue that was least recently added.
*
* @return the item on this queue that was least recently added
* @throws NoSuchElementException if the queue is empty
*/
public String dequeue() throws NoSuchElementException {
// TODO
// if first stack1 is empty emplies that Queue is empty
//check it first if yes then return null
if (stack1.isEmpty())
{
return null;
}
// Return top of stack1 (top of stack1 is the front element for Queue)
String x = stack1.peek(); //peek() is used to get top element from stack.
stack1.pop();
return x;
}
}
Driver Program to show working of StringQueue class :
Main.java :
import java.util.*; //import this line to use Stack in code.
public class StringQueue {
//You may NOT add any more fields to this class.
private Stack<String> stack1;
private Stack<String> stack2;
/**
* Initializes an empty queue.
*/
public StringQueue() {
// TODO
//Initializing both stacks
stack1 = new Stack<String>();
stack2 = new Stack<String>();
}
/**
* Returns true if this queue is empty.
*
* @return {@code true} if this queue is empty; {@code false} otherwise
*/
public boolean isEmpty() {
// TODO
/*
if stack1 is empty then queue is empty
*/
if(stack1.isEmpty()) {
return true;
}
return false;
}
/**
* Returns the number of items in this queue.
*
* @return the number of items in this queue
*/
public int size() {
// TODO
/*
stack1 size is the total size of queue
get size of stack1 using sixe() method (Stack built in method to get size of stack)
*/
return stack1.size();
}
/**
* Adds the item to this queue.
*
* @param item the item to add
*/
public void enqueue(String item) {
// TODO
/*
For enqueue we are going to remove all element from stack1 and push all removed elements
in stack2 then push ne item to stack1 and again push back removed element in stack1.
*/
// Move all elements from stack1 to stack2
while (!stack1.isEmpty())
{
stack2.push(stack1.pop());
}
// Push item into Stack stack1
stack1.push(item);
// Push everything back to stack1
while (!stack2.isEmpty())
{
stack1.push(stack2.pop());
}
}
/**
* Removes and returns the item on this queue that was least recently added.
*
* @return the item on this queue that was least recently added
* @throws NoSuchElementException if the queue is empty
*/
public String dequeue() throws NoSuchElementException {
// TODO
// if first stack1 is empty emplies that Queue is empty
//check it first if yes then return null
if (stack1.isEmpty())
{
return null;
}
// Return top of stack1 (top of stack1 is the front element for Queue)
String x = stack1.peek(); //peek() is used to get top element from stack.
stack1.pop();
return x;
}
}
Sample Output :
Please refer to the Screenshot of the code below to understand indentation of the code :
Screenshot of StringQueue class
Screenshot of Main class (Driver class)
: