Stack Implementation

Description:           This program is Menu driven program to implement Stack.

 
Primary Inputs:    the numbers to be pushed on to the stack
Primary Output:    Stack
Platform Used:      Turbo C++ version 3.0, Borland International Inc.
 

/*program to implement stack */

#include
#include
#include

#define MAX 10 /*size of the Stack*/

struct stack
{
int arr[MAX];
}s;

int TOP=-1;

void push(int x)
{
TOP=TOP+1;
s.arr[TOP]=x;
}

int pop()
{
int x;
if(TOP==-1)
{
printf("stack underflow");
getch();
}
else
{
x=s.arr[TOP];
TOP=TOP-1;
}
return x;
}

int displaymenu()
{
int c;
printf("\nSTACK MENU\n");
printf("1.PUSH\n");
printf("2.POP\n");
printf("3.PRINT\n");
printf("4.EXIT\n");
printf("Choice=");
scanf("%d",&c);
return c;
}

void main()
{
int x,i,choice;
clrscr();
do
{
choice=displaymenu();
switch(choice)
{
case 1:
if(TOP==MAX-1)
{
printf("stack overflow");
getch();
}
else
{
printf("enter the no.");
scanf("%d",&x);
push(x);
}
break;

case 2:
x=pop();
printf("\t%d",x);
break;

case 3:
for(i=0;i<=TOP;i++) printf("\t%d",s.arr[i]); break; case 4: break; default: printf("\n Invalid choice"); } }while(choice!=4); getch(); }

 

Rate this post

Leave a Reply