Showing posts with label Stack. Show all posts
Showing posts with label Stack. Show all posts

Sunday, September 20, 2015

Stack

Stack is a linear data structure which follows a particular order in which the operations are performed. Order is LIFO(Last In First Out) or FILO(First In Last Out)

Three main operations of a stack are :

  1. Push
  2. Pop
  3. Peek or Top
Stack


Push
The Push Operation pushes an element into the stack.

Pop
Pop operation removes the top most element in the stack.

Peek
Peek or top operation returns the top most value of the stack.


To understand stack, you can assume a pile of books placed one over the other.To get the last book in the stack, one needs to take away all the books before the last book.


Applications of Stack
  1. Recursion
  2. Tower of Hanoi
  3. Converting Infix to Postfix.


Stack can be implemented in two different ways: 
  1. Stack Implementation using Arrays 
  2. Stack Implementation using Linked list
The implemented codes are carried out into separate blogs in the above given links.

More stack related blogs


Stack Implementation Using Linked list

Implementation of a stack using linked list is dynamic and flexible. Yet the space consumed is high and adding or manipulating the linked list is complex.
Refer to the code for more details. The Code language is C++.

Code:



Output:




Stack Implementation Using Array

This blog will deal with implementation of stack using arrays.The implementation is simple yet we need to allocate a pre-defined size.The allocated size may or may not be utilized completely. This makes the array implementation non-flexible.

Using Array





Output