Question

In: Computer Science

Write method reverseStack(Stack s) that receives a stack s and reverse the order of its elements....

Write method reverseStack(Stack s) that receives a stack s and reverse the order of its elements.

the values inside the stack must be changed, that the top will be the last and so on.

please use java code to slove.

Thank you.

Solutions

Expert Solution

Program in java:

import java.util.Stack;
import java.util.Scanner;
public class Main
{
  
public void insert_at_bottom_of_stack(Stack<Integer> stack, int x){
if(stack.isEmpty()){//check if stack is empty
stack.push(x);
return;
}
//take out the existing items in stack and keep it in method stack
int n = stack.pop();
//push x to the bottom of stack
insert_at_bottom_of_stack(stack, x);
//push back all the items from method stack to stack
stack.push(n);
}
public void reverse(Stack<Integer> stack){
if(stack.isEmpty()==false){// if function is not empty
//pop out all the items from the stack and store it in method stack
int n1 = stack.pop();
reverse(stack); //insert the items into stack in reversed order
// by recursivly call reverse method
insert_at_bottom_of_stack(stack, n1);//pop last item from the stack
}
}
  
public static void main(String[] args) {
Stack<Integer> InputStack = new Stack<>();
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of element you want to enter in a stack");
int number=sc.nextInt();
int element;
for(int i=0;i<number;i++){
System.out.println("Enter "+(i+1)+ " elemnt in stack");
element=sc.nextInt();
InputStack.add(element);
}
System.out.println("Input Stack is : " + InputStack);
Main obj = new Main();
obj.reverse(InputStack);
System.out.println("Reversed Stack is: " + InputStack);
}
}

Outout 1:

Output 2:

Program Screenshot:

Method for reverse stack:

public void insert_at_bottom_of_stack(Stack<Integer> stack, int x){
if(stack.isEmpty()){//check if stack is empty
stack.push(x);
return;
}
//take out the existing items in stack and keep it in method stack
int n = stack.pop();
//push x to the bottom of stack
insert_at_bottom_of_stack(stack, x);
//push back all the items from method stack to stack
stack.push(n);
}
public void reverse(Stack<Integer> stack){
if(stack.isEmpty()==false){// if function is not empty
//pop out all the items from the stack and store it in method stack
int n1 = stack.pop();
reverse(stack); //insert the items into stack in reversed order
// by recursivly call reverse method
insert_at_bottom_of_stack(stack, n1);//pop last item from the stack
}
}


Related Solutions

Write a method public static Stack reverse(Stack s) that reverses the order of elements on stack...
Write a method public static Stack reverse(Stack s) that reverses the order of elements on stack s using a Queue. Test your method using some example stacks. In java
Write code to reverse the order of elements on a stack S.c++ ii. Using one additional...
Write code to reverse the order of elements on a stack S.c++ ii. Using one additional queue. iii. using an additional stack and a non array variable c++ .
Without using method size(), write recursive method stackSize(Stack > s1 ) that receives a stack and...
Without using method size(), write recursive method stackSize(Stack > s1 ) that receives a stack and returns number of elements in the stack. The elements in the stack should not be changed after calling this method. please do it in java
Write a Python function that receives a stack object s, where the items in the stack...
Write a Python function that receives a stack object s, where the items in the stack are only numbers in the set {0,1} and returns the number of 1's in the stack s. The function must satisfy the following restrictions: the state of the stack must be preserved; ie., after calling this function the state of the stack s must be the same it was before the function was called. The function cannot use any additional variables of any of...
Write a java program that uses a stack to reverse an integer of three digits. The...
Write a java program that uses a stack to reverse an integer of three digits. The program must prompt the user to input an integer of 3 digits and display it in reverse. - Your program must include the Stack class, the Reverse class, and the Test class. - In the Test class, the program must prompt the user to input a 3-digit number and display it in reverse. - Class Reverse must use the Stack class to reverse the...
The following code was meant to print out the elements in an array in reverse order....
The following code was meant to print out the elements in an array in reverse order. However, it does not behave correctly. public static void reverse(int[] a, int index) {       if (index == (a.length - 1))         System.out.printf("%d%n", a[index]);       else {         reverse(a, index); What does it do? Explain why it behaves in this way and There is more than one error in the code. Correct the code so that it will recursively print out the elements of...
I want to copy all the elements in f1() to f2() but in reverse order (C++)...
I want to copy all the elements in f1() to f2() but in reverse order (C++) this is my code: #include <iostream> #include <iomanip> using namespace std; const int R=10; const int C=10; int Array[R][C] ; ///////////////////////////////////// void f1(){ for(int i=0 ; i<R ; i++){    for(int j=0 ; j<C ; j++){ Array[i][j]= (rand () %100) + 1; } } for(int i=0 ; i<R ; i++){    for(int j=0 ; j<C ; j++){    cout<<"["<<i<<"]["<<j<<"]="<<setw(3)<<Array[i][j]<<" ";    } cout<<endl; }...
c++ code Reverse the order of elements in a vector of integers using two functions: (a)...
c++ code Reverse the order of elements in a vector of integers using two functions: (a) The first function takes the input vector a and return a vector b with the input data but in the reverse order, e.g. input: 1, 2, 3 and output 3, 2, 1. (b) The second function is a recursive one and it reverses the input data in place (without using an additional vector).
C++: Write a reverse function that receives a reference to a integer linked list and reverses...
C++: Write a reverse function that receives a reference to a integer linked list and reverses the order of all the elements in it. For example, if the input linked list is 1 -> 4-> 2-> 3-> 6-> 5}, after processing by this function, the linked list should become 5-> 6-> 3-> 2-> 4-> 1. You need to write a main file to insert elements into the linked list and call the reverseLinkedList() function which takes the reference of first...
Write a java program to reverse element of a stack. For any given word (from input),...
Write a java program to reverse element of a stack. For any given word (from input), insert every character (from the word) into a stack. The output from the stack should be the same as the input. Your program should be using a stack and a queue to complete this process. 1. Push into stack 2. Pop from stack 3. Enqueue into queue 4. Dequeue from queue 5. Push into stack 6. Pop from stack and display java
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT