Question

In: Computer Science

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

Solutions

Expert Solution

/*If you have any query do comment in the comment section else like the solution*/

import java.util.*;
public class ReverseStack {
        public static Stack reverse(Stack s) {
                Queue<Integer> q = new LinkedList<Integer>();
                while(s.size() > 0) {
                        q.add((Integer) s.pop());
                }
                while(q.size() > 0) {
                        s.push(q.poll());
                }
                return s;
        }
        public static void main(String[]args) {
                Stack<Integer> s = new Stack();   
                s.push(1);
                s.push(2);
                s.push(3);
                s.push(4);
                s.push(5);
                System.out.println("Original Stack : " + s);
                System.out.println("Reversed Stack: " + reverse(s));
        }
}

Output:


Related Solutions

Assembly Language. Write a procedure that reverses order of a 1-D array using the stack. The...
Assembly Language. Write a procedure that reverses order of a 1-D array using the stack. The array is a string array, and the address along with the number of elements are passed through the stack. Then write a complete program to test your procedure.
Write, specify and prove the function reverse that reverses an array in place. Take care of...
Write, specify and prove the function reverse that reverses an array in place. Take care of the unmodified part of the array at some iteration of the loop. Assume that the swap function is already proved. Note: Prototype is as below. [7 M] [CO2] void swap(int* a, int* b); void reverse(int* array, size_t len){ }
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...
in Java language, in most simple algorithm Using a stack class, write a static method called...
in Java language, in most simple algorithm Using a stack class, write a static method called parse that parses a String for balanced parentheses. we seek only to determine that the symbol ‘{‘ is balanced with ‘}’. parse accepts a single String parameter and returns an int. If parse returns a minus 1, then there are no errors, otherwise, parse should return the position within the String where an error occurred. For example parse(“{3 + {4/2} }”)   would return -1...
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
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...
Write a complete static method named addUp that accepts, in order, an integer number and an...
Write a complete static method named addUp that accepts, in order, an integer number and an integer thisMany as parameters and that prints a complete line of output reporting all the numbers that result from adding number to itself thisMany number of times (starting at 1 and ending at thisMany). For example, the following calls: addUp(3, 5); addUp(7, 3); should produce this output: Adding up 3 for 5 times gives: 3, 6, 9, 12, 15 Adding up 7 for 3...
Write a method with the signature public static NaturalNumber divRemainder(NaturalNumber m, NaturalNumber n) The method takes...
Write a method with the signature public static NaturalNumber divRemainder(NaturalNumber m, NaturalNumber n) The method takes two NaturalNumbers as arguments and should return a new NaturalNumber whose value is the sum of m divided by n and the remainder of m divided by n. For example, if m = 23 and n = 6, the method should return a NaturalNumber with the value 8, since 23/6 + 23 rem 6 = 3 + 5 = 8. Do not use toInt()...
Write a method with the following header: public static char calculateLetterGrade(int grade, int totalMarks) This method...
Write a method with the following header: public static char calculateLetterGrade(int grade, int totalMarks) This method calculates grade/totalMarks and returns a letter grade based on the following scale: 0-50 = F 51-60 = D 61-70 = C 71-80 = B 81-100 = A
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT