Prog - 3 Stack Op

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Develop a menu driven Program in C for the following operations on STACK of

Integers (Array Implementation of Stack with maximum size MAX)

a) Push an Element on to Stack


b) Pop an Element from Stack
c) Demonstrate how Stack can be used to check Palindrome
d) Demonstrate Overflow and Underflow situations on Stack
e) Display the status of Stack
f) Exit

Support the program with appropriate functions for each of the above operations

#include <stdio.h>
#include <stdlib.h>

#define MAX 5

int stack[MAX];
int top = -1;

void push(int element)


{
if (top == MAX - 1)
{
printf("Stack Overflow! Cannot push more elements.\n");
}
else
{
stack[++top] = element;
printf("%d pushed onto the stack.\n", element);
}
}

int pop()
{
if (top == -1)
{
printf("Stack Underflow! Cannot pop from an empty stack.\n");
return -1;
}
else
{
int ele = stack[top--];
return ele;
}
}

int isPalindrome()
{
int i, j;
for (i = top, j = 0; i >= j; i--, j++)
{
if (stack[i] != stack[j])
{
return 0; // Not a palindrome
}
}
return 1; // Palindrome
}

void displayStack()
{
if (top == -1)
{
printf("Stack is empty.\n");
} else
{
printf("Stack elements: ");
for (int i = top; i >=0; i--)
printf("%d\t", stack[i]);
printf("\n");
}
}

int main()
{
int choice, element;
do {
printf("\n----- Menu -----\n");
printf("1. Push an Element\n");
printf("2. Pop an Element\n");
printf("3. Check Palindrome\n");
printf("4. Display Stack Status\n");
printf("5. Exit");
printf("\n----------------\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice)
{
case 1:
printf("Enter the element to push: ");
scanf("%d", &element);
push(element);
break;

case 2:
int val=pop();
printf("%d popped from the stack.\n", val);
break;

case 3:
if (isPalindrome())
printf("The stack is a palindrome.\n");
else
printf("The stack is not a palindrome.\n");
break;

case 4:
displayStack();
break;

case 5:
printf("Exiting the program.\n");
break;

default:
printf("Invalid choice! Please enter a valid option.\n");
}
} while (choice != 5);

return 0;
}

Output:
----- Menu -----
1. Push an Element
2. Pop an Element
3. Check Palindrome
4. Display Stack Status
5. Exit
----------------
Enter your choice: 1
Enter the element to push: 12
12 pushed onto the stack.

Enter your choice: 1


Enter the element to push: 45
45 pushed onto the stack.

Enter your choice: 1


Enter the element to push: 65
65 pushed onto the stack.

Enter your choice: 1


Enter the element to push: 8
8 pushed onto the stack.

Enter your choice: 1


Enter the element to push: 65
65 pushed onto the stack.

Enter your choice: 1


Enter the element to push: 52
Stack Overflow! Cannot push more elements.

Enter your choice: 4


Stack elements: 65 8 65 45 12

----- Menu -----


1. Push an Element
2. Pop an Element
3. Check Palindrome
4. Display Stack Status
5. Exit
----------------
Enter your choice: 3
The stack is not a palindrome.

----- Menu -----


1. Push an Element
2. Pop an Element
3. Check Palindrome
4. Display Stack Status
5. Exit
----------------
Enter your choice: 2
65 popped from the stack.

Enter your choice: 2


8 popped from the stack.

Enter your choice: 2


65 popped from the stack.

Enter your choice: 2


45 popped from the stack.

Enter your choice: 2


12 popped from the stack.

Enter your choice: 2


Stack Underflow! Cannot pop from an empty stack.

Enter your choice:5

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy