Learning points:
1. Zero Initialization
D = T () If t is the basic data type, it indicates 0
If T is a custom data type, use the non-argument constructor.
2 members of the internal type:
Define a type, which is affected by access permissions. Generally, access is not allowed externally.
3. When an internal Member points to the dynamic memory, the Destructor assignment copy function must be rewritten.
4 pointer reference node * & P indicates that the pointer itself can be used to change the pointer pointing.
This usually appears in the returned values and parameters.
02list. h
# Ifndef list_h # define list_h # include <iostream> using namespace STD; typedef int t; class list {// access permission of members of internal types affects struct node {t data; node * Next; // once you write the constructor, You Cannot initialize the previous method. // d = T () zero initialization basic data type indicates 0 custom data type indicates no parameter constructor node (const T & D = T (): Data (D), next (null) {}}; node * head; // header pointer, used to save the header Node Address. The linked list with a head node is directly the Node object int Len; public: List (): Head (null), Len (0) {}//// because a pointer member points to the dynamic memory, the three major function destructor must be rewritten to assign values to the copy structure ~ List (); // copy the value assignment function list (const list & L); // value assignment function list & operator = (const list & L ); // if the return value of a function is a list type, it must be a pointer or a reference. // If the Destructor is not overwritten, the value is returned, because the head points to the same place, the nint size () const is returned if the number of spaces in the linked list is N in the Temporary Variable Structure; // void push_front (const T & D); // void push_back (const T & D ); // find the pointer node * & getptr (int pos) pointing to the specified position in the linked list; // insert void insert (const T & D, int POS) at any position ); // n nodes have n + 1 inserted POS 0 ~ N // traverse void travel () const; // Delete void erase (int pos) by position; // obtain the int find (t d) pointer to the node by numerical value ); // Delete the value void remove (t d) in the Node Deletion list by numerical value; // modify the node void set (INT POs, t d ); // determine whether the bool empty () const is empty; // return the data of the header node T front () const; // return the data of the End Node t back () const; // chain table reversal // Train of Thought 1 pointer reversal 3 temporary pointers void reverse (); // another idea of chain table inversion // re-construct a chain table and insert two temporary pointers void reverse2 (); // sort void sort () from small to large (); // merge void add (List & L); // clear void clear () ;};# endif
02list. cpp
# Include "02list. H "# include <iostream> using namespace STD ::~ List () {clear () ;}// copy the assignment function list: List (const list & L) {head = NULL; Len = 0; node * P = L. head; while (P! = NULL) {push_back (p-> data); P = p-> next ;}// value assignment function list & list: Operator = (const list & L) {clear (); node * P = L. head; while (P! = NULL) {push_back (p-> data); P = p-> next;} return * This ;} // if the return value of a function is a list type, it must be a pointer or a reference. // If the Destructor is not overwritten, the value is returned, because the head points to the same place, the nint list: size () is returned if the number of spaces in the linked list is N in the Temporary Variable Structure () const {return Len;} // void list: push_front (const T & D) {/* node * P = new node (d ); // local variables cannot be used here because local variables will destroy p-> next = head; head = P; */insert (D, 0);} // insert void list after:: push_back (const T & D) {insert (D, size (); // the chance of a variable error less than once is Len Difference from size ()} // find the pointer pointing to the specified position in the linked list // return the reference type to ensure that the pointer itself is returned // if not referenced, the value is passed. // POS ranges from 0 ~ N has n + 1 locations, 0 ~ N can be interpolated at 0 ~ N-1 is used to delete and modify. // write reference because I want to change the pointer to be returned in the future. // If the value is passed, I can only get one pointer to the same point after calling the function. In this way, I can only modify the value of the object to which it points, but cannot modify the pointer to which it returns. List: node * & list: getptr (int pos) // list: Internal node type {If (Pos = 0) return head; node * P = head; // 1 return p-> next // 2 p = p-> next; return p-> nextfor (INT I = 1; I <Pos; I ++) {P = p-> next;} return p-> next ;} // Insert at any position // 1 find the pointer pointing to this position in the linked list // 2 the pointer pointing to the new node and the pointer pointing to the same place // 3 the pointer pointing to the new node + + lenvoid list:: insert (const T & D, int POS) // n nodes have n + 1 inserted POS 0 ~ N {If (Pos <0 | POS> size () Pos = 0; node * & P = getptr (POS ); // continue to reference node * Pn = new node (d); PN-> next = P; P = pN; Len ++;} // traverse void list: Travel () const {node * P = head; while (P! = NULL) {cout <p-> data <''; P = p-> next;} cout <Endl ;} // Delete by location // insert n + 1 locations. Delete n locations. 0 ~ N-1 // 1 find the pointer pointing to this node // 2 Save the pointer as temp pointer pointing to the next node of this node // 3 release the temp space -- lenvoid list :: erase (int pos) {If (Pos <0 | POS> size ()-1) // invalid position {return;} node * & P = getptr (POS ); node * temp = P; P = p-> next; Delete temp; -- Len;} // obtain the int list: Find (t d) pointer to the node by numerical value) {node * P = head; int Pos = 0; while (P! = NULL) {If (P-> data) = d) return Pos; P = p-> next; POS ++;} return-1 ;} // delete a node by a number to delete all the values in the linked list void list: Remove (t d) {int Pos; while (Pos = find (D ))! =-1) {erase (POS) ;}// modify the node void list: Set (INT POs, t d) {If (Pos <0 | POS> size ()-1) return; getptr (POS)-> DATA = D;} bool list: Empty () const {return head = NULL;} // return the data of the first node T list: Front () const {If (empty () throw "The linked list is empty "; return head-> data;} // return the data of the End Node t list: Back () const {If (empty () throw "The linked list is empty "; node * P = head; while (p-> next! = NULL) {P = p-> next;} return p-> data;} // reverse linked list // Train of Thought 1 pointer reverse 3 temporary pointers void list: reverse () {// I am confused because the pointer reference has been used before. // Why not reference it here? // Because both the function parameter and the return value have a value transfer problem, the actual operation // is a temporary pointer to the same point. // This function does not have a transfer problem. It is a direct operation on the pointer. Node * P = head; node * p_next = NULL; node * p_pre = NULL; while (P! = NULL) {p_next = p-> next; P-> next = p_pre; p_pre = P; P = p_next;} head = p_pre ;} // another idea of chain table inversion // re-construct a chain table and insert two temporary pointers void list: reverse2 () {node * P = head; node * q; head = NULL; while (P! = NULL) {q = p-> next; P-> next = head; head = P; P = Q ;}// sort void list from small to large :: sort () {node * P = head; node * q; int temp; while (q = p-> next )! = NULL) {If (p-> DATA> q-> data) {temp = p-> data; P-> DATA = Q-> data; q-> DATA = temp; P = head;} else {P = Q ;}}// merge void list: add (List & L) {// getptr (SIZE () = L. head; this way, l operations are insecure and will affect the node * P = L. head; while (P! = NULL) {push_back (p-> data); P = p-> next ;}// clear void list: clear () {While (Head! = NULL) {node * P = head-> next; Delete head; head = P;} head = NULL; Len = 0 ;}
Main. cpp
# Include <iostream> # include "02list. H "using namespace STD; int main () {list l; cout <sizeof (l) <Endl; // The size is 8 L. push_front (1); // insert L in the header. push_front (10); L. push_front (100); L. travel (); // 100 10 1 L. insert (1000,0); L. travel (); // 1000 100 10 1l. push_back (10000); // 1000 100 10 1 10000 L. travel (); L. insert (70,-1); // The POS is incorrectly corrected to 0l. travel (); // 70 1000 100 10 1 20.l. insert (7000,200); // The POS is incorrectly corrected to 0l. travel (); // 700 0 70 1000 100 10 1 10000 L. erase (100); // return l directly if the location is invalid. travel (); // 7000 70 1000 100 10 1 10000 L. erase (5); // Delete Location 5, that is, Location 6, L. travel (); // 7000 70 1000 100 10 10000 // insert a duplicate value L. insert (999,2); L. insert (999,5); L. insert (999,6); L. travel (); // 7000 70 999 1000 100 999 999 10 10000 L. remove (999); // delete L by value. travel (); // 7000 70 1000 100 10 10000 // modify L. set (L. find (10), 9); // change 10 to 9l. travel (); // 7000 70 1000 100 9 then L. s Et (0th); // change to 8l. travel (); // 8 70 1000 100 9 10000 cout <boolalpha <L. empty () <Endl; // falsecout <L. front () <''<L. back () <Endl; // 8 10000 // cout <L. getptr (L. size ()-1)-> data <Endl; cout <"reverse:"; L. reverse (); L. travel (); L. reverse2 (); cout <"reverse2:"; L. travel (); L. sort (); cout <"sort:"; L. travel (); L. reverse (); cout <"reverse"; L. travel (); // copy the constructor list L2 (l); cout <"L2 Trav El: "; l2.travel (); List l3; l3.push _ back (1); l3.push _ back (2); l3.push _ back (3); cout <" L3 travel: "<Endl; l3.travel (); L2 = l3; cout <" L2 travel: "<Endl; l2.travel (); l2.add (L3 ); cout <"L2 travel:" <Endl; l2.travel (); cout <"size" <l2.size () <Endl; // clear l3.clear (); while (! L. Empty () {L. Erase (0) ;}cout <"Size:" <L. Size () <Endl; return 0 ;}