Stack using array implementation. - Coders PlayGround

Learn to code and change the future!

Friday, 4 August 2017

Stack using array implementation.

Stack using array implementation.

Hello friends!!!

Stack is a linear data structure wherein the elements in a stack are added and removed only from one end, which is called as top. Hence, stack is called as Last In First Out (LIFO) data structure as the element inserted last is the first to to be taken out.  


The stack has basically three main operations:
a) Push
b) Pop
c) Peek


A) Push
Push operation is used to add the element into the stack. The new element is added at the topmost position of stack. Here we have to check the following condition:
    a) if TOP = MAX - 1
         This condition means the stack is full and no more elements can be added into the stack. If an attempt is made to insert an element into a stack which is full, an OVERFLOW message is printed.

Now, if the above condition is not satisfied it means that we can add an element into the stack. For this we increment the value of TOP and then insert the element by writing stack[TOP] = value.


b) Pop
Pop operation is used to delete the element from the stack. Note that here the topmost element is deleted. Here we have to check the following condition before deleting an element from a stack:
    a) if TOP == -1
         This condition means that the stack is empty. If an attempt is made to delete an element from the stack then an UNDERFLOW message is printed.

Now, if the above condition is not satisfied then the stack is not empty and we can delete the element from the stack. To delete the topmost element, we decrement the value of top.


c) Peek 
Peek operation is an operation that returns the topmost value of the element. Here the following condition is checked:
    a) if TOP == -1
         This condition means that the stack is empty and hence the message: Stack UNDERFLOW is printed.

Now, if the above condition is not satisfied then we print the topmost value of the stack. The value of stack[top] is printed.


Here is the full program of Stack:


#include<stdio.h>
#define MAX 5
int stack[MAX],top=-1;

void disp()
{
 if(top == -1)
  printf("\n Stack underflow");
 else
 {
  int i;
  printf("\n Elements of the stack are : ");
  for(i=top;i>=0;i--)
   printf(" %d \t",stack[i]);
 }
}
void push()
{
 int val;
 if(top == MAX-1)
  printf("\n Stack overflow");
 else
 {
  printf("\n Enter the no to be pushed : ");
  scanf("%d",&val);
  top++;
  stack[top]=val;
  disp();
 }
}
void pop()
{
 int val;
 if(top == -1)
  printf("\n Stack underflow");
 else
 {
  val = stack[top];
  top--;
  printf("\n Element poped is %d ",val);
  disp();
 }
}
void peek()
{
 if(top == -1)
  printf("\n Stack underflow");
 else
 {
  printf("\n Value : %d",stack[top]);
 }
}
void quit()
{
 printf("\n Thank you!!! \n");
}
void main()
{
 int ch;
 do{
  printf("\n == MENU ==");
  printf("\n 1. PUSH ");
  printf("\n 2. POP ");
  printf("\n 3. PEEK ");
  printf("\n 4. DISPLAY ");
  printf("\n 5. QUIT");
  printf("\n Enter your choice :");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1 : push();
      break;
   case 2 : pop();
      break;
   case 3 : peek();
      break;
   case 4 : disp();
      break;
   case 5 : quit();
      break;
   default : printf("\n Invalid input");
  }
 }while(ch!=5);
}


Output:
Push elements_a

Push elements_b


Push elements_c
Pop Function_a
Pop Function_b
Peek Function
Display Function
Exit Function

Applications of Stack are as follows:

a) Reversing a list
b) Parentheses checker
c) Recursion
d) Tower of Hanoi
e) Postfix to Infix and vice versa






PropellerAds