C Program for Stack
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void push();
void pop();
void display();
int stack[100],maxstack,top=-1,choice,data;
void main()
{
clrscr();
printf("Enter the size of stack\n");
scanf("%d",&maxstack);
do
{
printf("\nEnter the Choice\n");
printf("Enter 1 for push element into stack\n");
printf("Enter 2 for pop element from stack\n");
printf("Enter 3 for display stack\n");
printf("Enter 4 for Exit\n");
scanf("%d",&choice);
switch (choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nEnter valid choice\n");
}
}while(choice!=4);
getch();
}
void push()
{
if(top==maxstack-1)
printf("\n.....0verflow......\n");
else
{
printf("\nEnter value for enter\n");
scanf("%d",&data);
top=top+1;
stack[top]=data;
printf("Element pushed successfully.........,\n");
}
}
void pop()
{
int pop;
if(top==-1)
printf("\nUnderflow\n");
else
{
pop=stack[top];
top=top-1;
printf("\nElement %d have been poped successfully.......,\n",pop);
}
}
void display()
{
int i;
if(top==-1)
printf("\nStack is Empty\n");
else
{
printf("\nElements in Stack are\n");
printf("[ ");
for(i=0;i<=top;i++)
printf("%d ",stack[i]);
printf("]");
}
}
OUTPUT
------------------------------------------------------------------------------------------------------------
You can also visit:-