C program to implement stack using array


/* C program to implement stack using array */

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

struct stack
{
int a[10],top;
}st;

//function to print the items in the stack
void view()
{
int i;
if(st.top==-1)
 printf("\nStack underflow\n");
 else
 {
 printf("\n");
  for(i=st.top;i>-1;i--)
  printf("%d ",st.a[i]);
 }
}

void main()
{
int i,c,n;
st.top=-1;
clrscr();
while(1)    //infinite loop
{
printf("\nEnter 1.push 2.pop 3.view 4.exit\n");
scanf("%d",&c);
switch(c)
{
case 1:if(st.top>=9)
printf("\nStack overflow\n");
else
{
printf("\nEnter a number:");
scanf("%d",&n);
st.top++;
st.a[st.top]=n;
}break;
case 2:if(st.top==-1)
printf("\nStack underflow\n");
else
{
  st.top--;
  printf("\nDeleted\n");
}
break;
case 3:view();
break;
case 4:exit(0);              
break;
default:printf("\nUnknown command\n");
break;
}
}
}

Comments