#include #include #include "sqstack.h" sqstack_t pkt_creat_sqstack(int len) { sqstack_t s; if((s = (sqstack_t)malloc(sizeof(struct sqstack))) == NULL) { printf("malloc failed!\n"); return NULL; } if((s->frame = (stack_frame_t)malloc(sizeof(struct stack_frame) * len)) == NULL) { printf("malloc failed!\n"); return NULL; } s->max_len = len; s->top = -1; return s; } int pkt_sqstack_empty(sqstack_t s) { return s->top == -1 ? 1 : 0; } int pkt_sqstack_full(sqstack_t s) { return s->top == (s->max_len - 1) ? 1 : 0; } void pkt_clear_sqstack(sqstack_t s) { s->top = -1; } stack_frame_t pkt_sqstack_pop(sqstack_t s) { if(pkt_sqstack_empty(s)) { printf("sqstack is empty!\n"); return NULL; } s->top--; return &s->frame[s->top + 1]; } int pkt_sqstack_push(sqstack_t s, struct stack_frame frame) { if(pkt_sqstack_full(s)) { printf("sqstack is full!\n"); return -1; } s->frame[s->top + 1] = frame; s->top++; return 0; } stack_frame_t pkt_get_sqstack_top(sqstack_t s) { return &s->frame[s->top]; } void pkt_sqstack_free(sqstack_t s) { free(s->frame); free(s); } int main(void) { sqstack_t s = pkt_creat_sqstack(10); struct stack_frame frame; for(int i = 1; i <= 10; i++) { frame.data = i; pkt_sqstack_push(s, frame); } while(!pkt_sqstack_empty(s)) { printf("%d\n", pkt_sqstack_pop(s)->data); } return 0; }