List source code 1 (refer to STL source code-hou Jie): List node, iterator, Data Structure

Source: Internet
Author: User

List Overview

List is much more complex than vector. List is a double-stranded table. For inserting and deleting elements, list is relatively simple.

List Node

Template <class T> struct _ list_node {typedef void * void_pointer; void_pointer Prev; // The type is void *. You can also set it to _ list_node <t> * void_pointer next; t data ;};

List iterator

The list iterator must be able to point to the list node and perform operations such as increment, decrease, value, and member access correctly. At the same time, the list iterator is a two-way linked list, the iterator must be capable of moving forward and backward. Therefore, list provides bidirectional iterators.

List has an important attribute: neither the insert operation nor the splice operation will invalidate the original list iterator, or even the list element deletion (erase) only the iterator that points to the deleted element is invalid. Other iterators are not affected.

Template <class T, class ref, class PTR> struct _ list_iterator {typedef _ list_iterator <T, T &, T *> iterator; typedef _ list_iterator <t, ref, PTR> self; typedef pointer; typedef t value_type; typedef PTR pointer; typedef ref reference; typedef _ list_node <t> * link_type; typedef size_t size_type; typedef ptrdiff_t difference_type; link_type node; // a common pointer is required inside the iterator to point to the list section. Point // constructor _ list_iterator (link_type X): node (x) {}_ _ list_iterator (const iterator & X): node (X. node) {} bool operator = (const self & X) const {return node = x. node;} bool Operator! = (Const self & X) const {return node! = X. node;} // The following iterator values take the node data value reference operator-> () const {return & (operator *());} // Add self & operator ++ () {node = (link_type) (* node) to the iterator ). next); return * This;} self operator ++ (INT) {self TMP = * This; ++ * This; return TMP ;} // subtract 1 Self & operator -- () {node = (link_type) (* node) from the iterator ). PREV); return * This;} self operator -- (INT) {self TMP = * This; -- * This; return TMP ;}};

List Data Structure

List is not only a two-way linked list, but also a ring two-way linked list, so it only needs to be able to fully express the entire linked list:

Template <class T, class alloc = alloc> // by default, use alloc as the adapter class list {protected: typedef _ list_node <t> list_node; public: typedef list_node * link_type; protected: link_type node; // as long as a pointer, it indicates that the entire ring is mounted with a two-way linked list ......};

If the node (as shown above) points to a space node deliberately placed at the end, the node can meet the STL Requirements for the "pre-closed and post-open" interval and become the last iterator, for example, in this way, the following functions can be well completed:

Iterator begin () {return (link_type) (* node ). next);} iterator end () {return node;} bool empty () const {return node-> next = node;} size_type size () const {size_type result = 0; distance (begin (), end (), result); return result;} // reference Front () {return * begin ();} // reference back () {return * (-- end ());}

 

 

List source code 1 (refer to STL source code-hou Jie): List node, iterator, Data Structure

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.