In: Computer Science
Create in Java
Create a stack class to store integers and implement following methods:
1- void push(int num): This method will push an integer to the top of the stack.
2- int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1.
3- void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value).
4- Boolean isEmpty(): This method will check the stack and if it is empty, this will return true, otherwise false.
Create a queue class to store integers and implement following methods:
1- void enqueue(int num): This method will add an integer to the queue (end of the queue).
2- int dequeue(): This method will return the first item in the queue (First In First Out).
3- void display(): This method will display all items in the queue (First item will be displayed first).
4- Boolean isEmpty(): This method will check the queue and if it is empty, this will return true, otherwise false.
Using the Stack and the Queue classes created on previous questions, implement an algorithm to receive a stack with some items stored in it and using the queue reverse the stack.
You can
Example:
Let's say we have following stack, where last item pushed to stack is 1. (5 is the first number pushed to stack):
1, 2, 3, 4, 5
After running your algorithm the stack must be reversed and first item in the stack (top) must be 5 as follow:
5, 4, 3, 2, 1
I have implemented the above-said algorithms to reverse the elements present in the stack using the queue data structure.
//implementing the stack data
structure
class Stack {
private int maxSize;
private int[] stackArray;
private int top;
public Stack(int s) {
maxSize = s;
stackArray = new int[maxSize];
top = -1;
}
//push method for adding elements in
stack
public void push(int num) {
if(top==maxSize)
System.out.print("OverFlow");
else
stackArray[++top] = num;
}
//pop method for removal of elements
from stack
public int pop() {
if(top==-1)
return -1;
return stackArray[top--];
}
//display method to display the
elements in stack
public void display(){
if(isEmpty()){
System.out.print("UnderFlow");
}
else{
for(int i=top; i>=0; i--)
System.out.print(stackArray[i]+" ");
}
}
//isEmpty method to check whether
stack is empty or not
public boolean isEmpty() {
if(top==-1)
return true;
return false;
}
}
//implementing queue data
structure
class Queue {
private int SIZE;
private int[] items;
private int front, rear;
public Queue(int s) {
SIZE=s;
items= new int[SIZE];
front = -1;
rear = -1;
}
public boolean isFull() {
if (front == 0 && rear == SIZE - 1) {
return true;
}
return false;
}
// check if the queue is
empty
public boolean isEmpty() {
if (front == -1)
return true;
else
return false;
}
// insert elements to the
queue
public void enQueue(int element) {
if (isFull()) {
System.out.println("Queue is full");
}
else {
if (front == -1) {
front = 0;
}
rear++;
items[rear] = element;
}
}
// delete element from the
queue
public int deQueue() {
int element;
// if queue is empty
if (isEmpty()) {
System.out.println("Queue is empty");
return (-1);
}
else {
element = items[front];
// if the queue has only one
element
if (front >= rear) {
front = -1;
rear = -1;
}
else {
// mark next element as the
front
front++;
}
return (element);
}
}
// display element of the
queue
void display() {
int i;
if (isEmpty()) {
System.out.println("Empty Queue");
}
else {
for (i = front; i <= rear; i++)
System.out.print(items[i] + " ");
}
}
}
public class MyMain{
public static void main(String[] args) {
Stack theStack = new Stack(10);
Queue theQueue = new Queue(10);
theStack.push(1); //pushing elements
in the stack
theStack.push(2);
theStack.push(3);
theStack.push(4);
theStack.push(5);
theStack.display(); //displaying
elements in the stack
System.out.println();
while(!theStack.isEmpty()){ //enqueue
the elements of stack in queue
int a=theStack.pop();
theQueue.enQueue(a);
}
while(!theQueue.isEmpty()){ //pushing
elements in the stack from the queue
int a=theQueue.deQueue();
theStack.push(a);
}
theStack.display(); //displaying the
reversed stack
}
}
output: