List: definition, list definition
Node definition:
Typedef int Rank; // Rank # define ListNodePosi (T) listNode <T> * // list node location template <typename T> struct ListNode {// list node template class (implemented in two-way linked list form) // member T data; ListNodePosi (T) pred; ListNodePosi (T) succ; // value, precursor, successor // constructor ListNode () {}// construct ListNode (T e, listNodePosi (T) p = NULL, ListNodePosi (T) s = NULL): data (e), pred (p), succ (s) {} // default constructor // operation interface ListNodePosi (T) insertAsPred (T const & e); // Insert a new node ListNodePosi (T) immediately before the current node) insertAsSucc (T const & e); // Insert a new node immediately after the current node };
Definition of the linked list (two-way linked list is used here ):
# Include "listNode. h "// introduce the List node class template <typename T> class List {// List template class private: int _ size; ListNodePosi (T) header; ListNodePosi (T) trailer; // scale, header, and tail guard protected: void init (); // initialize int clear () when the list is created (); // clear all nodes void copyNodes (ListNodePosi (T), int); // copy n void merge (ListNodePosi (T) &, int, list <T> &, ListNodePosi (T), int); // sorted List range merge void mergeSort (ListNodePosi (T) &, int ); // merge and sort n consecutive nodes starting from p Void selectionSort (ListNodePosi (T), int); // For n consecutive nodes starting from p, select void insertionSort (ListNodePosi (T), int ); // insert an order for n consecutive nodes starting from p. public: // constructor List () {init ();} // default List (List <T> const & L); // overall copy List L List (List <T> const & L, Rank r, int n ); // copy the n items List (ListNodePosi (T) p, int n) from item r in List L ); // copy n items from position p in the list. // destructor ~ List (); // release (including the header and tail Sentel) All nodes // read-only access interface Rank size () const {return _ size ;} // scale bool empty () const {return _ size <= 0 ;}// empty T & operator [] (Rank r) const; // reload, support sequential access (low efficiency) ListNodePosi (T) first () const {return header-> succ;} // The first node location ListNodePosi (T) last () const {return trailer-> pred;} // bool valid (ListNodePosi (T) p) of the last node // determine whether location p is valid externally {return p & (trailer! = P) & (header! = P);} // equals the header and tail nodes to NULL int disordered () const; // determines whether the list is sorted ListNodePosi (T) find (T const & e) const // unordered list query {return find (e, _ size, trailer);} ListNodePosi (T) find (T const & e, int n, ListNodePosi (T) p) const; // query ListNodePosi (T) search (T const & e) const // query the ordered list {return search (e, _ size, trailer );} listNodePosi (T) search (T const & e, int n, ListNodePosi (T) p) const; // query ListNodePosi (T) selectMax (ListNodePosi (T) p, int n); // select the receiver ListNodePosi (T) selectMax () {return selectMax (header-> succ, _ size) in p and the first n-1 successor );} // overall publisher // writable access interface ListNodePosi (T) insertAsFirst (T const & e); // insert e as the first node into ListNodePosi (T) insertAsLast (T const & e); // insert e as the last node into ListNodePosi (T) insertBefore (ListNodePosi (T) p, T const & e ); // insert e as the precursor of p into ListNodePosi (T) insertAfter (ListNodePosi (T) p, T const & e ); // insert e as the successor of p into T remove (ListNodePosi (T) p); // Delete the node at the valid position p, return the void merge (List <T> & L) {merge (first (), size, L, L. first (), L. _ size);} // merge void sort (ListNodePosi (T) p, int n) in the full list; // sort void sort () {sort (first (), _ size);} // The overall sorting int deduplicate (); // unordered deduplicated int uniquify (); // ordered deduplicated void reverse (); // invert (Exercise) // traverse void traverse (void (*) (T &); // traverse and implement visit operations in sequence (function pointer, read-only or partial modification) template <typename VST> // operator void traverse (VST &); // traverses and implements visit operations in sequence (function object, which can be globally modified )}; // List # include "List_implementation.h"