Рет қаралды 11,527
#define MAX 3
int stack[MAX];
int top=-1;
void push(int num)
{
if(top==MAX-1)
{
printf("
Stack Overflows");
}
else
{
top++;
stack[top]=num;
}
}
void pop()
{
if(top==-1)
{
printf("
Stack Underflows");
}
else
{
printf("
Poped element is %d",stack[top]);
top--;
}
}
void peek()
{
if(top==-1)
printf("
Stack Underflows");
else
printf("
Top element is %d",stack[top]);
}
void display()
{
if(top==-1)
printf("
Stack is empty");
else
{
int i;
printf("
Elements in the stack:");
for(i=0;ilessthan=top;i++)
printf("\t%d",stack[i]);
}
}
int main()
{
int choice,num;
printf("
\t STACK OPERATIONS USING ARRAY");
printf("
\t--------------------------------");
printf("
\t 1.PUSH
\t 2.POP
\t 3.TOP
\t 4.DISPLAY
\t 5.EXIT");
do
{
printf("
Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("
Enter the element to push: ");
scanf("%d",&num);
push(num);
display();
break;
case 2:
pop();
display();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
printf("
\t EXIT POINT ");
break;
default:
printf ("
\t Please Enter a Valid Choice");
break;
}
}
while(choice!=5);
return 0;
}
/*int isFull()
{
if(top==MAX-1)
return 1;
else
return 0;
}
int isEmpty()
{
if top==-1)
return 1;
else
return 0;
}
*/