Easy learning with example program codes

Java Stack tutorial

A stack is an ADT – Abstract Data Type or a linear data structure. It is a LIFO data structure because it allows all data operations at one end only i.e. elements can be added and removed from the stack only at the top. LIFO stands for Last-in-first-out. The element which is inserted last, is accessed first.

Stack operations

  • push(): Pushing an element on the stack.
  • pop(): Removing an element from the stack.
  • peek(): Get the top data element of the stack, without removing it.
  • isFull(): Check if stack is full.
  • isEmpty(): Check if stack is empty.

Stack states

Overflow state: A stack is in overflow state if it does not contain enough space to accept an entity to be pushed.
Underflow state: A stack is in underflow state if we want to operate stack with pop operation and the stack is empty.

Stacks efficiency

As we discussed, all operations are done at the top of the stack so no comparisons or moves of elements are necessary. Hence it is very fast.

Java stack examples

Copyright © 2018 CodesJava Protection Status SiteMap