Tuesday 2 June 2015

Stacks implementation in Java

Stacks is a ADT in Java which follows 'Last In First Out' principle. This post is a very simple implementation of Stacks in Java.

 

import java.util.Scanner;
class Stack {
    int top;
    int capacity;
    int array[];

    public Stack(int capacity) {
        this.top = -1;
        this.capacity = capacity;
        array = new int[capacity];
    }

    boolean isStackFull() {
        return this.top == capacity - 1;
    }

    boolean isStackEmpty() {   
        return this.top == -1;
    }

    void push(int data) {
        if(!isStackFull()) {
            this.array[++this.top] = data;
        } else {
            System.out.println("The Stack is Full!");
        }
    }

    int pop() {
        if(!this.isStackEmpty()) {
            return this.array[this.top--];
        } else {
            System.out.println("The Stack is Empty!");
            return -1;
        }
    }

    void display() {
        if(this.isStackEmpty()) {
            System.out.println("The Stack is Empty!");
        } else {
            System.out.print("The elements in the stack are: ");
            for (int i = 0; i <= this.top; i++) {
                System.out.print(this.array[i] + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the size of the stack: ");
        Stack stack = new Stack(scanner.nextInt());

        do {
            System.out.println("1: Push");
            System.out.println("2: Pop");
            System.out.println("3: Display");
            System.out.println("4: isStackEmpty");
            System.out.println("5: isStackFull");
            System.out.println("6: Quit");
            System.out.print("Enter the stack operations: ");   

            int choice = scanner.nextInt();

            switch(choice) {
                case 1: System.out.print("Enter the element to be pushed: ");
                        stack.push(scanner.nextInt());
                        break;

                case 2: System.out.println("The element popped is: " + stack.pop());
                        break;

                case 3: stack.display();
                        break;

                case 4: System.out.println("Stack Empty: " + stack.isStackEmpty());
                        break;

                case 5: System.out.println("Stack Full: " + stack.isStackFull());
                        break;

                case 6: break;

                default: System.out.println("Please enter a valid input!");
            }

            System.out.print("Do you want to continue, 'y' / 'n'?: ");
        } while(scanner.next().toLowerCase().charAt(0) == 'y');
    }
}


  • Space Complexity: O(n)
  • Time Complexity(for all the individual operations): O(1)
  • Limitations: Max size of the array must be defined and cannot be changed.
  • Solution: Dynamic Array Implementation

Please Let me know if this could be simplified any further and done more efficiently!

No comments:

Post a Comment