Simple stack program for performing all stack operations.

            Execution of Stack data structure.

#include<stdio.h>

#include<stdlib.h>

int SIZE = 30;

int stack[30], top = -1;

void push(int value)

{

    if(top == SIZE-1)

        printf("\nOverflow. Stack is Full");

    else

    {

        top++;

        stack[top] = value;

        printf("\nInsertion was successful");

    }

}

void pop()

{

    if(top == -1)

        printf("\nUnderflow. Stack is empty");

    else

    {

        printf("\nDeleted : %d", stack[top]);

        top--;

    }

}

void display()

{

    if(top == -1)

        printf("\nStack is Empty!");

    else

    {   int i;

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

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

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

    }

}

void peek()

{

    if(top == -1)

    {

        printf("The Stack is empty!");

    }

    else

    {

        printf("The top most elememt in stack is :%d",stack[top]);

    }

}

void main()

{

    int value, choice;

    while(1)

    {

        printf("\n\n------MENU-------\n");

        printf("1. Push\n2. Pop\n3. Display\n4. Peek\n5. Exit");

        printf("\nEnter your choice: ");

        scanf("%d",&choice);

        switch(choice)

        {

        case 1:

            printf("Enter the value to be inserted: ");

            scanf("%d",&value);

            push(value);

            break;

        case 2:

            pop();

            break;

        case 3:

            display();

            break;

        case 4:

            peek();

            break;

        case 5:

            exit(0);

        default:

            printf("\nWrong selection, please try again.");

        }

    }

}     



INPUT and OUTPUT:


Post a Comment

0 Comments