Skip to main content

Stack – Data Structure

Stack is a LIFO (Last in first out) data structure.

Operations

  1. pop() – adds an element in the stack.
  2. push() – removes an element from the stack.
  3. peek() – get the top element without removing it.
  4. isEmpty() – checks whether stack is empty or not.
  5. isFull() – checks whether stack is full or not.

stack

Implementation

There are 2 ways to implement a stack.

  • Array-based Stack
  • Linked List based Stack

Array-based Stack


Pros:-

  • Easier to use.
  • Less memory allocation because no need to track the next node.

Cons:-

  • Fixed size – can not increase or decrease the array.

Link List based Stack


Pros:-

  • Easily increase or decrease the Stack size.

Cons:-

  • Requires extra memory to keep details about next node.

Related Post

494total visits,3visits today

RSS
Follow by Email
Facebook
Facebook
Google+
http://mydevgeek.com/stack-data-strurcture
Twitter