MENU DRIVEN
QUESTION:
Develop a menu driven Program in C for the following operations on STACK of Integers (Array Implementation of Stack with maximum size MAX)
Push an Element on to Stack
Pop an Element from Stack
Demonstrate how Stack can be used to check Palindrome
Demonstrate Overflow and Underflow situations on Stack
Display the status of Stack
Exit
Support the program with appropriate functions for each of the above operations
EXPLANATION:
Push an element
Pop an element
Check if a number is Palindrome using Stack
Show Overflow/Underflow
Display stack
Exit
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int stack[MAX];
int top = -1;
// Function to push an element onto the stack
void push(int element) {
if (top == MAX - 1) {
printf("Stack Overflow! Cannot push %d\n", element);
return;
}
stack[++top] = element;
printf("%d pushed to stack.\n", element);
}
// Function to pop an element from the stack
int pop() {
if (top == -1) {
printf("Stack Underflow! Cannot pop.\n");
return -1; // Indicate error
}
int popped = stack[top--];
printf("%d popped from stack.\n", popped);
return popped;
}
// Function to display current stack
void displayStack() {
if (top == -1) {
printf("Stack is empty.\n");
return;
}
printf("Current Stack (top to bottom):\n");
for (int i = top; i >= 0; i--) {
printf("%d\n", stack[i]);
}
}
// Function to check if the current stack is a palindrome
void checkStackPalindrome() {
if (top == -1) {
printf("Stack is empty. Cannot check for palindrome.\n");
return;
}
int isPalindrome = 1;
int i = 0, j = top;
// Compare stack[i] with stack[j]
while (i < j) {
if (stack[i] != stack[j]) {
isPalindrome = 0;
break;
}
i++;
j--;
}
if (isPalindrome) {
printf("The stack elements form a palindrome.\n");
} else {
printf("The stack elements do NOT form a palindrome.\n");
}
}
// Main function with menu
int main() {
int choice, value;
while (1) {
printf("\n--- Stack Operations Menu ---\n");
printf("1. Push an Element\n");
printf("2. Pop an Element\n");
printf("3. Check if Stack Content is Palindrome\n");
printf("4. Display Stack Status\n");
printf("5. Exit\n");
printf("Enter your choice (1-5): ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter integer to push: ");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
checkStackPalindrome();
break;
case 4:
displayStack();
break;
case 5:
printf("Exiting program.\n");
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
OUTPUT
ceciot@cec-IOT:~$ cd Desktop/
ceciot@cec-IOT:~/Desktop$ gcc menudriven.c -o abcd
ceciot@cec-IOT:~/Desktop$ ./abcd
--- Stack Operations Menu ---
1. Push an Element
2. Pop an Element
3. Check if Stack Content is Palindrome
4. Display Stack Status
5. Exit
Enter your choice (1-5): 1
Enter integer to push: 23
23 pushed to stack.
--- Stack Operations Menu ---
1. Push an Element
2. Pop an Element
3. Check if Stack Content is Palindrome
4. Display Stack Status
5. Exit
Enter your choice (1-5): 1
Enter integer to push: 26
26 pushed to stack.
--- Stack Operations Menu ---
1. Push an Element
2. Pop an Element
3. Check if Stack Content is Palindrome
4. Display Stack Status
5. Exit
Enter your choice (1-5): 1
Enter integer to push: 27
27 pushed to stack.
--- Stack Operations Menu ---
1. Push an Element
2. Pop an Element
3. Check if Stack Content is Palindrome
4. Display Stack Status
5. Exit
Enter your choice (1-5): 4
Current Stack (top to bottom):
27
26
23
--- Stack Operations Menu ---
1. Push an Element
2. Pop an Element
3. Check if Stack Content is Palindrome
4. Display Stack Status
5. Exit
Enter your choice (1-5): 3
The stack elements do NOT form a palindrome.
--- Stack Operations Menu ---
1. Push an Element
2. Pop an Element
3. Check if Stack Content is Palindrome
4. Display Stack Status
5. Exit
Enter your choice (1-5): 2
27 popped from stack.
--- Stack Operations Menu ---
1. Push an Element
2. Pop an Element
3. Check if Stack Content is Palindrome
4. Display Stack Status
5. Exit
Enter your choice (1-5): 4
Current Stack (top to bottom):
26
23
--- Stack Operations Menu ---
1. Push an Element
2. Pop an Element
3. Check if Stack Content is Palindrome
4. Display Stack Status
5. Exit
Enter your choice (1-5): 5
Exiting program.
No comments:
Post a Comment