Thursday, 28 July 2022

Stack Program in C language


#include<stdio.h>

#include<conio.h>

#define n 5


void push();

void pop();

void peek();

void display();


int stack[n],top=-1;


void main()

{

    int ch;

    clrscr();


    printf("1.push\n2.pop\n3.peek\n4.display\n5.exit\n");

    do

    {

        printf("\nenter your choice: ");

        scanf("%d",&ch);


        switch(ch)

        {

            case 1: push();

                    break;


            case 2: pop();

                    break;


            case 3: peek();

                    break;


            case 4: display();

                    break;


            case 5: exit(0);


            default: printf("\n!!  enter valid choice  !!\n");

        }

    }while(ch != 5);


    getch();

}


void push()

{

    int val;

    if(top == n-1)

    {

        printf("\n\t\t\t!!  your stack is full  !!\n");

    }

    else

    {

        printf("enter value: ");

        top=top+1;

        scanf("%d",&stack[top]);

    }

}


void pop()

{

    if(top == -1)

    {

        printf("\n\t\t\t!!  your stack is empty  !!\n");

    }

    else

    {

       printf("your value %d is deleted successfully\n",stack[top]);

       top = top - 1;

    }

}


void display()

{

    int i;


    if(top == -1)

    {

        printf("\n\t\t\t!!  your stack is empty  !!\n");

    }

    else

    {

        for(i=top; i!=-1; i--)

        {

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

        }

    }

}


void peek()

{

    if(top == -1)

    {

        printf("\n\t\t\t!!  your stack is empty  !!\n");

    }

    else

    {

        printf("your top element is: %d\n",stack[top]);

    }

} 

No comments:

Post a Comment

python programs

1. sum of two number