Thursday, 24 August 2023

STACK

 #include <stdio.h>

#include <stdlib.h>  // For system("clear")

#define Max 10


int st[Max], top = -1;


void push(int st[], int val);

int pop(int st[]);

int peak(int st[]);

void display(int st[]);


int main() {

    int val, option;

    system("clear");


    do {

        printf("*****CHOOSE OPTION******\n");

        printf("1. Push\n");

        printf("2. Pop\n");

        printf("3. Peak\n");

        printf("4. Display\n");

        printf("5. Exit\n");


        printf("Enter your option: ");

        scanf("%d", &option);


        switch(option) {

            case 1:

                printf("Enter the number to Push: ");

                scanf("%d", &val);

                push(st, val);

                break;   ////

  • case 1:: Handles the case where the user chooses to push a value onto the stack.

  • printf("Enter the number to Push: ");: Prompts the user to enter a value to push onto the stack.

  • scanf("%d", &val);: Reads the value from the user and stores it in the val variable.

  • push(st, val);: Calls the push function to push the entered value onto the stack.

  • break;:


            case 2:

                val = pop(st);

                if(val != -1)

                    printf("The value deleted from the stack is %d\n", val);

                break;

Case 2 Explanation (Pop Operation):

This case corresponds to the "Pop" operation in the menu. When the user selects option 2, the program performs the pop operation on the stack. Let's break down the lines of code within this case:

  • val = pop(st);: This line calls the pop function with the st array as an argument. The pop function returns the value that was removed from the stack (or -1 in case of underflow) and assigns it to the val variable. This allows you to capture the value that was popped from the stack for later display.

  • if (val != -1): This if statement checks whether the val variable holds a value other than -1. A value of -1 indicates an underflow condition, which means the stack was empty and the pop operation could not be performed. If val is not -1, it means the pop operation was successful.

  • printf("The value deleted from the stack is %d\n", val);: If the pop operation was successful (i.e., val is not -1), this line prints the value that was popped from the stack. This provides feedback to the user about the removed element.

  • break;: This line ends the execution of case 2. After performing the pop operation and displaying the value (if applicable), the program exits the switch statement and continues with the loop to display the menu again.



            case 3:

                val = peak(st);

                if(val != -1)

                    printf("The value on top is %d\n", val);

                break;


Case 3 Explanation (Peak Operation):

This case corresponds to the "Peak" operation in the menu. When the user selects option 3, the program performs the peak operation on the stack. The peak operation retrieves the value at the top of the stack without removing it. Let's break down the lines of code within this case:

  • val = peak(st);: This line calls the peak function with the st array as an argument. The peak function returns the value at the top of the stack (or -1 in case of an empty stack) and assigns it to the val variable. This allows you to capture the value at the top of the stack for later display.

  • if (val != -1): This if statement checks whether the val variable holds a value other than -1. A value of -1 indicates an empty stack, and the peak operation cannot be performed.

  • printf("The value on top is %d\n", val);: If the peak operation was successful (i.e., val is not -1), this line prints the value that is currently at the top of the stack. This provides feedback to the user about the top element without removing it.

  • break;: This line ends the execution of case 3. After performing the peak operation and displaying the value (if applicable), the program exits the switch statement and continues with the loop to display the menu again.

            case 4:

                display(st);

                break;

        }

    } while(option != 5);


    getchar();  // Wait for a keypress

    return 0;

}


void push(int st[], int val) {

    if(top == Max - 1) {

        printf("Stack is overflow or full\n");

    } else {

        top++;

        st[top] = val;

    }

}


int pop(int st[]) {

    int val;

    if(top == -1) {

        printf("\nStack underflow or empty\n");

        return -1;

    } else {

        val = st[top];

        top--;

        return val;

    }

}


void display(int st[]) {

    int i;

    if(top == -1) {

        printf("\nStack is underflow or empty\n");

    } else {

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

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

        printf("\n");

    }

}


int peak(int st[]) {

    if(top == -1) {

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

        return -1;

    } else {

        return st[top];

    }

}


No comments:

Post a Comment