Flat Preloader Icon

Array implementation

Array Implementation Of Stack

In array implementation, the stack is formed by using the array. All the operations regarding the stack are performed using arrays. Lets see how each operation can be implemented on the stack using array data structure.

Basic example of how to create an array-based stack:

				
					public class ArrayStack {
    private int maxSize;     // Maximum size of the stack
    private int top;         // Index of the top element
    private int[] stackArray;

    // Constructor to initialize the stack
    public ArrayStack(int size) {
        maxSize = size;
        stackArray = new int[maxSize];
        top = -1; // Initialize top
        to -1 to indicate an empty stack
    }

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

    // Check if the stack is full
    public boolean isFull() {
        return top == maxSize - 1;
    }

    // Push an element onto the stack
    public void push(int value) {
        if (isFull()) {
            System.out.println("Stack is full.
            Cannot push element.");
        } else {
            stackArray[++top] = value;
        }
    }

    // Pop an element from the stack
    public int pop() {
        if (isEmpty()) {
            System.out.println("Stack is empty.
            Cannot pop element.");
            return -1; // Return a special value
            to indicate an error
        } else {
            return stackArray[top--];
        }
    }

    // Peek at the top element without removing it
    public int peek() {
        if (isEmpty()) {
            System.out.println("Stack is empty.");
            return -1; // Return a special value
            to indicate an error
        } else {
            return stackArray[top];
        }
    }

    // Get the size of the stack
    public int size() {
        return top + 1;
    }
}

				
			
				
					public class Main {
    public static void main(String[] args) {
        ArrayStack stack = new ArrayStack(5);

        stack.push(1);
        stack.push(2);
        stack.push(3);

        System.out.println
        ("Top element: " + stack.peek());
        // Output: 3

        stack.pop();
        System.out.println
        ("Top element after pop: "
        + stack.peek()); // Output: 2

        System.out.println
        ("Stack size: " + stack.size());
        // Output: 2
    }
}

				
			

Algorithms For The Push, Pop, & Peek Operations Of A Stack

Push Operation: The push operation is used to add an element to the top of the stack.

				
					class Stack {
    private int maxSize; // Maximum size of the stack
    private int top;     // Index of the top element
    private int[] stack; // Array to hold stack elements

    public Stack(int size) {
        maxSize = size;
        stack = new int[maxSize];
        top = -1; 
        
        // Initialize top as -1 to
        //indicate an empty stack.
    }

    // Push an element onto the stack
    public void push(int item) {
        if (top < maxSize - 1) {
            stack[++top] = item; 
            // Increment top and add the
            //item to the stack
        } else {
            System.out.println("Stack is full
            . Cannot push "+ item);
        }
    }
}

				
			

Pop Operation: The pop operation is used to remove and return the top element from the stack.

				
					    // Pop an element from the stack
    public int pop() {
        if (top >= 0) {
            return stack[top--]; 
            // Return the top element and decrement top
        } else {
            System.out.println
            ("Stack is empty. Cannot pop.");
            return -1;
            // -1 can be used as an error code
        }
    }

				
			

Peek Operation: The peek operation is used to view the top element of the stack without removing it.

				
					    // Peek at the top element without removing it
    public int peek() {
        if (top >= 0) {
            return stack[top];
        } else {
            System.out.println
            ("Stack is empty. Cannot peek.");
            return -1;
            // -1 can be used as an error code
        }
    }
}