/* C program to implement stack */
#include<stdio.h>
#define SIZE 5
int a[SIZE],top=-1;
void push()
{
if(top==SIZE-1)
{
printf("\n Stack overflow");
}
else
{
printf("Enter a no. to push:");
scanf("%d", &a[++top]);
printf("Element pushed into stack:\n\n");
}
}
void pop()
{
if(top==-1)
{
printf("\n Stack underflow");
}
else
{
top--;
printf("Element popped into stack is:%d", a[top+1]);
}
}
void show()
{
int i;
if(top==-1)
{
printf("Stack is empty:\n");
}
else
for(i=0; i<=top; i++)
printf("%d", a[i]);
}
int main()
{
int ch;
do
{
printf(":::Stack operation:::\n");
printf("1.push 2.pop 3.show 4.exit\n");
printf("Enter your choice:");
scanf("%d", &ch);
switch(ch)
{
case 1: push(); break;
case 2: pop(); break;
case 3: show(); break;
case 4: printf("Invalid choice..");
}
}while(ch!=4);
return 0;
}
#define SIZE 5
int a[SIZE],top=-1;
void push()
{
if(top==SIZE-1)
{
printf("\n Stack overflow");
}
else
{
printf("Enter a no. to push:");
scanf("%d", &a[++top]);
printf("Element pushed into stack:\n\n");
}
}
void pop()
{
if(top==-1)
{
printf("\n Stack underflow");
}
else
{
top--;
printf("Element popped into stack is:%d", a[top+1]);
}
}
void show()
{
int i;
if(top==-1)
{
printf("Stack is empty:\n");
}
else
for(i=0; i<=top; i++)
printf("%d", a[i]);
}
int main()
{
int ch;
do
{
printf(":::Stack operation:::\n");
printf("1.push 2.pop 3.show 4.exit\n");
printf("Enter your choice:");
scanf("%d", &ch);
switch(ch)
{
case 1: push(); break;
case 2: pop(); break;
case 3: show(); break;
case 4: printf("Invalid choice..");
}
}while(ch!=4);
return 0;
}
* INPUT/OUTPUT:- *
:::Stack operation:::
1.push 2.pop 3.show 4.exit
Enter your choice:1
Enter a no. to push:56
Element pushed into stack:
1.push 2.pop 3.show 4.exit
Enter your choice:1
Enter a no. to push:56
Element pushed into stack:
:::Stack operation:::
1.push 2.pop 3.show 4.exit
Enter your choice:1
Enter a no. to push:85
Element pushed into stack:
:::Stack operation:::
1.push 2.pop 3.show 4.exit
Enter your choice:1
Enter a no. to push:33
Element pushed into stack:
:::Stack operation:::
1.push 2.pop 3.show 4.exit
Enter your choice:1
Enter a no. to push:55
Element pushed into stack:
:::Stack operation:::
1.push 2.pop 3.show 4.exit
Enter your choice:1
Enter a no. to push:89
Element pushed into stack:
:::Stack operation:::
1.push 2.pop 3.show 4.exit
Enter your choice:1
Stack overflow
:::Stack operation:::
1.push 2.pop 3.show 4.exit
Enter your choice:3
56 85 33 55 89
1.push 2.pop 3.show 4.exit
Enter your choice:3
56 85 33 55 89
:::Stack operation:::
1.push 2.pop 3.show 4.exit
Enter your choice:2
Element popped into stack is:89
1.push 2.pop 3.show 4.exit
Enter your choice:2
Element popped into stack is:89
:::Stack operation:::
1.push 2.pop 3.show 4.exit
Enter your choice:2
Element popped into stack is:55
1.push 2.pop 3.show 4.exit
Enter your choice:2
Element popped into stack is:55
:::Stack operation:::
1.push 2.pop 3.show 4.exit
Enter your choice:2
Element popped into stack is:33
1.push 2.pop 3.show 4.exit
Enter your choice:2
Element popped into stack is:33
:::Stack operation:::
1.push 2.pop 3.show 4.exit
Enter your choice:2
Element popped into stack is:85
1.push 2.pop 3.show 4.exit
Enter your choice:2
Element popped into stack is:85
:::Stack operation:::
1.push 2.pop 3.show 4.exit
Enter your choice:2
Element popped into stack is:56
1.push 2.pop 3.show 4.exit
Enter your choice:2
Element popped into stack is:56
:::Stack operation:::
1.push 2.pop 3.show 4.exit
Enter your choice:2
Stack underflow
1.push 2.pop 3.show 4.exit
Enter your choice:2
Stack underflow
:::Stack operation:::
1.push 2.pop 3.show 4.exit
Enter your choice:2
Stack underflow
1.push 2.pop 3.show 4.exit
Enter your choice:2
Stack underflow
:::Stack operation:::
1.push 2.pop 3.show 4.exit
Enter your choice:3
Stack is empty:
1.push 2.pop 3.show 4.exit
Enter your choice:3
Stack is empty:
:::Stack operation:::
1.push 2.pop 3.show 4.exit
Enter your choice:4
Invalid choice..
1.push 2.pop 3.show 4.exit
Enter your choice:4
Invalid choice..