Search This Blog

Friday, March 7, 2014

Stack ADT


Stack ADT

#include<stdio.h>
#define max 10
int s[max],top=-1;
void push(int elmt)
{
if(top>=max)
printf("stackisfull\n");
else
{
top++;
s[top]=elmt;
}
}
int pop()
{
int elmt;
if(top==-1)
 return -1;
else
{
elmt=s[top];
top--;
return elmt;
}
}
int peep()
{
 int elmt;
 if(top==-1)
  return -1;
 else
  return s[top];
 }
void display()
{
int elmt;
int i;
if(top==-1)
printf("\n  stack is empty no element is displayed\n");
else
{
printf("\t<---Top");
for(i=top;i>=0;i--)
printf("\n| %d |\n",s[i]);
printf("\t\n----\n");
}
}

void main()
{
int ch,e;
while(1)
{
printf("1.Push\n2.Pop\n3.Peep\n4.Display\n5.Exit");
printf("\nEnter choice of operation=>");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the Element->");
scanf("%d",&e);
push(e);
break;
case 2:
e=pop();
if(e<0)
printf("\nError! Stack Empty\n");
else
printf("\nThe Poped Element is %d\n",e);
break;
case 3:
e=peep();
if(e<0)
printf("\nStack Empty\n");
else
printf("\nThe Top element in the stack is %d\n",e);
break;
case 4:
display();
break;
case 5:
exit(0);
default:
printf("invalid option");
}
}
}

No comments:

Post a Comment