0% found this document useful (0 votes)
2 views7 pages

C Stack Operations Program Guide

Uploaded by

wachheomkar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

C Stack Operations Program Guide

Uploaded by

wachheomkar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

3.

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], top = -1;

void push(int item) {

if (top == MAX - 1) {

printf("\n~~~ Stack Overflow ~~~\n");

return;

stack[++top] = item;

int pop() {

if (top == -1) {

printf("\n~~~ Stack Underflow ~~~\n");

return -1;

return stack[top--];

void display() {

if (top == -1) {
printf("\nStack is Empty\n");

return;

printf("\nStack elements:\n");

for (int i = top; i >= 0; i--)

printf("| %d |\n", stack[i]);

void palindrome() {

if (top == -1) {

printf("\nStack is Empty\n");

return;

int flag = 1;

for (int i = 0; i <= top / 2; i++) {

if (stack[i] != stack[top - i]) {

flag = 0;

break;

display();

printf("\nIt is %s Palindrome\n", flag ? "a" : "not a");

int main() {

int choice, item;

while (1) {

printf("\n~~~~ MENU ~~~~\n"


"1. Push (with Overflow demo)\n"

"2. Pop (with Underflow demo)\n"

"3. Palindrome check\n"

"4. Display Stack\n"

"5. Exit\n"

"Enter choice: ");

scanf("%d", &choice);

switch (choice) {

case 1: printf("Enter element: "); scanf("%d", &item); push(item); break;

case 2: item = pop(); if (item != -1) printf("Popped: %d\n", item); break;

case 3: palindrome(); break;

case 4: display(); break;

case 5: exit(0);

default: printf("Invalid choice!\n");

Expected output:

~~~~ MENU ~~~~

1. Push (with Overflow demo)

2. Pop (with Underflow demo)

3. Palindrome check

4. Display Stack

5. Exit

Enter choice: > 1

Enter element: > 10


~~~~ MENU ~~~~

1. Push (with Overflow demo)

2. Pop (with Underflow demo)

3. Palindrome check

4. Display Stack

5. Exit

Enter choice: > 1

Enter element: > 20

~~~~ MENU ~~~~

1. Push (with Overflow demo)

2. Pop (with Underflow demo)

3. Palindrome check

4. Display Stack

5. Exit

Enter choice: > 1

Enter element: > 30

~~~~ MENU ~~~~

1. Push (with Overflow demo)

2. Pop (with Underflow demo)

3. Palindrome check

4. Display Stack

5. Exit

Enter choice: > 4

Stack elements:

| 30 |
| 20 |

| 10 |

~~~~ MENU ~~~~

1. Push (with Overflow demo)

2. Pop (with Underflow demo)

3. Palindrome check

4. Display Stack

5. Exit

Enter choice: > 3

Stack elements:

| 30 |

| 20 |

| 10 |

It is not a Palindrome

~~~~ MENU ~~~~

1. Push (with Overflow demo)

2. Pop (with Underflow demo)

3. Palindrome check

4. Display Stack

5. Exit

Enter choice: > 2

Popped: 30

~~~~ MENU ~~~~


1. Push (with Overflow demo)

2. Pop (with Underflow demo)

3. Palindrome check

4. Display Stack

5. Exit

Enter choice: > 2

Popped: 20

~~~~ MENU ~~~~

1. Push (with Overflow demo)

2. Pop (with Underflow demo)

3. Palindrome check

4. Display Stack

5. Exit

Enter choice: > 2

Popped: 10

~~~~ MENU ~~~~

1. Push (with Overflow demo)

2. Pop (with Underflow demo)

3. Palindrome check

4. Display Stack

5. Exit

Enter choice: > 2

~~~ Stack Underflow ~~~

~~~~ MENU ~~~~


1. Push (with Overflow demo)

2. Pop (with Underflow demo)

3. Palindrome check

4. Display Stack

5. Exit

Enter choice: > 1

Enter element: > 1

~~~~ MENU ~~~~

Enter choice: > 1

Enter element: > 2

~~~~ MENU ~~~~

Enter choice: > 1

Enter element: > 1

~~~~ MENU ~~~~

Enter choice: > 3

Stack elements:

|1|

|2|

|1|

It is a Palindrome

~~~~ MENU ~~~~

Enter choice: > 5

You might also like