#include <stdio.h> #define MAXSIZE 1 << 10 typedef int elemtype; typedef struct { elemtype data[MAXSIZE]; int top; }seqstack; /* FUNCTION seqstack_init DESCRIPTION initialize the sequence stack PARAMETERS p_seq_s: the pointer to sequence stack RETURNS void */ void seqstack_init(seqstack * p_seq_s) { p_seq_s->top = 0; } /* FUNCTION seqstack_empty DESCRIPTION judge the sequence stack whether empty or not PARAMETERS p_seq_s: the pointer of sequence stack RETURNS 1: the sequence stack is empty 0: the sequence stack isn't empty */ int seqstack_empty(seqstack * p_seq_s) { if (p_seq_s->top == 0) { return 1; } else { return 0; } } /* FUNCTION seqstack_push DESCRIPTION push the element in the sequence stack PARAMETERS p_seq_s: the pointer of sequence stack RETURNS flag: the flag whether the push operation successful or not */ int seqstack_push(seqstack * p_seq_s, elemtype value) { int flag; if (p_seq_s->top == MAXSIZE) { flag = 0; } else { p_seq_s->data[p_seq_s->top++] = value; flag = 1; } return flag; } /* FUNCTION seqstack_pop DESCRIPTION pop the element from stack's top PARAMETERS p_seq_s: the pointer of sequence stack RETURNS flag: the flag whethe the pop operatrion successful or not */ int seqstack_pop(seqstack * p_seq_s) { int flag; if (p_seq_s->top == 0) { flag = 0; } else { p_seq_s->top--; flag = 1; } return flag; } /* FUNCTION seqstack_top DESCRIPTION get the top element in the stack PARAMETERS p_seq_s: the pointer of sequence stack RETURNS p_seq_s->data[p_seq_s->top - 1]: the top element in the sequence stack */ elemtype seqstack_top(seqstack * p_seq_s) { return p_seq_s->data[p_seq_s->top - 1]; } int main() { seqstack s; seqstack_init(&s); int i; for (i = 0; i < 20; i++) { seqstack_push(&s, i + 1); } while (!seqstack_empty(&s)) { printf("%d ", seqstack_top(&s)); seqstack_pop(&s); } return 0; }