栈
只能在一边进出,先进的后出。
进出的一端叫做栈顶,另一端叫做栈底。
栈可以使用顺序存储结构,也能使用链式存储结构。
注意:栈只能在一端进行操作,这是栈的关键特征,也就是说栈不允许在中间进行查找、插入、删除等操作,(但是在实际应用中我们可以打破它)。
这里掌握初始化、入栈、出栈、取栈顶元素操作即可。
顺序存储结构实现栈
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| #include<iostream> using namespace std;
#define MAX_SIZE 128 typedef int DataType;
typedef struct _Stack { DataType* top; DataType* base; }Stack;
bool initStack(Stack& S) { S.base = new int[MAX_SIZE]; if (!S.base) { return false; } S.top = S.base; return true; }
bool pushStack(Stack& S, DataType data) { if (!S.base) { return false; } if (S.top - S.base == MAX_SIZE) { return false; }
*(S.top) = data; S.top++; return true; }
DataType popStack(Stack& S) { if (S.top != S.base) { return *(--S .top); } else { return -1; } }
DataType getTop(Stack& S) { if (S.top - S.base == 0) { return -1; } return *(S.top-1); } int main(void) { Stack S; initStack(S); int n = 0; int m = 0; cin >> n; m = n; while (n) { pushStack(S, n); n--; } cout << "____" << endl; cout << getTop(S) << endl; cout << "____" << endl; while (m) { cout << popStack(S) << endl; m--; } return 0; }
|
入栈操作图示:
![image-20211014120918328](/images/%E3%80%90%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E3%80%91%E6%A0%88(C++).assets/image-20211014120918328.png)
链接存储结构实现栈
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| #include<iostream> using namespace std;
typedef int DataType;
#define MAX_SZIE 128
typedef struct _Snode { DataType data; struct _Snode* next; }Snode;
typedef struct _Stack { int count; Snode* base; Snode* top; }Stack;
bool initStack(Stack* S) { if (!S) { return false; } S->base = S->top = NULL; S->count = 0; return true; }
bool pushStack(Stack* S, Snode* node) { if (!S || !node) { return false; }
if (S->count == 0) { S->base = S->top = node; S->top->next = NULL; S->count++; } else { S->top->next = node; S->top = node; S->count++; } return false; }
bool popStack(Stack* S) { if (!S || S->count == 0) { return false; } Snode* tempnode = S->base;
while (tempnode->next != S->top && S->count != 1) { tempnode = tempnode->next; } if (S->count == 1) { S->base = NULL; } delete S->top; S->top = tempnode; S->top->next = NULL; S->count--; return true; }
bool getTop(Stack* S,DataType* m_data) { if (!S || S->count == 0) { return S; } *m_data = S->top->data; return false; }
bool isEmpty(Stack* S) { if (S->count == 0) { return true; } return false; } int main(void) { Stack* S = new Stack; initStack(S);
int n = 5; int m = n; while (n) { Snode* tempnode = new Snode; tempnode->data = n; pushStack(S, tempnode); n--; }
while (m >0) { m--; }
cout << S->top->data << " "; popStack(S);
cout << S->top->data << " "; popStack(S);
cout << S->top->data << " "; popStack(S); cout << S->top->data << " "; popStack(S);
cout << S->top->data << " "; popStack(S);
if (!isEmpty(S)) { cout << S->top->data << " "; }
int temp = 0; getTop(S, &temp); cout << temp << endl;
delete S; return 0; }
|
补充:要修改指针指向地址,可以传递二级指针。
一级指针修改值。
实际应用
迷宫求解
链接——【栈】实现迷宫求解(C++)(详解)
表达式求值
链接——【数据结构】栈(C++)