Data Structure lab

Program 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 <string.h>
#define MAX 100

int stack[MAX], top = -1;

// Function to push element
void push(int val) {
    if (top == MAX - 1)
        printf("Stack Overflow!\n");
    else
        stack[++top] = val;
}

// Function to pop element
void pop() {
    if (top == -1)
        printf("Stack Underflow!\n");
    else
        printf("Popped: %d\n", stack[top--]);
}

// Function to display stack
void display() {
    if (top == -1)
        printf("Stack is Empty\n");
    else {
        printf("Stack: ");
        for (int i = 0; i <= top; i++)
            printf("%d ", stack[i]);
        printf("\n");
    }
}

// Function to check palindrome
void checkPalindrome() {
    int isPalin = 1;
    for (int i = 0; i <= top / 2; i++) {
        if (stack[i] != stack[top - i]) {
            isPalin = 0;
            break;
        }
    }
    if (top == -1)
        printf("Stack is empty, cannot check palindrome.\n");
    else
        printf("Stack is %sa palindrome\n", isPalin ? "" : "not ");
}

int main() {
    int choice, val;

    do {
        printf("\n1.Push\n2.Pop\n3.Display\n4.Check Palindrome\n5.Exit\nEnter choice: ");
        scanf("%d", &choice);
        switch (choice) {
            case 1: printf("Enter value: ");
                    scanf("%d", &val);
                    push(val); break;
            case 2: pop(); break;
            case 3: display(); break;
            case 4: checkPalindrome(); break;
            case 5: printf("Exiting...\n"); break;
            default: printf("Invalid choice\n");
        }
    } while (choice != 5);

    return 0;
}



               


  

Output :-

1.Push
2.Pop
3.Display
4.Check Palindrome
5.Exit
Enter choice: 1
Enter value: 21

1.Push
2.Pop
3.Display
4.Check Palindrome
5.Exit
Enter choice: 1
Enter value: 22

1.Push
2.Pop
3.Display
4.Check Palindrome
5.Exit
Enter choice: 1
Enter value: 40

1.Push
2.Pop
3.Display
4.Check Palindrome
5.Exit
Enter choice: 3
Stack: 21 22 40 

1.Push
2.Pop
3.Display
4.Check Palindrome
5.Exit
Enter choice: 2
Popped: 40

1.Push
2.Pop
3.Display
4.Check Palindrome
5.Exit
Enter choice: 2
Popped: 22

1.Push
2.Pop
3.Display
4.Check Palindrome
5.Exit
Enter choice: 2
Popped: 21

1.Push
2.Pop
3.Display
4.Check Palindrome
5.Exit
Enter choice: 2
Stack Underflow!

1.Push
2.Pop
3.Display
4.Check Palindrome
5.Exit
Enter choice: 2
Stack Underflow!

1.Push
2.Pop
3.Display
4.Check Palindrome
5.Exit
Enter choice: 3
Stack is Empty

1.Push
2.Pop
3.Display
4.Check Palindrome
5.Exit
Enter choice: 1
Enter value: 48

1.Push
2.Pop
3.Display
4.Check Palindrome
5.Exit
Enter choice: 1
Enter value: 28

1.Push
2.Pop
3.Display
4.Check Palindrome
5.Exit
Enter choice: 1
Enter value: 48

1.Push
2.Pop
3.Display
4.Check Palindrome
5.Exit
Enter choice: 4
Stack is a palindrome

1.Push
2.Pop
3.Display
4.Check Palindrome
5.Exit
Enter choice: 1
Enter value: 48

1.Push
2.Pop
3.Display
4.Check Palindrome
5.Exit
Enter choice: 1
Enter value: 28

1.Push
2.Pop
3.Display
4.Check Palindrome
5.Exit
Enter choice: 1
Enter value: 38

1.Push
2.Pop
3.Display
4.Check Palindrome
5.Exit
Enter choice: 4
Stack is not a palindrome

1.Push
2.Pop
3.Display
4.Check Palindrome
5.Exit
Enter choice: 5
Exiting...
  
Comments

Replay !

0 Comments

Share Your Thoughts

Please enter your name
Please enter a valid email
Password must be at least 6 characters
Please enter your comment
Email Verification Required
We've sent a 6-digit verification code to . Please enter the code below to verify your email address.
Email Verified Successfully!
Your email has been verified. Would you like to proceed with posting your comment?

Type "YES" to confirm and post your comment, or click Cancel to skip posting.