Bidirectional linear list Container # include <cstring> #include <iostream> #include <stdexcept>using namespace std;// Linked List class template Template<typename T>class list{public://constructs, destructors, supports copy construction and copy assignment of deep copies List (void): M_head (null), M_tail (null) {}~ List (void) {clear ();} List (list const&): M_head (null), M_tail (null) {for (node* node = that.m_head; Node! = Null;node = Node->m_next ) push_back (node->m_data);} list& operator = (List const& rhs) {if (&rhs! = this) {List List (RHS); swap (M_head, list.m_head); Swap (M_tail, Li St.m_tail);} return *this;} Gets the first element t& front (void) {if (empty ()) throw Underflow_error ("list underflow!"). ");//underflow exception return m_head->m_data;} T const& Front (void) const {return const_cast<list*> (this)->front ();} Press the header into void Push_front (T const& data) {m_head = new Node (data, NULL, m_head); if (m_head->m_next! = null) M_head-> ; M_next->m_prev = M_head;elsem_tail = M_head;} eject void Pop_front (void) {if (empty ()) throw Underflow_error ("list underflow!") from the header. "); node* next = M_head->m_nExt;delete M_head;m_head = next;if (m_head) M_head->m_prev = Null;elsem_tail = NULL;} Gets the tail element t& back (void) {if (empty ()) throw Underflow_error ("list underflow!"). "); return m_tail->m_data;} T const& back (void), const {return const_cast<list*> (this)->back ();} Press the tail into void push_back (T const& data) {m_tail = new Node (data, m_tail); if (M_tail->m_prev! = NULL) M_tail->m_prev ->m_next = M_tail;elsem_head = M_tail;} POPs from the tail void Pop_back (void) {if (empty ()) throw Underflow_error ("list underflow! "); node* prev = M_tail->m_prev;delete M_tail;m_tail = prev;if (m_tail! = null) M_tail->m_next = Null;elsem_head = null;} Delete all matching elements void remove (T const& data) {for (node* node = m_head, *next; node! = NULL; node = next) {next = Node->m_n Ext;if (Equal (data, node->m_data)) {if (Node->m_prev! = NULL) Node->m_prev->m_next = node->m_next;elsem_ Head = node->m_next;if (node->m_next) Node->m_next->m_prev = Node->m_prev;elsem_tail = node->m_prev; Delete node;}}} empty void Clear (void) {while (!empty ()) Pop_back ();} empty bool Empty (void) const {return NULL = = M_head && NULL = = M_tail;} Sizes size_t size (void) const {size_t nodes = 0;for (node* node = m_head; Node! = Null;node = Node->m_next) ++nodes;retur n nodes;} Subscript operator ———— shit! t& operator[] (size_t i) {for (node* node = m_head; Node! = Null;node = node->m_next) if (0 = = i--) return node->m_ Data;throw Out_of_range ("Subscript out of Bounds!") ");} T const& operator[] (size_t i) Const{return const_cast<list&> (*this) [i];} Insert output stream friend ostream& operator << (ostream& os, list const& list) {for (node* node = list.m_head; node ! = Null;node = node->m_next) OS << *node;return os;} PRIVATE://Node Class templates class Node{public:node (T const& data, node* prev = null, node* next = null): M_data (data), M_prev (prev ), M_next (next) {}friend ostream& operator << (ostream& OS, node const& node) {return OS << ' [' < ;< node.m_data << '] ';} T m_data;//Data node* m_prev;//Front pointer node* m_next;//after pointer};//determines whether the element is equal bool equal (T const & A, T const& b) Const{return a = = b;} node* m_head;//head pointer node* m_tail;//tail pointer public://forward iterator class Iterator{public:iterator (node* head = null, node* tail = null, node* node = NULL): M_head (head), M_tail (tail), M_node (node) {}bool operator = = (Iterator const& rhs) Const{return m_ node = = Rhs.m_node;} BOOL operator! = (Iterator const& RHS) Const{return! (*this = = RHS);} iterator& operator++ (void) {if (m_node) M_node = M_node->m_next;elsem_node = M_head;return *this;} Iterator const operator++ (int) {Iterator old = *this;++*this;return old;} iterator& operator--(void) {if (m_node) M_node = M_node->m_prev;elsem_node = M_tail;return *this;} Iterator const operator--(int) {Iterator old = *this;--*this;return old;} t& operator* (void) Const{return m_node->m_data;} t* operator-> (void) Const{return &**this;} private:node* M_head; node* M_tail; node* M_node;friend class list;};/ /Gets the starting forward iterator ———— point to the first element iterator beGin (void) {return Iterator (M_head, M_tail, m_head);} Gets the terminating forward iterator ———— points to the next position of the last element Iterator end (void) {return Iterator (M_head, m_tail);} Inserted before the forward iterator, returns an iterator to the newly inserted element Iterator insert (Iterator loc, T const& data) {if (loc = = End ()) {push_back (data); return Iterator (M_head, M_tail, m_tail);} else{node* node = new node (data, Loc.m_node->m_prev, Loc.m_node); if (node->m_prev) Node->m_prev->m_next = Node;elsem_head = Node;node->m_next->m_prev = Node;return Iterator (m_head, M_tail, node);}} Removes the element that the iterator points to and returns the iterator after the element Iterator erase (Iterator Loc) {if (loc = = End ()) throw invalid_argument ("Invalid argument! ") if (loc.m_node->m_prev) Loc.m_node->m_prev->m_next = Loc.m_node->m_next;elsem_head = Loc.m_node->m _next;if (loc.m_node->m_next) Loc.m_node->m_next->m_prev = Loc.m_node->m_prev;elsem_tail = loc.m_node- >m_prev; node* next = Loc.m_node->m_next;delete Loc.m_node;return Iterator (M_head, M_tail, next);} Constant forward iterator//reverse iterator//constant reverse iterator};//a member-specific version of the Char const* type template<>BOOL List<char const*>::equal (char const* const& A, char const* const& b) Const{return (0 = = strcmp (A, b));}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Implementation of "C + +" bidirectional linear linked list container