Summarize the link stack today.
What is a link stack?
A chain stack is a chain-like storage structure that is similar to a single-linked list. But the head pointer becomes the stack-top pointer, which always points to the top element of the stack. The pointer field of the bottom node of the stack points to null, and when Top==null, the stack is empty. When implemented, a single linked list is compared, and then the diagram is easily written.
Icon:
Realize:
<span style= "Font-family:courier new;font-size:14px;" > #include <iostream>using namespace std;template <class t>struct Node {T data; struct node<t> *next;}; Template <class T>class Linkstack{public:linkstack () {top = NULL; Empty stack} ~linkstack (); destructor releases the node space void Push (T x); Into the stack T Pop (); Out of Stack T GetTop (); Get stack top element void Printstack (); Print stack within the element bool IsEmpty (); Determine if the empty stack private:struct node<t> *top;}; Template <class t>void linkstack<t>::P ush (T x) {node<t> *p = new node<t>; P->data = x; P->next = top; top = p;} Template <class t>void linkstack<t>::P rintstack () {node<t> * p = top; Save stack top pointer while (top) {cout<<top->data<< ""; top = top->next; } top = P; Cout<<endl the top pointer of the stack to the top element of the stack;} Template <class t>t Linkstack<t>::P op () {if (IsEmpty ()) cout<< "stack is empty"; Node<t>*p = top; T x = top->data; top = top->next; Delete p; return x;} Template <class T>bool Linkstack<t>::isempty () {if (top==null) return true; else return false;} Template <class t>t linkstack<t>::gettop () {if (IsEmpty ()) cout<< "Stack is empty" <<endl; return top->data;} Template <class T>linkstack<t>::~linkstack () {while (top) {node<t> *p = top; top = top->next; Delete p; }}int Main () {linkstack<int> stack; for (int i=0;i<5;i++) {stack. Push (i); } stack. Printstack (); cout<< "Out of the stack:" <<endl; Cout<<stack. Pop () <<endl; Stack. Printstack (); cout<< "Get stack top element" <<endl; Cout<<stack. GetTop () <<endl; return 0;} </span>
Data structure and algorithm--chain stack