登 录
头文件:
sqstack.h
/* * sqstack.h */ #ifndef SQSTACK_H_ #define SQSTACK_H_ #define OK 1 #define ERROR -1 #define MAXSZ 50 typedef char ElemType; typedef struct { ElemType elem[MAXSZ]; int top; } sqstack; int InitStack(sqstack *s); int Push(sqstack *s, const ElemType e); int Pop(sqstack *s); int StackLength(const sqstack *s); int StackEmpty(const sqstack *s); ElemType GetTop(sqstack *s); int PrintStack(const sqstack *s); #endif /* SQSTACK_H_ */
代码文件:
sqstack.c
/* * sqstack.c */ #include <stdio.h> #include <stdlib.h> #include "sqstack.h" int InitStack(sqstack *s) { if (NULL == s) { printf("ERROR: The stack is NULL!/n"); return ERROR; } s->top = -1; return OK; } int Push(sqstack *s, const ElemType e) { if (MAXSZ - 1 == s->top) { printf("ERROR: The stack is full!/n"); return ERROR; } s->top++; s->elem[s->top] = e; return OK; } int Pop(sqstack *s) { if (-1 == s->top) { printf("ERROR: The stack is empty!/n"); return ERROR; } s->top--; return OK; } int StackLength(const sqstack *s) { if (NULL == s) { printf("ERROR: The stack is NULL!/n"); return ERROR; } return s->top + 1; } int StackEmpty(const sqstack *s) { if (NULL == s) { printf("ERROR: The stack is NULL!/n"); return ERROR; } return (-1 == s->top); } ElemType GetTop(sqstack *s) { if (StackEmpty(s)) { printf("ERROR: The stack is empty!/n"); return '/0'; } return s->elem[s->top]; } int PrintStack(const sqstack *s) { if (NULL == s) { printf("ERROR: The stack is NULL!/n"); return ERROR; } printf("===============/n"); int i; for (i = s->top; i > -1; --i) { printf("%c", s->elem[i]); } printf("/n"); printf("===============/n"); return OK; }
main函数文件:
SeqStack.c
/* ============================================================================ Name : SeqStack.c Author : Version : Copyright : Copyright@DK Description : Hello World in C, Ansi-style ============================================================================ */ #include <stdio.h> #include <stdlib.h> #include "sqstack.h" int main(void) { sqstack s; InitStack(&s); Push(&s,'o'); Push(&s,'l'); Push(&s,'l'); Push(&s,'e'); Push(&s,'h'); PrintStack(&s); printf("The length is %d/n",StackLength(&s)); Pop(&s); Pop(&s); printf("%c/n", GetTop(&s)); PrintStack(&s); Pop(&s); Pop(&s); Pop(&s); Pop(&s); Push(&s, '1'); PrintStack(&s); return EXIT_SUCCESS; }
抱歉!评论已关闭.