Data structure and algorithm--chain stack

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.