Program for stack using array.

/**********************
WAP TO IMPLEMENT STACK
USING ARRAY
***********************/

#include stdio.h
#include conio.h

struct stack
{
int a[3];
int top;
}s;

void main()
{
int ch,x,i;
clrscr();
s.top= -1;
printf("\nARRAY AS STACK PROGRAM");
while(1)
{
printf("\n\n1: PUSH \
\n2: POP \
\n3: DISPLAY \
\n4: EXIT \
\nEnter Your Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
if(s.top==2)
{
printf("\nSTACK OVERFLOW\n");
}
else
{
printf("Enter The Element: ");
scanf("%d",&x);
s.top++;
s.a[s.top]=x;
printf("\nELEMENT PUSHED\n");
}
break;
}
case 2:
{
if(s.top==-1)
{
printf("\nSTACK UNDERFLOW\n");
}
else
{
printf("\nPOPED ELEMENT IS: %d",s.a[s.top--]);
}
break;
}
case 3:
{
printf("\nELEMENTS ARE:\n");
for(i=s.top;i>=0;i--)
{
printf("%d ",s.a[i]);
}
break;
}
case 4:
{
exit();
}
default:
{
printf("\nINVALID CHOICE");
}
}
}
}

No comments:

Post a Comment