Thursday, 31 July 2025

Implement stack using array with following operations: push, pop, print, peek, peep, change, exit.

Implement stack using array with following operations: push, pop, print, peek, peep, change, exit.

stack_operations.c
#include <stdio.h>
#include <stdlib.h>
#define MAX 100

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

// Push operation
void push(int value) {
    if (top >= MAX - 1) {
        printf("Stack Overflow! Cannot push.\n");
    } else {
        top++;
        stack[top] = value;
        printf("%d pushed to stack.\n", value);
    }
}

// Pop operation
int pop() {
    if (top == -1) {
        printf("Stack Underflow! Cannot pop.\n");
        return -1;
    } else {
        return stack[top--];
    }
}

// Peek operation
void peek() {
    if (top == -1) {
        printf("Stack is empty!\n");
    } else {
        printf("Top element is: %d\n", stack[top]);
    }
}

// Peep operation
void peep(int position) {
    int index = top - position + 1;
    if (index < 0 || index > top) {
        printf("Invalid position! Cannot peep.\n");
    } else {
        printf("Element at position %d from top is: %d\n", position, stack[index]);
    }
}

// Change operation
void change(int position, int value) {
    int index = top - position + 1;
    if (index < 0 || index > top) {
        printf("Invalid position! Cannot change.\n");
    } else {
        stack[index] = value;
        printf("Element at position %d changed to %d.\n", position, value);
    }
}

// Print operation
void print() {
    if (top == -1) {
        printf("Stack is empty!\n");
    } else {
        printf("Stack elements (top to bottom):\n");
        for (int i = top; i >= 0; i--) {
            printf("%d\n", stack[i]);
        }
    }
}

int main() {
    int choice, value, position;

    while (1) {
        printf("\n\n***** STACK OPERATIONS MENU *****\n");
        printf("1. Push\n2. Pop\n3. Peek\n4. Peep\n5. Change\n6. Print\n7. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

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

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

            case 3:
                peek();
                break;

            case 4:
                printf("Enter position from top to peep: ");
                scanf("%d", &position);
                peep(position);
                break;

            case 5:
                printf("Enter position from top to change: ");
                scanf("%d", &position);
                printf("Enter new value: ");
                scanf("%d", &value);
                change(position, value);
                break;

            case 6:
                print();
                break;

            case 7:
                printf("Exiting program...\n");
                exit(0);

            default:
                printf("Invalid choice! Please try again.\n");
        }
    }

    return 0;
}

๐Ÿงพ Step-by-Step Explanation

๐Ÿ”น Stack Initialization

int stack[MAX];
int top = -1;
  • stack[MAX]: Array to store stack elements (MAX = 100)
  • top: Index of top element, initialized to -1 (empty stack)

๐Ÿ”น Push Operation

void push(int value) {
    if (top >= MAX - 1) {
        printf("Stack Overflow! Cannot push.\n");
    } else {
        top++;
        stack[top] = value;
        printf("%d pushed to stack.\n", value);
    }
}
  • Checks for stack overflow (when stack is full)
  • Increments top and adds new element
  • Provides feedback about the operation

๐Ÿ”น Pop Operation

int pop() {
    if (top == -1) {
        printf("Stack Underflow! Cannot pop.\n");
        return -1;
    } else {
        return stack[top--];
    }
}
  • Checks for stack underflow (when stack is empty)
  • Returns top element and decrements top
  • Returns -1 if stack is empty

๐Ÿ”น Peek Operation

void peek() {
    if (top == -1) {
        printf("Stack is empty!\n");
    } else {
        printf("Top element is: %d\n", stack[top]);
    }
}
  • Displays the top element without removing it
  • Checks if stack is empty first

๐Ÿ”น Peep Operation

void peep(int position) {
    int index = top - position + 1;
    if (index < 0 || index > top) {
        printf("Invalid position! Cannot peep.\n");
    } else {
        printf("Element at position %d from top is: %d\n", position, stack[index]);
    }
}
  • Views element at specific position from top
  • Calculates index using top - position + 1
  • Validates position is within bounds

๐Ÿ”น Change Operation

void change(int position, int value) {
    int index = top - position + 1;
    if (index < 0 || index > top) {
        printf("Invalid position! Cannot change.\n");
    } else {
        stack[index] = value;
        printf("Element at position %d changed to %d.\n", position, value);
    }
}
  • Modifies element at specific position from top
  • Uses same position calculation as peep operation
  • Provides feedback about the change

๐Ÿ”น Print Operation

void print() {
    if (top == -1) {
        printf("Stack is empty!\n");
    } else {
        printf("Stack elements (top to bottom):\n");
        for (int i = top; i >= 0; i--) {
            printf("%d\n", stack[i]);
        }
    }
}
  • Displays all stack elements from top to bottom
  • Checks if stack is empty first
  • Uses reverse loop to show top element first

๐Ÿงช Sample Output

***** STACK OPERATIONS MENU *****
1. Push
2. Pop
3. Peek
4. Peep
5. Change
6. Print
7. Exit
Enter your choice: 1
Enter value to push: 10
10 pushed to stack.

Enter your choice: 1
Enter value to push: 20
20 pushed to stack.

Enter your choice: 3
Top element is: 20

Enter your choice: 4
Enter position from top to peep: 2
Element at position 2 from top is: 10

Enter your choice: 5
Enter position from top to change: 2
Enter new value: 15
Element at position 2 changed to 15.

Enter your choice: 6
Stack elements (top to bottom):
20
15

Enter your choice: 2
Popped value: 20

Enter your choice: 7
Exiting program...

๐Ÿ“Œ Key Concepts Covered

Stack
LIFO (Last In First Out) data structure
Array Implementation
Using static memory allocation with fixed size
Core Operations
Push, Pop, Peek, Peep, Change, Print
Position Calculation
top - position + 1 for peep/change operations
Error Handling
Stack overflow and underflow conditions checked

No comments:

Post a Comment

Total Pageviews

Search This Blog

Write a program which performs the following operations using a simple queue. : insert() -> delete() -> display()

Write a program which performs the following operations using a simple queue. : insert() -> delete() -> display() ...