Question

Write a method to reverse the order of elements on stack S using two additional stacks....

Write a method to reverse the order of elements on stack S using two additional stacks. When the method completes, stack S should have the items it originally had, but in reversed order.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

************************************Code*****************************

import java.util.*;

public class StackOp {
    private static Stack reverse(Stack stack) {
        // This function is used to reverse the stack using another two stacks First we will copy the elements of the
        // stack to s1 and then s1 to s2 then s2 to stack Then the stack elements will be reversed
        Stack<Integer> s1 = new Stack<Integer>();// Creating objects for stacks
        Stack<Integer> s2 = new Stack<Integer>();
        int temp;
        while (!stack.empty())s1.push((int) stack.pop());// Moving elements from stack to s1
        while (!s1.empty())s2.push((int) s1.pop());// Moving elements from s1 to s2
        while (!s2.empty())stack.push(s2.pop()); // Moving elements from s2 to stack
        return stack; // Returning stack
    }

    public static void main(String args[]) {
        //This is the driver method for this program here we are creating the stack object and inserting the elements to
        // it and then we are calling stack reverse function then displaying the result
        Scanner in = new Scanner(System.in);
        Stack<Integer> stack = new Stack<Integer>();
        int ch = 0;
        while (true) {
            System.out.println("1-enter\n2-exit");// Taking the input from the user and storing it in the stack
            ch = in.nextInt();
            if (ch == 1) {
                System.out.println("Enter data:");
                stack.push(in.nextInt());
            } else if (ch == 2) {
                break;
            } else {
                System.out.println("Choose the option correctly");
            }
        }
        System.out.println("Before Reverse Stack elements");
        System.out.println(stack);
        stack = reverse(stack);
        System.out.println("After Reverse Stack elements");
        System.out.println(stack);
    }
}

************************************Code Screens*****************************

************************************Output Screens*****************************

Add a comment
Know the answer?
Add Answer to:
Write a method to reverse the order of elements on stack S using two additional stacks....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • STACK Data Structure using JAVA 1. In a FILO structure, elements are processed in reverse order, ...

    STACK Data Structure using JAVA 1. In a FILO structure, elements are processed in reverse order, in which they arrive. Suppose during execution, an element of the stack becomes least important in terms of priority and has to be but at the bottom of the stack. Method Signature: public void decreasePriority(<AnyType> el) Write an operator that places a certain element of the stack at the bottom of the stack.

  • Consider two sorted stacks S1 and S2 (min on top) including comparable elements. Write a method...

    Consider two sorted stacks S1 and S2 (min on top) including comparable elements. Write a method to merge S1 and S2 and create one stack that is sorted (min on top). You are allowed to use stacks as extra space. Note: stack operations include push, pop, top and empty.

  • Program in Netbeans Write a method Stack Merge (Stack p1, Stack p2) to return a stack...

    Program in Netbeans Write a method Stack Merge (Stack p1, Stack p2) to return a stack with merged elements from both stacks without duplicate. Elements of the stack are objects. Complete the main program Q2.java that uses two stacks with random numbers and displays all the stacks p0, p1 and p2 before and after calling the method including the resulted stack p0.

  • Stacks There are two main operations associated with stacks; 1) putting things on the stack which...

    Stacks There are two main operations associated with stacks; 1) putting things on the stack which is referred to as push, 2) taking things from the stack which is referred to as pop.   We can create a stack using linked lists if we force ourselves to insert and remove nodes only at the top of the list. One use of a stack is when you want to write a word backward. In that case, you will read the letters of...

  • Write a program that uses a stack to reverse its inputs. Your stack must be generic...

    Write a program that uses a stack to reverse its inputs. Your stack must be generic and you must demonstrate that it accepts both String and Integer types. Your stack must implement the following methods: push, pop, isEmpty (returns true if the stack is empty and false otherwise), and size (returns an integer value for the number of items in the stack). You may use either an ArrayList or a LinkedList to implement your stack. Also, your pop method must...

  • Create a Stack class based on java.util.LinkedList class. Your Stack class should have a push(), pop(),...

    Create a Stack class based on java.util.LinkedList class. Your Stack class should have a push(), pop(), peek(), and isEmpy() methods. Create a new Java Application that has the following methods. Write a method reverseChar() to print a sentence in reverse order. Use a Stack to reverse each character. Example: if the user enters a sentence “ABC DEFG”, the program will display “GFED CBA” Write a method reverseWord() to print a sentence reverse order. Use a Stack to reverse each word....

  • Create a Stack class based on java.util.LinkedList class. Your Stack class should have a push(), pop(),...

    Create a Stack class based on java.util.LinkedList class. Your Stack class should have a push(), pop(), peek(), and isEmpy() methods. Create a new Java Application that has the following methods. Write a method reverseChar() to print a sentence in reverse order. Use a Stack to reverse each character. Example: if the user enters a sentence “ABC DEFG”, the program will display “GFED CBA” Write a method reverseWord() to print a sentence reverse order. Use a Stack to reverse each word....

  • Recursively sorting an unbounded stack (java) in ascending order? (This assignment is only limited to stacks...

    Recursively sorting an unbounded stack (java) in ascending order? (This assignment is only limited to stacks only, No other data structures are allowed) My professor gave us a hint on how to implement this, however she wants the comparable interface to be used. im not sure on how to do this. Hint: May want to use a public method that accepts the original stack and then creates the two additional stacks needed. This method then calls a private method that...

  • C++ Data structures and Algorithms Use a Queue to reverse a stack in place. Assume you...

    C++ Data structures and Algorithms Use a Queue to reverse a stack in place. Assume you are given a stack of string values like Stack s; using a Queue, cause all of the items in the stack to be reversed. For example, if you have the following contents on the stack s Top --- bird cat dog turtle ----- Bottom After you run your code, you stack contents should look like Top --- turtle dog cat bird ----- Bottom

  • Page ot 9 2. Stacks/Queues: Write a method splitStack that takes a stack of integers as...

    Page ot 9 2. Stacks/Queues: Write a method splitStack that takes a stack of integers as a parameter and splits it into negatives and non-negatives. The numbers in the stack should be rearranged so that all the negatives appear on the bottom of the stack and all the non-negatives appear on the top. In other words, if after this method is called you were to pop numbers off the stack, you would first get all the nonnegative numbers and then...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT