I implemented a stack in C as follows. And the program is working fine for three pushes but when I try to push more than 3 elements the program executes and the result is printing out but then the error message displays saying that the program has stopped working
My code is as follows :
#include <stdio.h>
#include <stdlib.h>
#define MAX 50
//typedef char stackEntryType;
typedef enum{FALSE,TRUE} Boolean;
typedef struct stack{
int top;
int entry[MAX];
}stack;
void createStack(stack *s){
s->top=-1;
}
Boolean IsStackEmpty(const stack *s){
return(s->top==-1);
}
Boolean IsStackFull(const stack *s){
return(s->top==MAX-1);
}
void push(stack *s,int item){
if(IsStackFull(s)){
printf("Stack is full\n");
exit(1);
}else{
s->entry[++s->top]=item;
printf("%d pushed to stack\n",item);
}
}
void pop(stack *s)
{
int item;
if(IsStackEmpty(s))
{
printf("Stack is empty\n");
exit(1);
}
else{
item=s->entry[s->top--];
printf("%d popped from the stack",item);
}
}
void main()
{
stack *s;
createStack(&s);
push(&s,1);
push(&s,2);
push(&s,3);
push(&s,4);
pop(&s);
}
Can someone resolve this issue?