Stack
Stack
h>
#define MAXSIZE 1000
struct stack{
int elements[MAXSIZE];
int top;
};
int IsStackempty(struct stack *S){
return S->top == -1;
}
int IsStackfull(struct stack *S){
return S->top == MAXSIZE-1;
}
void push(struct stack *S, int item){
if(IsStackfull(S)){
printf("stack overflow! \n");
}
S->top++;
S->elements[S->top]=item;
}
int pop(struct stack *S){
if(IsStackempty(S)){
printf("Stack underflow! \n");
}
int item = S-> elements[S->top];
S->top--;
return item;
}
void printstack(struct stack *S){
if(IsStackempty(S)){
printf("Stack is empty.\n");
}
else{
printf("stack contents(top to bottom):\n");
for(int i=S->top; i>=0; i--){
printf("%d\n", S->elements[i]);
}
}
}
int main(){
struct stack source;
source.top= -1;
IsStackfull(&source);
push(&source, 10);
push(&source, 20);
push(&source, 30);
printf("print stack before pop:\n");
printstack(&source);
IsStackempty(&source);
pop(&source);
printf("print stack:\n");
printstack(&source);
}