Stack

Stacks

Stack is a ADT(Abstract Data Type) used to store elements. In stack order in which data arrives is important. Lets take a simple example. Plates are added to stack as they cleaned. So a Stack is an ordered list which insertions and deletions are done at one end LIFO(last in first out) or FILO(first in last out). Naming conventions used in stack are:

push – It is used to insert elements in the stack.

pop – It is used to remove the element from the stack.

peek – Element at the top.

underflow – you are trying to pop an element but stack is empty

overflow – you are trying to add an element into stack when it is full.

Applications of stack are:

  1. In complier
  2. Balancing of symbols.
  3. Infix to postfix expressions.
  4. Matching start and end tags in markup languages.
  5. Page visited history in browser.

Implementation

This implementation of stack uses an array. We add elements from left to right and use a variable to keep track of each index.

Static array:

In static array stack. We manually give array size.It can not be increased by default. You can add validation whether the stack is full or not while poping or pushing data.

 

Dynamic array implementation

Here you have to implement two methods.First is expand() to expand the current stack with some size when it is full and second shrink() to shrink the size when element pop out.

 

Implement stack with Linked List

Insertions of data in linked list is of two types insert at first and insert at last.

Insert at the last

Insert at the first

Full Program is below:

 

Please follow and like us:

Post Author: Ambrish Rajput

Leave a Reply

Your email address will not be published. Required fields are marked *