I am looking for a C program for stack.
I wrote it below for reference of everyone
#include <stdio.h>
#include <stdlib.h>
#define SIZE 4
int top = -1;
int inp_array[SIZE];
void push();
void pop();
void show();
int main()
{
int choice;
while(1)
{
printf(“\n perform operations on the stack\n”);
printf(“\n1.push the element.\n2.pop the element\n3.show the element\n4.End”);
printf(“\nEnter your choice [1,2,3,4]”);
scanf(“%d”,&choice);
switch (choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
show();
break;
case 4:
exit(0);
default:
printf("InValid choice!\n");
}
}
return 0;
}
void push()
{
int x;
if(top == SIZE-1)
printf(“Overflow!”);
else
{
printf(“\nEnter element to be added to stack: “);
scanf(”%d”,&x);
top = top+1;
inp_array[top] = x;
}
}
void pop()
{
if(top == -1)
printf(“Underflow!”);
else
{
printf(“\n Popped element : %d”,inp_array[top]);
top = top-1;
}
}
void show()
{
if(top == -1)
printf(“Underflow!”);
else
{
printf(“\n Element in the stack \n”);
for(int i=0; i<top;i++)
printf("%d ",inp_array[i]);
}
}
Thank You!