Implementation of Stack + Palindrome check

Exported from Notepad++
#include <stdio.h> #include <stdlib.h> #include <conio.h> int stk[100]; int stack_size; int top = -1; int is_full( void ) { return ( top == stack_size - 1 ); } int is_empty( void ) { return ( top == -1 ); } void push( int item ) { stk[ ++top] = item; } int pop( void ) { return stk[top--]; } void display( void ) { int i; printf ( "\n\nThe STACK elements are...\n\n" ); for ( i = top; i >= 0; --i ) printf ( "%d\n\n", stk[i] ); } int is_palindrome( int nums[] ) { int i = 0; while ( !is_empty()) { if ( nums[i ++] != pop() ) return 0; } return 1; } int main( void ) { int item, ch; int nums[] = {10, 20, 10}; printf ( "\nEnter the size of STACK: ?\b" ); scanf ( "%d", &stack_size ); do { printf ( "\n\t1. Push \t 2. Pop \t 3. Display\n" ); printf ( "\n\t4. Palindrome Check \t 5. Exit \n" ); printf ( "\nEnter your choice: ?\b" ); scanf ( "%d", &ch ); switch ( ch ) { case 1: if ( is_full() ) { printf ( "\nSTACK is Overflow\n\n" ); } else { printf ( "\nEnter an Item to insert: ?\b" ); scanf ( "%d", &item ); push ( item ); } break; case 2: if ( is_empty() ) { printf ( "\nSTACK is Underflow\n" ); } else { printf ( "\nPopped: %d\n", pop() ); } break; case 3: if ( is_empty() ) { printf ( "\nSTACK is Empty\n" ); } else { display(); } break; case 4: if ( is_empty() ) { printf ( "\nSTACK is Empty\n" ); } else { if ( is_palindrome(nums) ) printf ( "\nIt is Palindrome\n" ); else printf ( "\nIt is not a Palindrome\n" ); } break; case 5: return 0; default: printf ( "\nInvalid Option\n" ); } getch(); }while(1); return 0; } /* OUTPUT: Run - 1 Enter the size of STACK: 3 1. Push 2. Pop 3. Display 4. Palindrome Check 5. Exit Enter your choice: 1 Enter an Item to insert: 10 1. Push 2. Pop 3. Display 4. Palindrome Check 5. Exit Enter your choice: 1 Enter an Item to insert: 20 1. Push 2. Pop 3. Display 4. Palindrome Check 5. Exit Enter your choice: 1 Enter an Item to insert: 30 1. Push 2. Pop 3. Display 4. Palindrome Check 5. Exit Enter your choice: 1 STACK is Overflow 1. Push 2. Pop 3. Display 4. Palindrome Check 5. Exit Enter your choice: 3 The STACK elements are... 30 20 10 1. Push 2. Pop 3. Display 4. Palindrome Check 5. Exit Enter your choice: 2 Popped: 30 1. Push 2. Pop 3. Display 4. Palindrome Check 5. Exit Enter your choice: 3 The STACK elements are... 20 10 1. Push 2. Pop 3. Display 4. Palindrome Check 5. Exit Enter your choice: 2 Popped: 20 1. Push 2. Pop 3. Display 4. Palindrome Check 5. Exit Enter your choice: 3 The STACK elements are... 10 1. Push 2. Pop 3. Display 4. Palindrome Check 5. Exit Enter your choice: 2 Popped: 10 1. Push 2. Pop 3. Display 4. Palindrome Check 5. Exit Enter your choice: 3 STACK is Empty 1. Push 2. Pop 3. Display 4. Palindrome Check 5. Exit Enter your choice: 5 Exit Run 2: Enter the size of STACK: 3 1. Push 2. Pop 3. Display 4. Palindrome Check 5. Exit Enter your choice: 1 Enter an Item to insert: 10 1. Push 2. Pop 3. Display 4. Palindrome Check 5. Exit Enter your choice: 1 Enter an Item to insert: 20 1. Push 2. Pop 3. Display 4. Palindrome Check 5. Exit Enter your choice: 1 Enter an Item to insert: 10 1. Push 2. Pop 3. Display 4. Palindrome Check 5. Exit Enter your choice: 3 The STACK elements are... 10 20 10 1. Push 2. Pop 3. Display 4. Palindrome Check 5. Exit Enter your choice: 4 It is Palindrome 1. Push 2. Pop 3. Display 4. Palindrome Check 5. Exit Enter your choice: 1 Enter an Item to insert: 10 1. Push 2. Pop 3. Display 4. Palindrome Check 5. Exit Enter your choice: 1 Enter an Item to insert: 20 1. Push 2. Pop 3. Display 4. Palindrome Check 5. Exit Enter your choice: 1 Enter an Item to insert: 30 1. Push 2. Pop 3. Display 4. Palindrome Check 5. Exit Enter your choice: 3 The STACK elements are... 30 20 10 1. Push 2. Pop 3. Display 4. Palindrome Check 5. Exit Enter your choice: 4 It is not a Palindrome 1. Push 2. Pop 3. Display 4. Palindrome Check 5. Exit Enter your choice: 5 Exit */

No comments:

Post a Comment

Online Certifications

Python for Beginners Offered by: Christian Drumm, Stephan Jacobs Course dates: 2022-04-05 to 2022-06-01 Topics Python Fundamentals Lists and...