WAP of stack that perform push,pop and peep operation using functions

/*stack-push,pop and peep*/
#include<stdio.h>
#include<conio.h>
int top=-1;
const int size=10;
int a[10];
void push();
void pop();
void peep();
void display();
void main()
{
int n;
char ch='y';
clrscr();
while(ch=='y')
{
printf("\n1.push\n2.pop\n3.peep\n4.display\nEnter your choice:");
scanf("%d",&n);
switch(n)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
peep();
break;
}
case 4:
{
display();
break;
}
default:
{
printf("Invalid choice....");
break;
}
}
printf("\nDo you want to continue?");
fflush(stdin);
scanf("%c",&ch);
}
getch();
}
void push()
{
int item;
if(top>=size)
{
printf("\nStack overflow.....\n");
}
else
{
top+=1;
printf("Enter element:");
scanf("%d",&item);
a[top]=item;

}
}
void pop()
{
int item;
if(top<0)
{
printf("\nStack underflow....\n");
}
else
{
item=a[top];
top-=1;
printf("\nValue deleted is:%d",item);
}

}
void display()
{
int i;
if(top<0)
{
printf("\nStack underflow......");
}
else
{
for(i=0;i<=top;i++)
{
printf("\n%d",a[i]);
}
}
}
void peep()
{
int item;
if(top<0)
{
printf("\nStack underflow......");
}
else
{
item=a[top];
printf("the top most element is %d",item);
}
}


No comments:

Post a Comment