### 思路
1. **初始化栈**:分配初始大小为 `STACK_INIT_SIZE` 的内存空间,并将 `base` 和 `top` 指针指向该空间的起始位置。
2. **入栈**:检查栈是否已满,如果已满则扩展栈的存储空间。将新元素插入栈顶,并更新 `top` 指针。
3. **出栈**:检查栈是否为空,如果不为空则删除栈顶元素,并更新 `top` 指针。
4. **获取栈顶元素**:检查栈是否为空,如果不为空则返回栈顶元素。
5. **获取栈的长度**:计算并返回栈的元素个数。
6. **遍历栈**:从栈顶到栈底依次输出栈中的每个元素。
### 伪代码
```
function InitStack(S):
S.base = allocate memory of size STACK_INIT_SIZE
S.top = S.base
S.stacksize = STACK_INIT_SIZE
return OK
function Push(S, e):
if S.top - S.base >= S.stacksize:
reallocate memory for S.base with size S.stacksize + STACKINCREMENT
S.top = S.base + S.stacksize
S.stacksize += STACKINCREMENT
S.top = e
S.top += 1
return OK
function Pop(S, e):
if S.top == S.base:
return ERROR
S.top -= 1
e = S.top
return OK
function GetTop(S, e):
if S.top == S.base:
return ERROR
e = S.top - 1
return OK
function StackLength(S):
return S.top - S.base
function StackTraverse(S):
if S.top == S.base:
print "The Stack is Empty!"
else:
print "The Stack is: "
p = S.top - 1
while p >= S.base:
print *p
p -= 1
return OK
```
### C++代码
#include <malloc.h>
#include <stdio.h>
#define OK 1
#define ERROR 0
#define STACK_INIT_SIZE 100 // 存储空间初始分配量
#define STACKINCREMENT 10 // 存储空间分配增量typedef int SElemType; // 定义栈元素类型
typedef int Status; // Status是函数的类型,其值是函数结果状态代码,如OK等struct SqStack {SElemType *base; // 在栈构造之前和销毁之后,base的值为NULLSElemType *top; // 栈顶指针int stacksize; // 当前已分配的存储空间,以元素为单位
}; // 顺序栈Status InitStack(SqStack &S) {S.base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));if (!S.base) return ERROR;S.top = S.base;S.stacksize = STACK_INIT_SIZE;return OK;
}Status Push(SqStack &S, SElemType e) {if (S.top - S.base >= S.stacksize) {S.base = (SElemType *)realloc(S.base, (S.stacksize + STACKINCREMENT) * sizeof(SElemType));if (!S.base) return ERROR;S.top = S.base + S.stacksize;S.stacksize += STACKINCREMENT;}*S.top++ = e;return OK;
}Status Pop(SqStack &S, SElemType &e) {if (S.top == S.base) return ERROR;e = *--S.top;return OK;
}Status GetTop(SqStack S, SElemType &e) {if (S.top == S.base) return ERROR;e = *(S.top - 1);return OK;
}int StackLength(SqStack S) {return S.top - S.base;
}Status StackTraverse(SqStack S) {SElemType *p = S.top - 1;if (S.top == S.base) {printf("The Stack is Empty!\n");} else {printf("The Stack is: ");while (p >= S.base) {printf("%d ", *p--);}printf("\n");}return OK;
}int main() {int a;SqStack S;SElemType x, e;if (InitStack(S) == OK) {printf("A Stack Has Created.\n");}while (1) {printf("1:Push \n2:Pop \n3:Get the Top \n4:Return the Length of the Stack\n5:Load the Stack\n0:Exit\nPlease choose:\n");scanf("%d", &a);switch (a) {case 1:scanf("%d", &x);if (Push(S, x) == ERROR) printf("Push Error!\n");else printf("The Element %d is Successfully Pushed!\n", x);break;case 2:if (Pop(S, e) == ERROR) printf("Pop Error!\n");else printf("The Element %d is Successfully Poped!\n", e);break;case 3:if (GetTop(S, e) == ERROR) printf("Get Top Error!\n");else printf("The Top Element is %d!\n", e);break;case 4:printf("The Length of the Stack is %d!\n", StackLength(S));break;case 5:StackTraverse(S);break;case 0:return 1;}}
}