Friday, April 17, 2009

STACK USING ARRAYS

#include
#include
/*stack using arrays*/
#define max 5
int top=-1;
int stack[max];
void main()
{
int rpt=1;
int ch;
clrscr();
while(rpt)
{
printf("1.push\n");
printf("2.pop\n");
printf("3.display\n");
printf("4.exit\n");
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:return;
}
printf("Do you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&rpt);
}
}

push()
{
int item;
if(top==(max-1))
{
printf("stack is full\n");
return;
}
else
{
printf("enter the element to be inserted\n");
scanf("%d",&item);
top=top+1;
stack[top]=item;
}
}

pop()
{
if(top==-1)
{
printf("stack is empty\n");
return;
}
else
{
printf("Deleted element is %d",stack[top]);
top=top-1;
}
}

display()
{
int i;
if(top==-1)
{
printf("stack is empty\n");
return;
}
else
{
for(i=top;i>=0;i--)
printf("%d\n",stack[i]);
}
}

No comments: