1 堆栈的主要操作
2 栈的顺序存储
栈的顺序存储结构通常由一个一维数组和一个记录 栈顶元素位置的变量组成。
#include<iostream>
#include<vector>
using namespace std;
#define MAX 100
typedef struct SNode* StackPtr;
struct SNode {
int data[MAX];
int top;
};
StackPtr pushStack(StackPtr Stack, int value) {
if (Stack->top == MAX) {
cout << "This stack is fool." << endl;
}
else {
++(Stack->top);
Stack->data[Stack->top] = value;
}
return Stack;
}
int popStack(StackPtr &Stack) {
if (Stack->top == -1) {
cout << "This stack is null." << endl;
return -101;
}
else {
int data = Stack->data[Stack->top];
(Stack->top)--;
return data;
}
}
int main() {
StackPtr stack = new SNode;
stack->top = -1;
auto a = vector<int>(10, 4);
for (auto i : a) {
pushStack(stack, i);
auto data = popStack(stack);
cout << data << endl;
}
return 0;
}
3 堆栈的链式存储
|