w3resource

Java: New stack from a portion of the original stack


24. Create a 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:

import java.util.Scanner;

public class Stack {
  private int[] arr;
  private int top;

  // Constructor to initialize the stack
  public Stack(int size) {
    arr = new int[size];
    top = -1;
  }

  // Method to push an element onto the stack
  public void push(int num) {
    if (top == arr.length - 1) {
      System.out.println("Stack is full");
    } else {
      top++;
      arr[top] = num;
    }
  }

  // Method to pop an element from the stack
  public int pop() {
    if (top == -1) {
      System.out.println("Stack Underflow");
      return -1;
    } else {
      int poppedElement = arr[top];
      top--;
      return poppedElement;
    }
  }

  // Method to get the top element of the stack
  public int peek() {
    if (top == -1) {
      System.out.println("Stack is empty");
      return -1;
    } else {
      return arr[top];
    }
  }

  // Method to check if the stack is empty
  public boolean isEmpty() {
    return top == -1;
  }

  // Method to get the size of the stack
  public int getSize() {
    return top + 1;
  }

  // Method to create SubStack
  public Stack slice(int start, int end) {
    if (start < 0 || end >= arr.length || start > end) {
      System.out.println("Invalid indices");
      return null;
    }

    int newSize = end - start + 1;
    Stack subStack = new Stack(newSize);

    for (int i = start; i <= end; i++) {
      subStack.push(arr[i]);
    }

    return subStack;
  }

  // Method to display the elements of the stack
  public void display() {
    if (top == -1) {
      System.out.println("Stack is empty!");
    } else {
      System.out.print("Stack elements: ");
      for (int i = top; i >= 0; i--) {
        System.out.print(arr[i] + " ");
      }
      System.out.println();
    }
  }

  public static void main(String[] args) {

    Stack stack1 = new Stack(8);
    Stack result = new Stack(8);
    stack1.push(1);
    stack1.push(2);
    stack1.push(3);
    stack1.push(4);
    stack1.push(5);
    stack1.push(6);
    stack1.push(7);
    stack1.push(8);
    System.out.println("Original Stack:");
    stack1.display();
    System.out.println("\nExtract a portion from the said stack:");
    int index_pos1 = 1;
    int index_pos2 = 3;
    System.out.println("\nIndex Position1 = " + index_pos1 + " Index Position2 = " + index_pos2);
    result = stack1.slice(index_pos1, index_pos2);
    result.display();
    index_pos1 = 3;
    index_pos2 = 7;
    System.out.println("\nIndex Position1 = " + index_pos1 + " Index Position2 = " + index_pos2);
    result = stack1.slice(index_pos1, index_pos2);
    result.display();
  }
}

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:

Flowchart: Java  Exercises: New stack from a portion of the original stack.


Flowchart: Java  Exercises: New stack from a portion of the original stack.


Flowchart: Java  Exercises: New stack from a portion of the original stack.


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.

Go to:


PREV : Create a new stack without duplicates from two stacks.
NEXT : Create a new stack with elements in either stack but not both.

Live Demo:

Java Code Editor:

Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.