C program to create a stack (static) -DSA



#include<stdio.h>

#define max 3

typedef struct stack

{

    int data[max];

    int top;

} stack;

void push(stack *ps,int num)

{

    ps->data[++ps->top]=num;

}

int pop(stack *ps)

{

    int num;

    num=ps->data[ps->top--];

    printf("Popped value:%d\n",num);

}

int isempty(stack *ps)

{

    if(ps->top==-1)

        return 1;

    return 0;

}

int isfull(stack *ps)

{

    if(ps->top==max-1)

        return 1;

    return 0;

}

int main()

{

    stack s;

    s.top=-1;

    int choice,num,i;

    do

    {

        printf("1.Push\n2.pop\n3.Exit\n\n");

        printf("Enter: ");

        scanf("%d",&choice);

        switch(choice)

        {

        case 1:

            if(!isfull(&s))

            {

                printf("Enter value to push:");

                scanf("%d",&num);

                push(&s,num);

            }

            else

                printf("Overflow\n");

            break;

        case 2:

            if(!isempty(&s))

                pop(&s);

            else

                printf("Underflow\n");

            break;

        }

    }

    while(choice!=3);

}

Post a Comment

0 Comments