Friday, August 27, 2010
C Program to demonstrate stack Push and Pop
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define MAXSIZE 10
void push();
int pop();
void traverse();
int stack[MAXSIZE];
int top=-1;
void main()
{
int choice;
char ch;
do
{
clrscr();
printf("\n 1. PUSH");
printf("\n 2. POP");
printf("\n 3. TRAVERSE");
printf("\n Enter your choice ");
scanf("%d",&choice);
switch(choice)
{
case 1: push();
break;
case 2: printf("\n The popped element is %d",pop());
break;
case 3: traverse();
break;
default : printf("\n You entered the WRONG CHOICE ");
}
printf("\n Do you wish to continue(Y/N)");
fflush(stdin);
scanf("%c",&ch);
}
while(ch=='y' || ch=='Y');
}
void push()
{
int item;
if(top==MAXSIZE-1)
{
printf("\n The stack is full");
getch();
exit(0);
}
else
{
printf(" Enter the element to be inserted ");
scanf("%d",&item);
top=top+1;
stack[top]=item;
}
}
int pop()
{
int item;
if(top==-1)
{
printf(" The stack is Empty ");
getch();
exit(0);
}
else
{
item=stack[top];
top=top-1;
}
return (item);
}
void traverse()
{
int i;
if(top==-1)
{
printf(" The stack is Empty ");
getch();
exit(0);
}
else
{
printf(" The Elements are :\n");
for(i=top;i>=0;i--)
{
printf("\n %d",stack[i]);
}
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment