helllo stack c program

#include <stdio.h>

#include <stdlib.h>

#define MAXSIZE 3

typedef struct

{

    int items[MAXSIZE];

    int top;

} STACK;

int isfull(STACK s)

{

    if (s.top==MAXSIZE-1)

    {

        return 1;

    }

    return 0;

}

int isempty(STACK s)

{

    if (s.top==-1)

    {

        return 1;

    }

    return 0;

}

void push(STACK *s,int data){

s->items[++s->top]=data;

printf (“\n %d is pushed onto stak”,data);

}

int pop(STACK *s){

return (s->items[s->top–]);

}

int peek (STACK s){

return (s.items[s.top]);

}

void Display(STACK s){

int i;

printf(“\n STACK CONTENTS:\n BOS->”);

for(i=0;i<=s.top;i++){

printf(“%d ->”,s.items[i]);

}

printf(“TOS”);

}

int main()

{

    int choice,data;

    STACK s;

    s.top=-1;

    while(1){

    printf(“\n\n1:PUSH\n2:POP\n3:PEEK\n4:DISPLAY\n5:EXIT\n”);

    printf(“Enter your Choice:”);

    scanf(“%d”,&choice);

    switch(choice){

    case 1:if(isfull(s)){

    printf(“STACK OVERFLOW”);

    }

    else{

    printf(“Enter the data to be pushed:”);

    scanf(“%d”,&data);

    push(&s,data);

    }

    break;

    case 2: if(isempty(s)){

    printf(“STACK UNDERFLOW”);

    }

    else{

    printf(“\n %d is popped from top of the stack”,pop(&s));

    }

    break;

    case 3: if(isfull(s)){peek(s)

    printf(“STACK OVERFLOW”);

    }

    else{

    printf(“%d”,peek(s));}

    break;

    case 4: if(isempty(s)){

    printf(“STACK UNDERFLOW”);

    }

    else {

    Display(s);

    }

    break ;

    case 5: exit (0);

    default : printf(“\n Invalid choice!!!”);

    }

    }

    return 0;

}

Scroll to Top