Advertisement
Sathvikks8

STACKofIntegers

Sep 24th, 2020 (edited)
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MAX 5
  4. int STACK[MAX],top=-1;
  5. int isEmpty()
  6. {
  7.   if(top==-1)
  8.     return 1;
  9.   else
  10.     return 0;
  11. }
  12. int isFull()
  13. {
  14.   if(top==MAX-1)
  15.     return 1;
  16.   else
  17.     return 0;
  18. }
  19. int PUSH(int elem)
  20. {
  21.   if(isFull())
  22.   {
  23.     printf("\nStack Overflow. Please remove an element before adding a new one");
  24.     return -1;
  25.   }
  26.   else
  27.     return STACK[++top]=elem;
  28. }
  29. int POP()
  30. {
  31.   if(isEmpty())
  32.   {
  33.     printf("\nStack Underflow. Please insert an element before removing");
  34.     return -1;
  35.   }
  36.   else
  37.     return STACK[top--];
  38. }
  39. void status()
  40. {
  41.   if(isEmpty())
  42.   {
  43.     printf("\nStack Underflow");
  44.     return;
  45.   }
  46.   else
  47.   {
  48.     if(isFull())
  49.       printf("\nStack Overflow");
  50.     printf("\nThe elements of the STACK are: ");
  51.     for(int i=0;i<=top;i++)
  52.       printf("%d ",STACK[i]);
  53.   }
  54. }
  55. void palindrome()
  56. {
  57.   int num;
  58.   printf("\nEnter the number to be checked: ");
  59.   scanf("%d",&num);
  60.   int backupNum=num;
  61.   while(!isEmpty())
  62.     POP();
  63.   while(!isFull() && num>0)
  64.   {
  65.     STACK[++top]=num%10;
  66.     num/=10;
  67.   }
  68.   if(isFull() && num>0 )
  69.   {
  70.     printf("\nStack Overflow");
  71.     return;
  72.   }
  73.   num=backupNum;
  74.   while(num>0)
  75.   {
  76.     if(num%10!=POP())
  77.     {
  78.       printf("\nThe entered number is not a palindrome");
  79.       return;
  80.     }
  81.     num/=10;
  82.   }
  83.   printf("\nThe entered number is a palindrome");
  84. }
  85. void exitProgram()
  86. {
  87.   printf("\nThank you for using the program");
  88.   exit(0);
  89. }
  90. int main()
  91. {
  92.   int choice;
  93.   while(1)
  94.   {
  95.     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>|");
  96.     scanf("%d",&choice);
  97.     switch (choice)
  98.     {
  99.       case 1:
  100.       {
  101.         int elem;
  102.         printf("\nEnter the element to be pushed in to the STACK: ");
  103.         scanf("%d",&elem);
  104.         if(PUSH(elem)!=-1)
  105.           printf("\n%d was added to the stack",elem);
  106.       }
  107.       break;
  108.       case 2:
  109.       {
  110.         int result=POP();
  111.         if(result!=-1)
  112.           printf("\n%d was removed from the stack",result);
  113.       }
  114.       break;
  115.       case 3:
  116.         palindrome();
  117.       break;
  118.       case 4:
  119.         status();
  120.       break;
  121.       case 5:
  122.         exitProgram();
  123.       break;
  124.       default:
  125.         printf("\nIncorrect choice. Please retry\n");
  126.     }
  127.   }
  128. }
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement