C PROGRAMMINGDATA STRUCTURES
write a c program for stack using array
stack using array
#include<stdio.h>
#include<conio.h>
int limit=5;//size of stack
int stack[10];
int top=-1;
void push(int data)
{
if(top==limit-1)
printf("nOverflow");
else
{
top++;
stack[top]=data;
printf("n%d pushed into Stackn",data);
}
}
int pop()
{
int d;
if(top==-1)
printf("nUnderflow");
else
{
d= stack[top];
stack[top]=0;
top--;
return d;
}
}
int peek()
{
int v;
if(top == -1)
{
// Stack is empty
return -999;
}
else
{
v = stack[top];
return v;
}
}
void display()
{
int c;
c = 0;
for(c=0;c<limit;c++)
{
printf("%d ", stack[c]);
c = c++;
}
}
void main()
{
int option, val;
do
{
printf("n *****MENU*****");
printf("n 1. PUSH");
printf("n 2. POP");
printf("n 3. PEEK");
printf("n 4. DISPLAY");
printf("n 0. EXIT");
printf("n Enter your choice: ");
scanf("%d",&option);
switch(option)
{
case 1:
printf("n Enter the value to push on Stack: ");
scanf("%d",&val);
push(val);
break;
case 2:
val = pop();
if(val!=-999)
printf("n The value popped from Stack = %d", val);
break;
case 3:
val = peek();
if(val!=-999)
printf("n The value at top of Stack = %d", val);
break;
case 4:
printf("n The contents of Stack are : n");
display();
break;
case 0:
printf("nExiting...");
exit(0);
break;
default:
printf("nInvalid option. Try again...");
}
}while(1);
getch();
}