Decree of single-chain table structure (C ++ version, link.h)

xiaoxiao2021-03-06  82

Link.h Applicable Textbook: "Data Structure" C Edition Liu Daothy, etc. Higher Education Press #ifndef __linkedList_HPP __ # Define __LinkedList_HPP__

#if _MSC_VER> 1000 # prgma overce # endif //_MSC_VER> 1000

EXTERN "C" {int exit (int);};

// Single-link table Node Definition Template // Node Data Data provides class node {public: // public member T data; // 公域, allowing external direct access Private: // Private member Node * Next; // Reflecting the Needle Domain (Chain Domain), pointer: // Public Members // Constructor (Initial Data and Next) Node (Const T & ITEM, Node * PNEXT = NULL): DATA (item), Next (pnext) {} // Insert the pointer P inserted by the pointer P, {if (! P) {if (! P) {ix Return; // If p is empty, return p-> next = next; // will point the next NEXT to be inserted to point to the NEXT domain next = p of the current node; // update the current node's NEXT update to Waiting to insert Node P} // Delete the backpoint of the current node and return the address of the deleted node Node * deleteafter () {if (! Next) Return null; // If there is no success (Next is NULL , Return node * pnext = next; // next is not empty, record its address (left after returning) Next = next-> next; // Subsequent NETS (Next) To change the NEXT domain RETURN PNEXT of the current node; / / Returns the indicated node address} // returns a pointer Node * next Node * NextNode () Const {Return NEXT; } T getdata () const {return data;} void setData (const t & item) {data = item;}}; // single-strand table definition template class linkedList {private: Node * Front, * REAR; // Header, tail Node * currptr; // pointing Current node pointer int size; // Summer (Number of nodes) private: // Generate new node Node * getnode (const t & item, node * pnext = null) {node < T> * newNode; // Newly assigned a node storage space and initialize data member newNode = new node (item, pnext); if (! Newnode) {cerr << "storage space allocation failed! The program will terminate.

<< Endl; exit (1);} Return NewNode;} // Release Node P Void * FreeEnode (Node * P) {if (p) delete P;} private: // When the lin list is empty Treatment INT INSERTNEWNODEWHENLISTISEMPTY (SIZE> 0) {if (size> 0) Return 0; // is not empty, return false (0) currptr = getNode (item); Front = currptr; rest = currptr; Size ; RETURN 1; // Insert a node in the empty list, return true (1)} public: // constructor LinkedList () {front = null; rear = null; currptr = null; size = 0;} // Copy constructor LinkedList (const linkdlist & l) {size = 0; Node * P; p = l.front; if (! P) {// l is empty chain table front = null; rest = NULL; currptr = null; return;} front = getNode (p-> getData ()); currptr = front; size ; rest = front; p = p-> nextnode (); while (p) {= getnode (p-> getData ()); currptr-> insertafter (rear); SIZE ; P = P-> NextNode (); currptr = REAR;} currptr = front;} // Destructure function ~ LinkedList () {clearlist ();} public: // overloaded LINKEDLIST & Operator = (Const LinkedList & l) {clearList (); // Clear the current list content Node * P; p = l.front; // P pointing to the head IF of the table L on the table L (! P ) {// L is empty latter table front = null; rest = null; currptr = null; prevptr = null; return * this;} front = getNode (p-> getData ());

Currptr = Front; Rear = Front; size ; p = p-> nextnode (); while (p) {rear = getNode (p-> getData ()); currptr-> insertafter (REAR); Size ; p = p-> nextNode (); currptr =R;} currptr = front; return * this;} public: // Return the top length;} // Decision Lin list is Empty int listempty () const {return size == 0;} // Reset the current pointer currptr void reset (INT POS = 0) {// If the POS is out of the range, return if (POS <0 || POS> = Size RETURN; Register INT i = 0; currptr = front; // From the beginning // positioning the Currptr to the node (i nextNode (); i ;}} // Position the current pointer to the node of Item for IT Locate (const t & item, int bbeginfromcurrentpos = 1) {if (size == 0) Return -1; // empty table Return -1 Node * P; if (! Currptr) currptr = front; if (BbegInfromCurrentpos ! = 1) P = Front; // From the beginning of the head node, ELSE P = currptr; // Start whene (p) from the current location (p-> getData () == item) Break; // t The corresponding data type should be supported == operator P = P-> NextNode ();} if (! P) return -1; currptr = p; // Located to the found NOSPSITION (); // Return The node sequence} // Moves CurrPtr to the next node vid next () {if (size == 0) Return; // If a vacancy table, the IF (Currptr == REAR) RETURN; / If the end of the end, it is no longer moving if (! Currptr) {currptr = front; return;} // is moved to the header currptr =

Currptr-> NextNode (); // Move to the next junction} // Judgment whether or not the end end endoflist () const {return currptr ==R;} // Return Currptr Node Point INT thisPosition () const {= (SIZE == 0) Return -1; // Return -1 Node * P = Front; Register INT i = 0; While (P! = Currptr)) {P = P-> NextNode (); i ;} if (p) return i; // Returns its location else return -1; // Nothing, return -1} public: // Head Insert Void INSERTFRONT (Const T & Item) {Front = getNode (item, front); currptr = front; if (size == 0) REAR = Front; Size ;} // Terminal Insert Void InsertRear (Const T & ITEM) {Currptr = getnode (item); if (size == 0) Front = currptr; else rear-> INSERTAFTER (currptr); rest = currptr; size ;} // Insert Void Insertat before the current node (Const T & ITEM ) {IF (INSERTNEWNODEWHENLISEMPTY (ITEM) RETURN; / / LED Plug in Insert IF (Currptr == Front) {INSERTFRON T (item); return;} prevptr = front; while (prevptr-> nextnode ()! = currptr)) prevptr = prevptr-> NextNode (); if (! prevptr) Return; currptr = getNode (item) Prevptr-> INSERTAFTER (CURRPTR);} // Insert VOID INSERTAFTER (ITNEWNDEENLISTISEMPTY (ITETNEWNODEENLISTISEMPTY (ITETNEWNDEENLISTISEMPTY (ITEM) {///////////////////////////// If Currptr is empty, insert the head currptr = getNode (item, front); Front = Currptr; Size ; Return;

} // Insert a new node currptr-> insertafter after the current node; if (currptr == rear) // is inserted, modify the REAR pointer REAR = currptr-> NextNode () ; Currptr = currptr-> NextNode (); Size ;} // Insert Node Void InserttPos (Const T & Item, INT POS = -1) {IF (POS <= 0) { INSERTFRONT (ITEM);}}}}};}; return;} if (size == 0) {INSERTNEWNODEWHENLISTISEMPTY (Item); return;} regiSter int i = 1; currptr = front; / / Positioning to the front displacement point of the node of POS POS WHILE (Currptr && (i NextNode (); i ;} if (! Currptr) return; currptr > INSERTAFTER (GetNode (item); currptr = currptr-> nextnode (); size ;} // Remove header node void deletefront () {if (! Front) return; // Link table Empty Currptr = Front -> NextNode (); delete front; front = currptr; size -; if (size == 0) Rear = currptr; // Link table Empty, NULL } // Remove the current node void deletet () {if (! Currptr) Return; // If there is no current node if (currptr == front) {// is a head currptr = front-> nextnode (); delete Front; Front = currptr; size -; if (size == 0) REAR = currptr; // Link table Empty, null return;} node * prevptr = front; while (prevptr) {// Find currptr Prevptr-> NextNode () == currptr) Break; prevptr = prevptr-> NextNode ();} if (! Prevptr) return;

If (currptr == rear) // If the current node is the end of the tail, update the tail tail REAR = prevptr; prevptr-> deleteafter (); // From the chain table out of the node currptr delete currptr; // Release Node IF (Prevptr-> NextNode ()) // Update the value of currptr = prevptr-> NextNode (); else currptr = prevptr;} // Delete Non-point Void deletetpos (INT POS = -1) {IF (POS <1) {deletefront (); return;} register int i = 1; Node * P = front; while (p && (i nextnode (); i ;} If (! P) return; currptr = p-> deleteafter (); delete currptr; if (p-> nextnode ()) // Update Currptr's value currptr = P-> NextNode (); else currptr = P } // Returns the current node Data Domain (DATA) T & DATA () {if (currptr) Return Currptr-> getData (); // else // Return T () // Empty the entire Link table VoidList () {While (front) {currptr = front-> NextNode (); delete front; front = Currptr;} // front = null; rear = null; currptr = null; size = 0;} // output List node content, each row m.

Void PrintList (Const Char * SDELIMITER = ", // Node Diendal (Strings) INT m = -1, // After the M element is output, ignore ostream & stream = cout, / / Default Output to Screen Cout INT BendwithNewline = 1 // Whether I turn this after the output is fixed) Const {Node * P = Front; Register INT CNT = 0; While (P) {// Node Data Domain T Support output operator << stream << p-> getData (); if (p! = Rear) stream << SDELIMITER; P = P-> NextNode (); if (M <1) Continue; if ( CNT == m) {stream << endl; cnt = 0;}} f (bendwithnewline && (m <1 || cnt> 0)) stream << Endl;}}; # endif //! __ linkedList_HPP__

转载请注明原文地址:https://www.9cbs.com/read-107233.html

New Post(0)