Question

Using only stack operations push() and pop() reverse the contents of a stack in the Java...

Using only stack operations push() and pop() reverse the contents of a stack in the Java programming language

Homework Answers

Answer #1

CODE

import java.util.Stack;

public class Main {

public static void main(String[] args)

{

Stack<String> stack = new Stack<String>();

stack.push("a");

stack.push("b");

stack.push("c");

stack.push("d");

stack.push("e");

stack.push("f");

ReverseStack(stack);

System.out.println(stack);

}

public static <T> void ReverseStack(Stack<T> stack)

{

for (int i = 0; i < stack.size(); i++)

{

T targetElement = stack.pop();

ReverseStackStep(stack, stack.size() - i, 0, targetElement);

}

}

public static <T> void ReverseStackStep(Stack<T> stack, int limit, int currentLevel, T targetElement)

{

if (currentLevel == limit)

{

stack.push(targetElement);

}

else

{

T element = stack.pop();

ReverseStackStep(stack, limit, currentLevel + 1, targetElement);

stack.push(element);

}

}

}

Know the answer?
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
Similar Questions
Given main() complete the Stack class by writing the methods push() and pop(). The stack uses...
Given main() complete the Stack class by writing the methods push() and pop(). The stack uses an array of size 5 to store elements. The command Push followed by a positive number pushes the number onto the stack. The command Pop pops the top element from the stack. Entering -1 exits the program. Ex. If the input is Push 1 Push 2 Push 3 Push 4 Push 5 Pop -1 the output is Stack contents (top to bottom): 1 Stack...
Suppose that a client performs an intermixed sequence of Stack push and pop operations. The push...
Suppose that a client performs an intermixed sequence of Stack push and pop operations. The push operations put the integers 0 through 9 in order onto the Stack. That is, the following operations must appear in this order with any number of pop operations in between: push(0), push(1), …, push(8), push(9). Each pop operation pops the top item off the Stack and prints the return value. Determine if each of the following sequences can or cannot be a result of...
10.6 LAB: Implement a stack using an array Given main() complete the Stack class by writing...
10.6 LAB: Implement a stack using an array Given main() complete the Stack class by writing the methods push() and pop(). The stack uses an array of size 5 to store elements. The command Push followed by a positive number pushes the number onto the stack. The command Pop pops the top element from the stack. Entering -1 exits the program. Ex. If the input is Push 1 Push 2 Push 3 Push 4 Push 5 Pop -1 the output...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language library LinkedList Stack methods will call the LinkedList methods You can use string as the object Instead of using an array, as the StackLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : push(), pop(), size(), printStackDown(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
JAVA Write a class to implement the Stack ADT using an array as the underlying structure....
JAVA Write a class to implement the Stack ADT using an array as the underlying structure. Implement the pop function so that pop not only removes the top item from the stack, but also reports that value.
Assume you have a stack and a queue implemented using an array of size 4. show...
Assume you have a stack and a queue implemented using an array of size 4. show the content of the array for the stack (then the queue) for the following operations: (for the queue replace push by add and pop by remove; keep in mind that the queue uses a circular array): push(-3), push(-5), push(-9), push(-10), pop(), pop(), push(-13), pop(), push( -15), push(-17).
Using Java, in the most simple algorithm please Implement a static stack class of char. Your...
Using Java, in the most simple algorithm please 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
1). Which of the following statements about the operations of a stack is correct? a). Both...
1). Which of the following statements about the operations of a stack is correct? a). Both pop and push take O(1) time. b). The speed of pop and push depends on the stack size. c). Push is faster than pop. d). Pop is faster than push. 2). For a singly linked list with n nodes to find and remove a node from the list takes how long? a). O( log n ) b). O( n^2 ) c). O( 1 )...
Write a java class program to convert from INFIX TO POSTFIX Using stack operations
Write a java class program to convert from INFIX TO POSTFIX Using stack operations
You are required to write a program in JAVA based on the problem description given. Read...
You are required to write a program in JAVA based on the problem description given. Read the problem description and write a complete program with necessary useful comment for good documentation. Compile and execute the program. ASSIGNMENT OBJECTIVES: • To introduce queue data structure. DESCRIPTIONS OF PROBLEM: Exercise : Write a 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...