Saturday, 12 April 2014

Stack Implementation Using Array

PROGRAM:

#include<stdio.h>
#include<conio.h>
#define SIZE 5
void push();
void pop();
void display();
int s[SIZE],top=-1,i,item;
void main()
{
int ch;
clrscr();
printf("\n\n\n$$$ STACK IMPLEMENTATION USING ARRAY $$$\n\n\n");
while(1)
{
printf("STACK OPERATIONS ARE\n");
printf("1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n");
printf("Enter your choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:push();break;
case 2:pop();break;
case 3:display();break;
case 4:exit(0);
default:printf("Enter the correct option\n");
break;
}
}
}
void push()
{
if(top==SIZE-1)
{
printf("Stack is full or overflow\n");
}
else
{
printf("Enter the element to be pushed:\t");
scanf("%d",&item);
top=top+1;
s[top]=item;
}
}
void pop()
{
if(top==-1)
{
printf("Stack is empty or underflow\n");
}
else
{
printf("The deleted element is: %d",s[top]);
top=top-1;
}
}
void display()
{
if(top==-1)
{
printf("Stack is empty or underflow\n");
}
else
{
printf("Content of the stack is:\n");
for(i=top;i>=0;i--)
{
printf("%d\n",s[i]);
}
}
}


OUTPUT:


$$$ STACK IMPLEMENTATION USING ARRAY $$$

STACK OPERATIONS ARE
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:      1
Enter the element to be pushed: 11

STACK OPERATIONS ARE
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:      1
Enter the element to be pushed: 22

STACK OPERATIONS ARE
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:      1
Enter the element to be pushed: 33

STACK OPERATIONS ARE
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:      3
Content of the stack is:
33
22
11

STACK OPERATIONS ARE
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:      2
The deleted element is: 33

STACK OPERATIONS ARE
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:      3
Content of the stack is:
22
11

STACK OPERATIONS ARE
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter your choice:      4



No comments:

Post a Comment