The data structure (1) After passing to the Internet, it has been supported by many people and the support of classmates, and thank you here. This kind of concern and support have brought me more power, which makes me feel better, and then take the knowledge and experience you have for exploring. Other people helping to a certain extent, are improving themselves.
In this article I continue to introduce the linear table, it is another linear storage structure ------ linked list. Links, although and arrays are linear storage structures, there is a big difference. The fundamental difference is that the linked list is not stored with a continuous address space, so it is not possible to obtain data in the form of a subscript as the array. It uses a pointer called "Next" to point to the next storage element, of course, can also point to a storage element, which is referred to as "prev". If you can't store the elements in the form of a subscript being considered a linked list, then the linked list can be easily changed to change its storage capabilities. It is because the linked list is not to store an element with a continuous address space but to mark the position of the upper element and the next element with a pointer, so it can change the address of the element by changing the "NEXT" pointer and "prev" pointer. The structure of the linked list. For example: A-> B-> C, three elements are stored on this chain. Now I want to delete B, and change the length of the linked list. So, A-> C, DELETE B, realized our goal.
A single-strand table, a two-way linked list, and a loop list are included in the family of the linked list. According to different needs and different occasions, we can choose to use different forms of linked lists to meet our needs.
The list is a brief description of the list, maybe you are not very clear, then take a look at the code below you will be open. Here is a two-way chain table as an example.
First, we want to define a node class (Listerlement). This class contains three member variables, one is used to store data (Datum), called data field; one is used to point to an address (PPREV), called a PREV field; one is used to point to the next node Address (PNEXT), called a NEXT domain. Other member functions only do some simple operation processing.
Template
Tbertlement {private: t dam; listelement * pprev; listElement * pnext; public: / **************************** *********************************************************** * * Infut parameter: * output parameter: * Return Values: ****************************************** ***************************************************** / LISTELEMENT () { DATUM = 0; pprev = 0; pnext = 0;} / ******************************************* *************************************************** * function explain: * * Input parameter: * Output parameter: * Return Values: ***************************************************** ********************************************* / LISTELEMENT (t const & _datum, listlement * _prev, listelement * _next) {DATUM = _DATUM; pprev = _prev; pnext = _next;} / ******************************************* ***************************************** * Function Explain: * * Input Parameter : * Output Parameter: * Return VALUES: ************************************************* ************************************************** / LISTELEMENT (Listelement Const & PCLASS) {IF Pclass! = this) && (pclass! = null)) {this-> datum = pclass.datum; this-> pprev = pclass.pprev; this-> pnext = pclass.pnext;}} / ****** *********************************************************** ********************* * function explain: * * input parameter: * output parameter: * Return Values: ************ *********************************************************** *************** / T Const & GetDatum () const {return Datum
} / ******************************************************* ****************************** * function explain: * * Input parameter: * Output parameter: * Return Values: **** *********************************************************** *********************** / VOID SETDATUN (T Value) {DATUM = Value;} / ************* *********************************************************** ************* * Function Explain: * * Input Parameter: * Output Parameter: * Return Values: ******************* *********************************************************** ******** / Listelement * getnext () const {return pnext;} / *************************************** ******************************************************** * Function EXPLAIN: * * Input parameter: * output parameter: * Return Values: ************************************************************** **************************************************** / VOID SETNEXT (Listelement * Const PNEW ) {PNEXT = PNEW;} / ************************************************************ ********************************* * Fu NCTION EXPLAIN: * * OUTPUT parameter: * Return Values: ******************************************* **************************************************** / LISTELEMENT * GETPREV () Const {Return PPREV;} / ************************************************************ ***************************************** * function explain: * * Input parameter: * Output parameter: * Return Values: *********************************************************** ************************************* / VOID SETPREV (listElement * const pnew) {pprev = PNEW;
} / ******************************************************* ****************************** * function explain: * * Input parameter: * Output parameter: * Return Values: **** *********************************************************** *********************** / LISTELEMENT & OPERATOR = (& listelement! = This) {this-> Datum = Listelement.datum; This-> pprev = listelement.pprev; this-> pnext = listelement.pnext; return * this;} Return * this;} // Friend LinkedList
LinkedList is the chain class we have to use. This class will put each node and eventually form a complete chain. We let it become a friend of the Listerlement class, in order to get better access to the data domain, the Prev domain, and the NEXT domain.
Template
:: getfirst () const {if (0 == PHEAD) {Try {throw Domain_ERROR ("List is Empty!");} Catch (Exception & E) {CERR << "caught:" << E.What () <
PTEMP; PTAIL = PTEMP; PTEMP = NULL; setCountPlus ();} / ********************************** *********************************************************** *********** Function Explain: * * Input Parameter: * Output Parameter: * Return Values: ********************** *********************************************************** **** / template
= PTR) {throw std :: invalid_argument ("item not found");} if (ptr == PHEAD) {PHEAD-> getNext () -> setprev (null); PHEAD = Ptr-> getNext (); else {Ptr-> getNext () -> setPrev (prevptr-> setnext (ptr-> getnext ());} if (ptr == ptail) {ptail = prevptr; prevptr-> setnext (null);} setcountsub (); Delete PTR;} / *********************************************************** ******************************************** FUNCTION EXPLAIN: * * Input Parameter: * Output Parameter: * Return VALUES: ****************************************************************** ************************************************ / TEMPLATE
} Listelement
(NULL == Templement-> getNext ())) {tempelement-> getPrev () -> setnext (0); ptail = tempelement-> getPrev ();} else {tempelement-> getPrev () -> setnext (TempleElement- > GetNext ()); tempelement-> getNext () -> setprev (Templelement-> getPrev ());} setcountsub (); delete templement;} / ************************ *********************************************************** *********** Function Explain: * Input Parameter: * Output Parameter: * Return Values: ********************** *********************************************************** **** / template