Stack Implementation In Java

Chapter: Data Structures Last Updated: 08-03-2018 18:36:26 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

Similar Programs Chapter Last Updated
Linked List Implementation In Java Data Structures 09-03-2018
Queue Implementation In Java Data Structures 02-03-2018
Binary Search Tree Java Data Structures 11-02-2018
Insertion Sort In Java Data Structures 10-02-2018
Java Stack Example Data Structures 16-10-2017
Java String Array Sort Data Structures 31-03-2017
Java Hashmap Add Key And Value Data Structures 25-03-2017
Java Binary Tree Vertical Sum Data Structures 11-11-2016
Java Binary Tree Boundary Traversal Data Structures 11-11-2016
Java Binary Tree Lowest Common Ancestor (LCA) Data Structures 11-11-2016
Java Binary Tree Maximum Element Data Structures 11-11-2016
Java Three Dimensional Array Data Structures 28-10-2016
Java Infix Expression To Postfix Expression Data Structures 25-10-2016
Java Linked List Add Element First And Last Data Structures 24-10-2016
Java Maximum Element From Vector Data Structures 24-10-2016
Java Binary Search On Vector Data Structures 24-10-2016
Java Get Elements Of LinkedList Data Structures 23-10-2016
Java Linked List Update Element Data Structures 23-10-2016
Java Delete Elements From LinkedList Data Structures 23-10-2016
Java Double Ended Queue Data Structures 09-10-2016
Java Dynamic Queue Using Array Data Structures 07-10-2016
Java Queue Array Based Implementation Data Structures 07-10-2016
Java Sort Short Array Data Structures 25-09-2016
Java Sort Long Array Data Structures 25-09-2016
Java Sort Int Array Data Structures 19-09-2016
Java Sort Float Array Data Structures 19-09-2016
Java Sort Double Array Data Structures 19-09-2016
Java Sort Char Array Data Structures 19-09-2016
Java Sort Byte Array Data Structures 19-09-2016
Java Clear LinkedList Elements Data Structures 09-09-2016

1 2 3