Stack Applications in Data Stuctures

 

A stack is useful in writing recursive calls.

A stack is useful to convert in fix expression into post fix expression.

It can be used in function calls for example, suppose that function “a” calls function “b” and passes two variables ,”x” and “y” to “b”. Then the function “a” can store these two variables on the stack, from where the function “b” can retrieve and use them.

A stack can be used to evaluate the postfix expression.

A+B=>AB+=>30.

A stack can be used to implement a calculator by storing the operands and the operators on the stack and using them as and when required.

The stack can be used in the algorithms which needs the back traversal.

 

Program: 

Implementation of stack data structures using array

# include <studio.h>

#include <conio.h>

#include <stdlib.h>

#define maxsize 5

int stack [maxsize];

int Top=-1;

void insertion ()

{

Int else;

if (Top== maxsize-1)

{

printf(“stack is over flow \n”);

}

else

{

printf(“Enter an element:”);

scanf (“%d”,&ele);

Top=Top+1;

stack[Top]=ele;

}

}

 

 

void deletion ()

{

if (Top==-1)

{

printf(“Stack is under flow \n”);

}

else

{

printf(“ The deleted element is %d  \n”, stack [Top]);

Top=Top-1;

}

void display()

{

inti;

if (Top==-1)

{

printf(“stack is under flow  \n”);

}

else

{

for(i=0; i<=Top;i++)

{

printf(i=0; i<=Top; stack[i]);

}

printf( “%5d”,stack[i]);

}

 

printf(“\n”);

}

}

int main()

{

int choice;

printf(“1.INSERTION \n”);

printf(“2.DELETION  \n”);

printf(“3.DISPLAY  \n”);

printf(“4.EXIT \n”);

do

{

printf(“Enter your choice:”);

scanf(“%d”,&choice);

switch (choice)

{

case1: insertion (); break;

case2: deletion (); break;

case3: Display(); break;

case4: (0);

}

}

while (c<=4);

return;

}

 

Initial stack:

2

 

Top[-1]  maxsize[5]

After inserting i.e. entry 10, 20, 30, 40, 50.

Top             max size

- 1                            [5]

0

2

3

4

 

 

Inserting element

stack is overflow

After deleting elements 50, 40, 30, 20, 10.

 

3

 

Inserting elements

Stack is over flow

After deleting elements 50,40,30,20,10

4