Question

In: Computer Science

Create in Java Create a stack class to store integers and implement following methods: 1- void...

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

Solutions

Expert Solution

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:


Related Solutions

Code in Java Create a stack class to store integers and implement following methods: 1) void...
Code 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)....
Program in Java Create a stack class to store integers and implement following methods: 1- void...
Program 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)....
Program in Java Create a queue class to store integers and implement following methods: 1- void...
Program in Java 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,...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class Stack{ public Stack(){ // use LinkedList class } public void push(int item){ // push item to stack } public int pop(){ // remove & return top item in Stack } public int peek(){ // return top item in Stack without removing it } public boolean isEmpty(){ // return true if the Stack is empty, otherwise false } public int getElementCount(){ // return current number...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for managing a singly linked list that cannot contain duplicates. Default constructor Create an empty list i.e., head is null. boolean insert(int data) Insert the given data into the end of the list. If the insertion is successful, the function returns true; otherwise, returns false. boolean delete(int data) Delete the node that contains the given data from the list. If the deletion is successful, the...
0. Introduction. In this assignment you will implement a stack as a Java class, using a...
0. Introduction. In this assignment you will implement a stack as a Java class, using a linked list of nodes. Unlike the stack discussed in the lectures, however, your stack will be designed to efficiently handle repeated pushes of the same element. This shows that there are often many different ways to design the same data structure, and that a data structure should be designed for an anticipated pattern of use. 1. Theory. The most obvious way to represent a...
In simple Java language algorithm: Implement a static stack class of char. Your class should include...
In simple Java language algorithm: Implement a static stack class of char. Your class should include two constructors. One (no parameters) sets the size of the stack to 10. The other constructor accepts a single parameter specifying the desired size of the stack a push and pop operator an isEmpty and isFull method . Both return Booleans indicating the status of the stack Using the stack class you created in problem 1), write a static method called parse that parses...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Java: Create a class and name it MyArray and implement following method. * NOTE: if you...
Java: Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items in the...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT