Tuesday, July 2, 2013

C PROGRAM FOR STACK

/*PROGRAM TO MAKE A STACK TEST PUSH & POP OPERATION IN A STACK*/
#include<stdio.h>
#include<conio.h>
#define max 5
void PUSH();
void POP();
void DISPLAY();
int a[max], item, i, v, top=-1, n;
void main()
{
clrscr();
do {
printf("\nChoice are:");
printf("\n1: PUSH");
printf("\n2: POP");
printf("\n3: DISPLAY ITEMS");
printf("\n4: EXIT");
printf("\nEnter your choice:");
scanf("%d", &n);
switch(n) {
case 1: { PUSH();
break; }
case 2: { POP();
    break; }
case 3: { DISPLAY();
break; }
}
}
while (n!=4);
}

void PUSH() {
if(top==max-1)
printf("\nSTACK OVERFLOW\n");
else {
top=top+1;
printf("\nEnter pushing value:");
scanf("%d", &v);
a[top] = v; }
}
void POP() {
if(top==-1)
printf("\nSTACK UNDERFLOW\n");
else {
item = a[top];
printf("The poped item is %d", item);
top= top-1; }
}
void DISPLAY() {
printf("\nThe items in the STACK are:");
for(i=0;i<=top;i++)
printf(" %d " , a[i]); }





/*OUTPUT:-

Choice are:
1: PUSH
2: POP
3: DISPLAY ITEMS
4: EXIT
Enter your choice:1

Enter pushing value:30

Choice are:
1: PUSH
2: POP
3: DISPLAY ITEMS
4: EXIT
Enter your choice:3

The items in the STACK are: 30
Choice are:
1: PUSH
2: POP
3: DISPLAY ITEMS
4: EXIT
Enter your choice:2
The poped item is 30
Choice are:
1: PUSH
2: POP
3: DISPLAY ITEMS
4: EXIT
Enter your choice:4 */

No comments:

Post a Comment