I am a beginner in programming. I just made this program in c language for popping elements from Stack. Firstly, I defined function, then scanned a stack and then used a menu for popping elements. But the output is "The stack is empty" everytime. Can you spot the mistake? It will be of great help.
/* Program to Pop elements from a given stack*/
#include<stdio.h>
#include<conio.h>
#define MAX_SIZE 20
int stack[MAX_SIZE],top=NULL;
int pop() //function define
{
int x;
if(top==NULL)
return(NULL);
else
{
x=stack[top];
top=top-1;
return(x);
}
}
main() //program initialization
{
int c,i,x;
clrscr();
printf("Enter the size of stack:");
scanf("%d",&top);
printf("Enter %d elements of stack from top to bottom:",top);
for(i=top;i>0;i--)
scanf("%d",stack[i]);
//Pop element from stack
while(1)
{
printf("1.Enter 1 to Pop element from stack\n");
printf("2.Enter 2 to Print Stack\n");
printf("3.Enter 3 to Exit\n");
scanf("%d",&c);
switch(c)
{
case 1:
{
x=pop();
if(x!=NULL)
printf("\nThe number poped is: %d\n",x);
else
printf("\nThe stack is empty!!");
break;
}
case 2:
{
if(top==NULL)
printf("stack is empty!!\n");
else
{
printf("stack is\n");
for(i=top;i>0;i--)
printf("%d\n",stack[i]);
}
break;
}
case 3:
{
exit(0);
break;
}
default:
{
printf("Wrong input\n");
}
printf("\n");
}
getch();
}
}