Written with StackEdit中文版.
顺序栈
简介
栈,是限定仅在表尾进行插入或删除的线性表,其表尾称为栈顶,表头称为栈底。栈具有后进先出的特点,即先入栈的元素后出栈。
栈有两种存储表示方法:
- 顺序栈 — 即栈的顺序存储结构,是用一组地址连续的存储单元依次存放自栈底到栈顶的元素。
- 链栈 — 即栈的链式表示,其实现方法与单链表类似,这里不实现。
声明与定义
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| typedef int SElemType; typedef int Status; #define STACK_INIT_SIZE 10 #define STACKINCREMENT 10 #define OK 1 #define ERROR 0 #define OVERFLOW -1
typedef struct { SElemType* base; SElemType* top; int stacksize; }Sqstack;
Status InitStack(Sqstack *S);
Status StackEmpty(Sqstack S);
Status GetTop(Sqstack S, SElemType *e);
Status Push(Sqstack *S, SElemType e);
Status Pop(Sqstack *S, SElemType *e);
int StackLength(Sqstack S);
Status StackTraverse(Sqstack S);
|
相关方法实现
InitStack
1 2 3 4 5 6 7 8 9 10 11
| Status InitStack(Sqstack *S){ S->base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType)); if (!S->base){ printf("存储分配失败!\n"); exit(OVERFLOW); } S->top = S->base; S->stacksize = STACK_INIT_SIZE; return OK; }
|
StackEmpty
1 2 3
| Status StackEmpty(Sqstack S){ return S.top == S.base; }
|
GetTop
1 2 3 4 5 6 7 8
| Status GetTop(Sqstack S, SElemType *e){ if (StackEmpty(S)){ printf("栈空!\n"); return ERROR; } *e = *(S.top); return OK; }
|
Push
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| Status Push(Sqstack *S, SElemType e){ if (S->top - S->base >= S->stacksize) { int new_size = S->stacksize + STACKINCREMENT; SElemType* new_base = (SElemType*)realloc(S->base, new_size * sizeof(SElemType)); if (!new_base){ printf("追加存储分配失败!\n"); exit(OVERFLOW); } S->base = new_base; S->top = S->base + S->stacksize - 1; S->stacksize += STACKINCREMENT; } *++(S->top) = e; return OK; }
|
Pop
1 2 3 4 5 6 7 8
| Status Pop(Sqstack *S, SElemType *e){ if (StackEmpty(*S)){ printf("栈空!\n"); return ERROR; } *e = *S->top--; return OK; }
|
StackLength
1 2 3 4 5 6 7
| Status StackLength(Sqstack S){ if (StackEmpty(S)){ printf("栈空!\n"); return ERROR; } return (int)(S.top - S.base); }
|
StackTraverse
1 2 3 4 5 6 7 8 9 10
| Status StackTraverse(Sqstack S) { SElemType e; printf("遍历顺序栈: "); while (!StackEmpty(S)){ Pop(&S,&e); printf("%d ", e); } printf("遍历结束\n"); return OK; }
|
方法测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| int main(void){ SElemType e; int size = 0;
Sqstack MyStack; InitStack(&MyStack);
printf("输入初始栈长!\n"); scanf_s("%d", &size); printf("输入%d个元素入栈!\n", size); for (int i = 0; i < size; i++){ scanf_s("%d", &e); Push(&MyStack, e); } StackTraverse(MyStack); GetTop(MyStack, &e); printf("栈顶元素是%d\n", e);
printf("再输入一个数入栈!\n"); scanf_s("%d", &e); Push(&MyStack, e); printf("此时栈长为%d\n", StackLength(MyStack)); StackTraverse(MyStack);
return 0; }
|