Stack Abstract Data Structure with Array Implementation

Stack is a abstract data type which store the elements in a particular order called Last in first out (LIFO), where insertion is called PUSH operation and deletion called POP operation.

The basic operations of stack are:

  1. PUSH insert the element into the stack
  2. POP deletion of the element from the stack
  3. PEEK return the TOP item of the stack
Stack implementation can be possible using:
  1. Linked List
  2. Array
Array implementation of stack  has following advantages and disadvantages:

Advantages: Best Performance
Disadvantage: Fixed Size


Stack Applications
  1. Compiler (parsing the data between the brackets)
  2. Operating system (program stack)
  3. Artificial intelligence(finding the path)
Stack implementation using an array:
  1. First define the size of the array
  2. Take a index variable called TOP=-1
  3. If TOP==MAX then stack is full
  4. If TOP==-1 stack is empty
  5. PUSH operation is performed with increment of TOP
  6. POP operation is performed with TOP decrements


#include
#include
#define MAX 10

int a[MAX];
int top=-1;

void Push()
{
int i;
if(top==MAX)
{
printf("stack is full");
exit(0);
}
else
{
scanf("%d",&i);
top++;
a[top]=i;
printf("insert is sucessful\n");
}
}

void Pop()
{
if(top==-1)
{
printf("stack is empty");
exit(0);
}
else
{
printf("%d\n", a[top]);
top=top-1;
}
}

void display()
{
if(top==-1)
{
printf("stack is empty");
exit(0);
}
else
{
while(top!=-1)
printf("poped all elements are %d\n",a[top--]);
}
}

int main()
{
int n,c;
while(1)
{
printf("1: Push operation into stack\n");
printf("2: POP operation into stack\n");
printf("3: Display all the pop elements\n");
printf("4: exit\n");
printf("enter the choice");
scanf("%d",&n);
switch(n)
{
case 1:
    printf("enter the data");
    Push();
    break;
case 2: 
   printf("poped elements");
   Pop();
   break;
case 3:
   display();
   break;
case 4:
   exit(0);
default :
   printf("invalid choice");
}
}
}


Comments

Popular posts from this blog

Types of Algorithm and Its Complexity Analysis

First Step of Problem Solving Using Data Structure and Algorithm

Asymptotic Notations