Stack Implementation In Java

Chapter: Data Structures Last Updated: 22-09-2018 08:41:06 UTC

Program:

            /* ............... START ............... */
                
public class Stack {

	private int maxSize;
	private int[] stackArray;
	private int top;

	public Stack(int size) {
		this.maxSize = size; // Stack Size
		this.stackArray = new int[maxSize];
		this.top = -1; // initially we can set top as -1 showing that stack is
						// empty
	}

	public void push(int j) {
		// First we check the stack is full or not
		if (isFull()) {
			System.out.println(" this stack is already full");
		} else {
			top++;
			stackArray[top] = j;
		}

	}

	public int pop() {
		if (isEmpty()) {
			System.out.println("the stack is already empty");
			return 'O';
		} else {
			int old_top = top;
			top--;
			return stackArray[old_top];
		}
	}

	public int peak() {
		return stackArray[top]; // peak will return the top element of stack
								// without removing the top element
	}

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

	public boolean isFull() {
		return (maxSize - 1 == top); // Returns true stack is full
	}

	public static void main(String args[]) {

		Stack theStack = new Stack(4);
		theStack.push(20);
		theStack.push(40);
		theStack.push(60);
		theStack.push(80);

		while (!theStack.isEmpty()) {
			long value = theStack.pop();
			System.out.println(value);
		}

	}
}
                /* ............... END ............... */
        

Output

80
60
40
20

Notes:

  • Java Stack is a legacy Collection class. It extends Vector class with five operations to support LIFO (Last In First Out). We can see stack data structure as brick that pile up on top of each other bricks.During operation we will take the topest brick first and other in order.(As like these bricks stack works).
  • Stack theStack = new Stack(4) - will create a new stack with size four and we initialize top of stack to -1 (For empty stack).
  • In push operation (Adding value), first we will check that stack is full or not. If stack is not full we will increment top by one and place the value at the incremented location.
  • In pop operation, first we check that stack is empty or not if it is empty we return default value saying that stack is empty. Otherwise we will store top index value to a temporary variable and decrement the top value and return the element top index in stack.
  • Five operations extended from vector class are described below.
  • Object push(element) : Pushes (adding an element) on the top of the stack (During push top will increment by one).
  • Object pop() : Removes the top element and return the value at the top of stack.
  • Object peek() : Returns the top element without removing the top element from stack.
  • boolean empty() : Return true when the stack is empty , that is then top = -1 (if top is -1 stack is emtpy).
  • int search(Object element) : This function is used to search the index of an element. If the element is found, it returns the position of the element from the top of the stack. Else, it returns -1.

Tags

Stack Implementation, Java Stack, Data Structures, stack implementation in java using array, stack in java example, stack program in java source code

Similar Programs Chapter Last Updated
HashMap Implementation In Java Data Structures 07-07-2018
Linked List Implementation In Java Data Structures 09-03-2018
Queue Implementation In Java Data Structures 22-09-2018
Binary Search Tree Java Data Structures 11-02-2018
Insertion Sort In Java Data Structures 10-02-2018

1