Java: New stack from a portion of the original stack
Write a Java program that implements a stack and creates a new stack from a portion of the original stack.
Sample Solution:
Java Code:
Sample Output:
Original Stack: Stack elements: 8 7 6 5 4 3 2 1 Extract a portion from the said stack: Index Position1 = 1 Index Position2 = 3 Stack elements: 4 3 2 Index Position1 = 3 Index Position2 = 7 Stack elements: 8 7 6 5 4
Flowchart:



For more Practice: Solve these Related Problems:
- Write a Java program to extract a sub-stack from the original stack between two given indices.
- Write a Java program to create a new stack containing the middle portion of an original stack using iterative pop operations.
- Write a Java program to use recursion to copy a specified portion of a stack into a new stack without modifying the original.
- Write a Java program to implement a method that slices a stack into two parts and returns the second part as a new stack.
Live Demo:
Java Code Editor:
Improve this sample solution and post your code through Disqus
Java Stack Previous: Elements from two stacks without duplicates.
Java Stack Exercises Next: Symmetric difference of two stacks.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics