Java Program to Implement a Stack Using Array

Introduction

A stack is a linear data structure that follows the Last In, First Out (LIFO) principle, where elements are added (pushed) and removed (popped) from the top of the stack. This guide will walk you through writing a Java program that implements a stack using an array. The stack operations include push, pop, peek, and checking if the stack is empty or full.

Problem Statement

Create a Java program that:

  • Implements a stack using an array.
  • Provides methods to perform standard stack operations: push, pop, peek, isEmpty, and isFull.
  • Displays the stack contents after various operations.

Example:

  • Input: Push elements: 10, 20, 30
  • Operations: push(40), pop(), peek()
  • Output:
    • After pushing 40: [10, 20, 30, 40]
    • After popping: [10, 20, 30]
    • Peeked element: 30

Solution Steps

  1. Create the Stack Structure: Define a Stack class to represent the stack, including methods for standard stack operations.
  2. Implement Stack Operations: Implement methods to push elements onto the stack, pop elements from the stack, peek at the top element, and check if the stack is empty or full.
  3. Handle Edge Cases: Ensure the stack handles underflow (popping from an empty stack) and overflow (pushing onto a full stack) conditions.
  4. Display the Stack: Output the stack’s contents after performing various operations.

Java Program

// Java Program to Implement a Stack Using Array // Author: https://www.rameshfadatare.com/ class Stack { private int maxSize; private int top; private int[] stackArray; // Constructor to initialize the stack public Stack(int size) { maxSize = size; stackArray = new int[maxSize]; top = -1; // Stack is initially empty } // Method to push an element onto the stack public void push(int value) { if (isFull()) { System.out.println("Stack is full. Unable to push " + value); } else { stackArray[++top] = value; System.out.println("Pushed " + value + " onto the stack."); } } // Method to pop an element from the stack public int pop() { if (isEmpty()) { System.out.println("Stack is empty. Unable to pop."); return -1; // Return a sentinel value indicating the stack is empty } else { int poppedValue = stackArray[top--]; System.out.println("Popped " + poppedValue + " from the stack."); return poppedValue; } } // Method to peek at the top element of the stack public int peek() { if (isEmpty()) { System.out.println("Stack is empty. Nothing to peek."); return -1; // Return a sentinel value indicating the stack is empty } else { System.out.println("Peeked at the top element: " + stackArray[top]); return stackArray[top]; } } // Method to check if the stack is empty public boolean isEmpty() { return (top == -1); } // Method to check if the stack is full public boolean isFull() { return (top == maxSize - 1); } // Method to display the contents of the stack public void display() { if (isEmpty()) { System.out.println("Stack is empty."); } else { System.out.print("Stack contents: "); for (int i = 0; i <= top; i++) { System.out.print(stackArray[i] + " "); } System.out.println(); } } } public class StackDemo { public static void main(String[] args) { Stack stack = new Stack(5); // Create a stack with a maximum size of 5 // Push elements onto the stack stack.push(10); stack.push(20); stack.push(30); stack.display(); // Push another element stack.push(40); stack.display(); // Pop an element from the stack stack.pop(); stack.display(); // Peek at the top element stack.peek(); stack.display(); // Attempt to push more elements to test overflow stack.push(50); stack.push(60); stack.push(70); // This push should trigger an overflow condition stack.display(); // Pop all elements to test underflow stack.pop(); stack.pop(); stack.pop(); stack.pop(); // This pop should trigger an underflow condition stack.display(); } } 

Explanation

Step 1: Initialize the Stack Class

  • The Stack class is used to represent a stack, including methods for standard stack operations.
  • The stack is represented by an integer array (stackArray), where top keeps track of the index of the top element. The maxSize defines the maximum capacity of the stack.

Step 2: Implement Stack Operations

  • Push: Adds an element to the top of the stack if it’s not full. If the stack is full, an overflow message is displayed.
  • Pop: Removes and returns the top element of the stack if it’s not empty. If the stack is empty, an underflow message is displayed.
  • Peek: Returns the top element without removing it. If the stack is empty, an appropriate message is displayed.
  • isEmpty: Checks if the stack is empty by verifying if top is -1.
  • isFull: Checks if the stack is full by verifying if top is maxSize - 1.
  • Display: Prints all elements currently in the stack.

Output Example

Pushed 10 onto the stack. Pushed 20 onto the stack. Pushed 30 onto the stack. Stack contents: 10 20 30 Pushed 40 onto the stack. Stack contents: 10 20 30 40 Popped 40 from the stack. Stack contents: 10 20 30 Peeked at the top element: 30 Stack contents: 10 20 30 Pushed 50 onto the stack. Pushed 60 onto the stack. Stack is full. Unable to push 70 Stack contents: 10 20 30 50 60 Popped 60 from the stack. Popped 50 from the stack. Popped 30 from the stack. Popped 20 from the stack. Stack is empty. Unable to pop. Stack is empty. 

Example with Different Stack Size

If you modify the stack size to 3:

Stack stack = new Stack(3); // Create a stack with a maximum size of 3 

The output will adjust accordingly, demonstrating the stack’s overflow condition when more than 3 elements are pushed.

Conclusion

This Java program demonstrates how to implement a stack using an array, including handling overflow and underflow conditions. The program efficiently manages stack operations, providing fundamental functionality commonly used in various applications. This exercise is valuable for understanding how to implement and manipulate stacks in Java programming.

Leave a Comment

Scroll to Top