Two-way linked list (insert, delete, append, forward and reverse traversal, search ...)

Source: Internet
Author: User
# Include <iostream> # include <stdexcept> using namespace STD; class list {public: List (void): m_head (null), m_tail (null), m_size (0) {}~ List (void) {for (node * node = m_head; m_head = node) {node = m_head-> m_next; Delete m_head ;}} // insert element void insert (INT data, size_t index) at index {// If the subscript exceeds the number of elements, append the element at the end of the linked list if (index> = m_size) {append (data); return;} node * temp = new node (data); // If the subscript is 0, insert it to the header node if (0 = index) {temp-> m_next = m_head; m_head-> m_prev = temp; m_head = temp; ++ m_size; return;} size_t COUNT = 1; // In other cases, for (node * node = m_head-> m_next; nod E; node = node-> m_next) {If (Index = count) {temp-> m_next = node; temp-> m_prev = node-> m_prev; node-> m_prev-> m_next = temp; node-> m_prev = temp; ++ m_size; return ;}++ count ;}} // append the element void append (INT data) {node * node = new node (data); If (! M_head) m_head = node; else {node-> m_prev = m_tail; m_tail-> m_next = node;} m_tail = node; ++ m_size ;} // Delete the element void erase (size_t index) {If (! M_head) Throw underflow (); // first, find the node * & node = find (index, m_head); node-> m_prev-> m_next = node-> m_next; node-> m_next-> m_prev = node-> m_prev; delete node; node = NULL;} // search for the Int & find (size_t index) Element) {node * & node = find (index, m_head); Return node-> m_data;} // forward traversal void forward () const {If (! M_head) Throw underflow (); For (node * node = m_head; node = node-> m_next) {cout <node-> m_data <'';} cout <Endl;} // reverse traversal void backward () const {If (! M_tail) Throw underflow (); For (node * node = m_tail; node = node-> m_prev) {cout <node-> m_data <'';} cout <Endl;} // Number of linked list elements size_t size () {return m_size;} // overload the subscript operator as the left value Int & operator [] (size_t index) {If (index> = m_size) Throw overflow (); node * & node = find (index, m_head); Return node-> m_data;} // reload the subscript operator, as the right value const Int & operator [] (size_t index) const {// After the constant attribute is removed, call the above subscript to reload return const_cast <list &> (* This) [ind Ex];} PRIVATE: // linked list each node class node {public: node (int data = 0, node * Prev = NULL, node * Next = NULL ): m_data (data), m_prev (prev), m_next (next) {}int m_data; node * m_prev; node * m_next ;}; // underflow exception class underflow: public exception {public: const char * What () Throw () {return "overflow exception! ";}}; // Subscript out of bounds! Class overflow: Public exception {public: const char * What () Throw () {return "subscript out of bounds! ";}}; Node * & find (size_t index, node * head) {If (index> = m_size) Throw overflow (); size_t COUNT = 0; for (node * & node = head; node = node-> m_next) {If (Index = count) return node; ++ count ;}} node * m_head; node * m_tail; size_t m_size;}; int main () {list; List. append (10); list. append (20); list. append (30); list. append (40); list. append (50); list. append (60); list. append (70); cout <"number of elements:" <list. size () <Endl; List. forward (); list. backward (); list. insert (100, 0); list. insert (150, 11); cout <"number of elements:" <list. size () <Endl; List. forward (); list. backward (); cout <"5th elements:" <list. find (5) <Endl; List. erase (5); cout <"after deleting 5th elements:"; List. forward (); list [5] = 876; List. forward (); cout <"5th elements:" <list [5] <Endl; return 0 ;}

Two-way linked list (insert, delete, append, forward and reverse traversal, search ...)

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.