Question

In: Computer Science

Reverse the contents of a stack using only stack operations [ push() and pop()  ]. Using the...

Reverse the contents of a stack using only stack operations [ push() and pop()  ].

Using the java and give the explanation

Solutions

Expert Solution

By using two stack we can reverse stack contents.

For that what I have done is poping element from one stack and pushing that element into another stack.

Stack.java

//This implementation is using a linked list
import java.util.*;
public class Stack{
   Node head = null;
   //class node
   class Node{
       int data;
       Node next;
       //Node constructor
       Node(int d){
           this.data = d;
           this.next = null;
       }
   }
   //Method for pushing an element into stack
   public void push(Stack stack,int data){
       Node newNode = new Node(data);
       if(head==null){
           stack.head = newNode;
       }
       else{
           Node temp = stack.head;
           stack.head = newNode;
           newNode.next = temp;
       }
   }
   //Method for removing top most element from the stack
   public int pop(){
       int element = -1;
       if(head == null){
           System.out.println("Stack is empty");
       }
       else{
           element = head.data;
           head = head.next;
          
       }
       return element;
   }
   //Method for traversing the stack
   public void traverse(Stack stack){
       Node temp = stack.head;
       while(temp!=null){
           System.out.print(temp.data+"\t");
           temp = temp.next;
       }
   }
   //Method for reversing stack
   public void reverse(Stack stack1){
       //Here what i am doing is removing one element from one stack and adding it to the another one
       Stack stack2 = new Stack();//New stack object
       while(stack1.head!=null){
           stack2.push(stack2,stack1.pop());
       }
       System.out.println("\nStack after Reverse");
       stack2.traverse(stack2);
   }
  
   public static void main(String[] args) {
       Stack stack1 = new Stack();
       System.out.print("Enter no.of elements to be inserted into stack:");
       Scanner scan = new Scanner(System.in);
       int n = scan.nextInt();
       for(int i=0;i<n;i++){
           System.out.print("Enter an element:");
           int data = scan.nextInt();
           stack1.push(stack1,data);
       }
      
       //System.out.println(stack1.pop());
       System.out.println("Stack before reverse");
       stack1.traverse(stack1);
       stack1.reverse(stack1);
   }
}

Sample input and output:


Related Solutions

Write a function that returns the largest value in a stack (only use push and pop)
Write a function that returns the largest value in a stack (only use push and pop)
5 marks] A MinStack supports three main operations: the standard Stack operations push(x) and pop() and...
5 marks] A MinStack supports three main operations: the standard Stack operations push(x) and pop() and the non-standard min() operation which returns the minimum value stored on the stack. The zip file gives an implementation SlowMinStack that implements these operations so that push(x) and pop() each run in O(1) time, but  min()runs in Θ(n) time. For this question, you should complete the implementation of FastMinStack that implements all three operations in O(1) time per operation. As part of your implementation, you...
All code should be in Python 3. Implement the Stack Class, using the push, pop, str,...
All code should be in Python 3. Implement the Stack Class, using the push, pop, str, init methods, and the insurance variable 'list'.
Please code in C /* Implements functions that operate on Stack 1. PUSH 2. POP 3....
Please code in C /* Implements functions that operate on Stack 1. PUSH 2. POP 3. isEmpty 4. PEEK 5. Size */ #include <stdio.h> #define CAPACITY 1000 //Two stacks .. for each stack we need // 1. An Array that can hold capacity of elements // 2. A top initialzied to -1 (signifying that the stak is empty at the start) //NOTE : THESE STACKS ARE OF TYPE CHAR :( ... so you need to FIX IT!!!! to int and...
(In Java) Design a stack that supports getMin(), pop() and push() in O(1) time. Must use...
(In Java) Design a stack that supports getMin(), pop() and push() in O(1) time. Must use the iterator and comparator, does not need to be a linked list, although it's what I've tried using. I'm using another tester class to test this class. import java.util.Comparator; import java.util.List; import java.util.LinkedList; import java.util.Iterator; import java.util.Stack; import java.util.ListIterator; public class FMinStack<T> implements MinStack<T> { protected Comparator<? super T> comp; T min; protected List<T> ds; public FMinStack() { this(new DefaultComparator<T>()); } public FMinStack(Comparator<? super...
Problem A. The pseudocode as below illustrates the basic push() and pop() operations of an array-based...
Problem A. The pseudocode as below illustrates the basic push() and pop() operations of an array-based stack, where top is initialized as 0 when the stack is empty. Assuming that at a given moment, SIZE is equal to 20, top is equal to 8, this code is used in a concurrent environment (top and stack[] are in shared memory section), process P0 is about to call push() and process P1 is about to call pop() concurrently a. Which variable has...
how could I implement an intStack class that has only a push and pop method? in...
how could I implement an intStack class that has only a push and pop method? in java of course.
Implement the stack class (called Stack2) using queue, meaning that the only operations that can be...
Implement the stack class (called Stack2) using queue, meaning that the only operations that can be used are the ones defined in the Queue class. class Queue: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def enqueue(self, item): self.items.insert(0, item) def dequeue(self): return self.items.pop() def size(self): return len(self.items) The codes to fill: """ 1. Stack2 class Implement stack data structure using queue """ class Stack2: def __init__(self): # Write your definition for __init__ here def isEmpty(self): #...
Ellie needs a data structure that can handle the following operations: push, pop, contains, remove, where...
Ellie needs a data structure that can handle the following operations: push, pop, contains, remove, where contains(Item x) answers whether a given item is in the data structure and remove(Item x) removes the given item from the data structure. How can this be implemented efficiently?
Suppose an initially empty stack S has performed a total of 15 push operations, 12 top...
Suppose an initially empty stack S has performed a total of 15 push operations, 12 top operations, and 13 pop operations ( 3 of which returned null to indicate an empty stack ). What is the current size of S? Question 1 options: Question 2 (1 point) Saved What values are returned during the following series of stack operations, if executed upon an initially empty stack? push(5), push(3), pop(), push(2), push(8), pop(), pop(), push(9), push(1), pop(), push(7), push(6), pop(), pop(),...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT