1. The concept of stack
1) It has been written when introducing the stack STL, you can look it up, portal: click to open the link
2) I didn't mention the bottom of the stack, the bottom of the stack: it refers to the -1 subscript in the sequential stack, and everyone can look down.
Second, the sequence stack
1. Sequence stack diagram
2. Features
1) The space is limited, because the array space is allocated at one time, and the stack is full .
2) Popping into the stack is the change of the subscript top. The actual popping is only a one-bit backward subscript , and the actual value is still in the array, which wastes memory.
3. Throw a piece of code
/* sequential stack */ #include<iostream> using namespace std; #define MaxSize 100 template <class T> class Stack { private: T Data[MaxSize]; int Top; public : Stack(); int Push(T Elem); //Push the stack int Pop(); //Pop stack int StackEmpty(); //stack empty returns 1 int StackFull(); //return 1 when the stack is full int GetTop(T& TopData); //Get the top element of the stack }; template<class T> Stack<T>::Stack() :Top(-1) {}; template<class T> int Stack<T>::Push(T Elem) { if (StackFull()) //stack full return 0; else { Data[++Top] = Elem; return 1; } } template<class T> int Stack<T>::Pop() { if (StackFull()) //stack empty return 0; else { --Top; return 1; //successful popping } } template<class T> int Stack<T>::StackEmpty() { return Top == -1 ? 1 : 0; // empty return 1 } template<class T> int Stack<T>::StackFull() { return Top == MaxSize - 1 ? 1 : 0; //return 1 when full } template<class T> int Stack<T>::GetTop(T& TopData) { if (StackEmpty()) //stack empty return means no stack top value return 0; else { TopData = Data[Top]; return 1; } }
3. Chain stack
1. Chain stack diagram (the scum soul painter is here again)
Pause a sentence
This diagram is also helpful for understanding linked lists, andThe data structure is based on a linked list, the linked list is unfamiliar with self- portraits : click to open the link
2. Features:
1) andFehling's reagentThe same is currently (distributed) allocated and used, so the upper limit is not capped and the stack is not full
2) Please always note that when the stack is empty, you cannot pop or fetch the top of the stack :)
3. Throw the code
#include<iostream> using namespace std; struct Node { int Data; Node* Next; }; class Stack { private: Node * Top; //The top of the stack, the space is allocated one by one into the stack, regardless of the stack's fullness public: Stack(); //Initial stack ~Stack(); //destruct the stack int Push(int Elem_Node); //Push the stack int Pop(int& Be_Pop_Elem); //出栈 int Get_Top_Node(int& Get_Top_Elem); //Get the bottom of the stack int Empty(); //The stack is empty }; Stack::Stack() { Top = NULL; } Stack::~Stack() { Node* Temp_Top = Top; while (Temp_Top) { Top = Top->Next; //top keeps moving backward, and defines a node to be deleted in front of temp storage delete Temp_Top; Temp_Top = Top; } } int Stack::Push(int Elem_Node) { Node* Push_Node = new Node; //Create a new node Push_Node->Next = Top; //The link is in front of the top of the old stack Push_Node->Data = Elem_Node; Top = Push_Node; //New stack top return 1; } int Stack::Pop(int& Be_Pop_Elem) { if (Top) { Node* Temp_Top = Top; Be_Pop_Elem = Top->Data; Top = Top->Next; //top is moved back, you need to save the old stack top for deletion delete Temp_Top; return 1; } else return 0; } int Stack::Get_Top_Node(int& Get_Top_Elem) { if (Top) { Get_Top_Elem = Top->Data; return 0; } else return 0; } int Stack::Empty() { return Top == NULL ? 1 : 0; //If top is empty, it means the stack is empty~ }
Fourth, the application (very important)
1) Thought: recursion, example tower of Hanoi, portal: click to open the link
2) Bracket matching: click to open the link
Apply the above sequence stack class to do it, the code is as follows:
intmain() { Stack<char> KuoHao; char KuoHao_str[MaxSize]; int flag = 1, i = 0; char GetTop_Elem; cin >> KuoHao_str; while (flag&&KuoHao_str[i] != '\0') { switch (KuoHao_str[i]) { case '[': case '(': case '{': KuoHao.Push(KuoHao_str[i]); break; case ')': KuoHao.GetTop (GetTop_Elem); if (GetTop_Elem == '(') KuoHao.Pop(); else flag = 0; break; case '}': KuoHao.GetTop (GetTop_Elem); if (GetTop_Elem == '{') KuoHao.Pop(); else flag = 0; break; case ']': KuoHao.GetTop (GetTop_Elem); if (GetTop_Elem == '[') KuoHao.Pop(); else flag = 0; break; default: break; } i++; } if (flag&&KuoHao.StackEmpty()) cout << "Parenthesis match" << endl; else cout << "error" << endl; return 0;
ps: Just compile the above class code and implementation code together, I am afraid that someone willConfused