Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define MAX 5
- int STACK[MAX],top=-1;
- int isEmpty()
- {
- if(top==-1)
- return 1;
- else
- return 0;
- }
- int isFull()
- {
- if(top==MAX-1)
- return 1;
- else
- return 0;
- }
- int PUSH(int elem)
- {
- if(isFull())
- {
- printf("\nStack Overflow. Please remove an element before adding a new one");
- return -1;
- }
- else
- return STACK[++top]=elem;
- }
- int POP()
- {
- if(isEmpty())
- {
- printf("\nStack Underflow. Please insert an element before removing");
- return -1;
- }
- else
- return STACK[top--];
- }
- void status()
- {
- if(isEmpty())
- {
- printf("\nStack Underflow");
- return;
- }
- else
- {
- if(isFull())
- printf("\nStack Overflow");
- printf("\nThe elements of the STACK are: ");
- for(int i=0;i<=top;i++)
- printf("%d ",STACK[i]);
- }
- }
- void palindrome()
- {
- int num;
- printf("\nEnter the number to be checked: ");
- scanf("%d",&num);
- int backupNum=num;
- while(!isEmpty())
- POP();
- while(!isFull() && num>0)
- {
- STACK[++top]=num%10;
- num/=10;
- }
- if(isFull() && num>0 )
- {
- printf("\nStack Overflow");
- return;
- }
- num=backupNum;
- while(num>0)
- {
- if(num%10!=POP())
- {
- printf("\nThe entered number is not a palindrome");
- return;
- }
- num/=10;
- }
- printf("\nThe entered number is a palindrome");
- }
- void exitProgram()
- {
- printf("\nThank you for using the program");
- exit(0);
- }
- int main()
- {
- int choice;
- while(1)
- {
- printf("\n\n1)\tPush an Element on to Stack\n2)\tPop an Element from Stack\n3)\tCheck for Palindrome\n4)\tDisplay the Status of STACK\n5)\tExit\n>|");
- scanf("%d",&choice);
- switch (choice)
- {
- case 1:
- {
- int elem;
- printf("\nEnter the element to be pushed in to the STACK: ");
- scanf("%d",&elem);
- if(PUSH(elem)!=-1)
- printf("\n%d was added to the stack",elem);
- }
- break;
- case 2:
- {
- int result=POP();
- if(result!=-1)
- printf("\n%d was removed from the stack",result);
- }
- break;
- case 3:
- palindrome();
- break;
- case 4:
- status();
- break;
- case 5:
- exitProgram();
- break;
- default:
- printf("\nIncorrect choice. Please retry\n");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement