Sunday, May 22, 2011

Program to demonstrate the PUSH,POP,DISPLAY,EXIT operations on stack.

#include /*Header File*/
#define maximum 9 /*Header File*/
int top=-1; /*Declaration of variables*/

void push(int [],int);
int pop(int []);
void dis(int []);
void exit();

int main()
{
int stack[maximum]; /*Declaration of variables*/
int item; /*Declaration of variables*/
int ch; /*Declaration of variables*/
int m; /*Declaration of variables*/
while(1)
{
printf("\nMain Menu");
printf("\n1. Push");
printf("\n2. Pop");
printf("\n3. Display");
printf("\n4. Exit");
printf("\n Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1 : printf("\n Enter the value of item: ");
scanf("%d",&item);
printf("Element inserted");
push(stack,item);
break;
case 2 : m=pop(stack);
printf("\nElement deleted is: ");
printf("%d",m);
break;
case 3 : printf("Elements are: ");
disp(stack);
break;
case 4 : exit();
break;
default: printf("\nWrong choice entered");
break;
}
}
return 0;
}

void push(int stack[],int item)
{
if(top==maximum)
{
printf("\n Overflow Error");
}
else
{
top=top+1;
stack[top]=item;
}
}

int pop(int stack[])
{
int x;
x=stack[top];
top=top-1;
return x;
}

void display(int stack[])
{
int count; /*Declaration of variables*/

for( count=0;count<=top;count++)

{
printf("%d",stack[count]);
}

}

void exit()
{
exit(0);
}



OUTPUT

Main Menu
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1

Enter the value of item: 14
Element inserted
Main Menu
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 3
Elements are: 14
Main Menu
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 2

Element deleted is: 14
Main Menu
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 4

No comments:

Post a Comment