Data Structure BASICS (13), data structure basics 13

Source: Internet
Author: User

Data Structure BASICS (13), data structure basics 13

The stack using chain storage is a chain stack (or a chain stack for short). The advantage of chain stack is that it facilitates multiple stacks to share storage space and improve its efficiency, there is no overflow in the stack (because the stack is linked together by pointers, as long as the memory is large enough, there is no upper limit on the elements that can be stored in the stack theory );

Compared with the ordered stack, the ordered stack uses array implementation. Therefore, once the array is filled up, you must apply for memory again and "move" all elements ", the chain stack ignores this "time-consuming and labor-consuming" task, but it has to pay the cost of attaching a pointer;

The chain stack is usually implemented using a single linked list, and all operations must be performed on the header of a single-chain table. w our chain Stack has no header node, and m_top points directly to the top element of the stack;

The following figure shows the chain Stack:


Stack node structure:

template <typename Type>class ChainNode{    template <typename T>    friend ostream &operator<<(ostream &os, const LinkStack<T> &stack);    friend class LinkStack<Type>;private:    ChainNode(const Type &_data, ChainNode *_next = NULL)        :data(_data), next(_next) {}    Type data;    ChainNode *next;};

Stack design:

template <typename Type>class LinkStack{    template <typename T>    friend ostream &operator<<(ostream &os, const LinkStack<T> &stack);public:    LinkStack(): m_top(NULL) {}    ~LinkStack()    {        makeEmpty();    }    bool isEmpty() const    {        return m_top == NULL;    }    void pop() throw(std::out_of_range);    const Type &top() const throw(std::out_of_range);    void push(const Type &data);    void makeEmpty();private:    ChainNode<Type> *m_top;};

Three Major Stack operations:

template <typename Type>const Type &LinkStack<Type>::top() constthrow (std::out_of_range){    if (isEmpty())        throw std::out_of_range("stack is empty, can`t get data");    return m_top->data;}
template <typename Type>void LinkStack<Type>::pop()throw (std::out_of_range){    if (isEmpty())        throw std::out_of_range("stack is empty, can`t delete");    ChainNode<Type> *deleteNode = m_top;    m_top = m_top->next;    delete deleteNode;}
Template <typename Type> void LinkStack <Type>: push (const Type & data) {// This is the key point of the entire chain stack // This operation generates a node, // automatically move m_top up to a grid, // and use m_top as the next node of the newly generated node. // note this // If push is run for the first time, the original m_top is NULL // The new m_top points to the first element m_top = new ChainNode <Type> (data, m_top );}

Clear the entire stack:

template <typename Type>void LinkStack<Type>::makeEmpty(){    while (!isEmpty())    {        pop();    }}

Output all content in the stack:

template <typename Type>ostream &operator<<(ostream &os, const LinkStack<Type> &stack){    ChainNode<Type> *currentNode = stack.m_top;    while (currentNode != NULL)    {        cout << currentNode->data << ' ';        currentNode = currentNode->next;    }    return os;}

Test code:

int main(){    LinkStack<int> test;    for (int i = 0; i < 10; ++i)    {        test.push(rand()%100);    }    cout << test << endl;    cout << "top = " << test.top() << endl;    test.pop();    cout << "top = " << test.top() << endl;    test.push(1);    cout << "top = " << test.top() << endl;    while (!test.isEmpty())    {        test.pop();    }    cout << test << endl;    test.push(11);    test.push(22);    test.push(33);    cout << test << endl;    test.makeEmpty();    try    {        cout << "top = " << test.top() << endl;    }    catch (const std::exception &e)    {        cerr << e.what() << endl;    }    return 0;}

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.