Below is a "linked list" class written, everyone gives you opinions. The first is the declaration of the "Link" class / ************************************************* ***** * * File Name: CLINKLIST.H * * Function: Declaration Link Sheet ************************************* ****************** /
#ifndef clinklist_h_ # define clinklist_h_
Class clinklist // Link table {public: // General constructor, copy constructor, assignment operator, destructor clinklist (); ClinkList (const clinklist & rhs); clinklist & operator = (const clinklist & rhs); ~ clinklist () ;
// Insert BOOL INSERT (int position, const char * element; // Add Node Bool INSERT (const char * beforeelelec, const char * element) on the specified location; / / Add a node in front of the specified node
/ / Delete Bool Delete (INT Position); // Delete Node Bool Delete (Const Char * Element) on the specified location; // Delete the specified node
/ / Modify Bool Update (INT Position, Const Char * Element); / / Modify Node Bool Update (const char * OLDelem, const char * newlem) on the specified location; / / Modify the specified node is the new node value
/ / Find BOOL Search (const char * element, int & position); // Returns the location BOOL Search (int position, char ** element) on the list; / / Returns the node on the specified location
Bool Sort (Void); / / Sort the chain list (bubble sort)
Bool Display (Void); // Sequentially Displays element values in all nodes in the list
Inline void count // Returns the number of all nodes in the list {nodecount = m_count;};
PRIVATE: STRUCT NODE // Node Structure {struct node * last; // Finger to the forward one node pointer char * Element; // node element struct node * next; // Pointer to the latter node};
Struct node * m_head; // Table pointer for the list
INT m_count; // Number of nodes
}
#ENDIF
/// The following is the implementation of the "Link" class / ****************************************************** ******** * * File Name: CLINKLIST.CPP * * Function: Implement the various operations of the list class ********************************* ************************ /
#include
Using namespace std;
// General constructor CLINKLIST :: CLINKLIST (): m_count (0), m_head (null) {m_head = new node; // The memory space IF (m_head! = Null) // application success {// Initialization table head pointer M_Head-> last = null; m_head-> element = null; m_head-> next = null;} else // Request failed {m_head = null;}}
// Copy Construction Function CLINKList :: Clinklist (Const CLINKLIST & RHS): m_count (0), m_head (null) {INT LEN = 0;
// Temporary node pointer struct node * temp = null; struct node * back = null;
m_head = new node; // Application Header Pointer Memory Space IF (M_HEAD! = NULL) // Application Success {// Initialization Table Head Pointer M_HEAD-> Last = NULL; M_HEAD-> Element = NULL; M_HEAD-> NEXT = NULL;
Temp = rhs.m_head-> next; // Temp points to the first node IF (Temp! = null) // RHS referenced by RHS in the first node IF (Temp! = null) // RHS is not empty {Back = New node; / / For the first node of the list in this object, the memory space IF (back! = null) // application success {m_head-> next = back; // Header pointer points to the first node
Back-> last = m_head; // The first node points to the header pointer
// Copy Node Element Value LEN = Strlen (Temp-> Element); Back-> Element = New Char [LEN 1]; STRCPY (Back-> Element, Temp-> ELEMENT);
Back-> next = null; // The subsequent node is temporarily empty
m_count; // node number plus 1
// The next node TEMP = TEMP-> next; // Temp points the second node of the LED in the CLINKList object referenced by RHS (Temp! = NULL) // node exists {back-> next = new node; IF (back-> next! = null) // Memory application success {back-> next-> last = back; // Finger forward a node
BACK = back-> next; // point to the next node
LEN = Strlen (Temp-> Element); Back-> Element = new char [g 1]; strcpy (back-> element, temp-> element); back-> next = null; // The subsequent node is temporarily empty
m_count; // node number plus 1
Temp = TEMP-> Next; // Temp points the next node of the linked list in the CLINKList object referenced by RHS
} ELSE // Memory application failed {Break;}
} // end of while
} // end of if
} // end of if
} Else // Request failed {m_head = null;}
}
// Assignment Operator CLINKLIST & CLINKLIST :: Operator = (const clinklist & r Hs) {int LEN = 0;
// Temporary node pointer struct node * temp = null; struct node * back = null;
IF (this! = & r HS) // checks if it is self-assigned {
/ / Delete all the nodes of the original list pointed to by M_Head
Temp = m_head-> next; // Temp pointing to the first node M_Head-> next = null in the list in this object;
While (Temp! = null) {back = TEMP-> Next; // Save the pointer value of the next node delete [] TEMP-> Element; // Release the memory space occupied by the node element value DELETE TEMP; // Release Node Memory space occupied
Temp = back; // Point next to a node}
m_count = 0; // Number of nodes be 0
Temp = null; back = NULL;
// Copy all nodes of the linked list in the CLINKList object referenced by RHS to the current CLINKLIST object
Temp = rhs.m_head-> next; // Temp points the first node of the list of the LEDs in the CLINKList object referenced by RHS
The linked list in the clinklist object referenced by if (Temp! = NULL) // RHS is not empty {back = new node; / / is the first node for the list of this object (BACK! = NULL) // Application Success {m_head-> next = back; // Header Pointer pointing to the first node
Back-> last = m_head; // The first node points to the header pointer
// Copy Node Element Value LEN = Strlen (Temp-> Element); Back-> Element = New Char [LEN 1]; STRCPY (Back-> Element, Temp-> ELEMENT);
Back-> next = null; // There is no subsequent node
m_count; // Number Add 1 // Next Node Temp = Temp-> Next; // Temp Points the second node of the chain list in the CLINKList object referenced by RHS (Temp! = null) {Back -> Next = new node; if (back-> next! = null) // memory application success {back-> next-> last = back; // Refers to a node
BACK = back-> next; // point to the next node
LEN = strlen (TEMP-> Element); Back-> Element = new char [len 1]; strcpy (back-> element, temp-> element);
Back-> next = NULL;
m_count; // node number plus 1
Temp = Temp-> Next; // Refers to the next node
} ELSE // Memory application failed {Break;}
} // end of while
} // end of if
} // end of if
} // end of if
Return * this; // Return to the reference to this object (generally used for chain assignment)}
// Destructor CLINKLIST :: ~ clinklist () {// Temporary Node Pointer Struct Node * Temp = Null; Struct Node * Back = NULL;
IF (m_head! = null) // Header pointer does not empty {// Remove all nodes in the list pointed to by M_HEAD
Temp = m_head-> next; // Point to the first node // Release the memory space occupied by the table header DELETE M_HEEAD; M_HEAD = NULL;
While (Temp! = NULL) // Node exists {back = TEMP-> Next; // Save the pointer value of the next node delete [] TEMP-> Element; // Release the memory space occupied by the node element value DELETE TEMP; // Release the memory space occupied by the node
--M_count; // Number of nodes minus 1
Temp = back; // Refers to the next node
}
} // end of if
}
/ / Add Node Bool Clinklist :: Insert (INT Position, Const Char * Element) {INT INDEX = 0; int Len = 0;
// Temporary node pointer struct node * temp = null; struct node * back = null; struct node * new = null;
IF ((position <1) || (position> (m_count 1))) // The specified location is incorrect {RETURN FALSE;} if (m_head! = null) // Header pointer is not empty {TEMP = m_headad -> Next; // Point to the first node
/ / The linked list is empty, there is no node if (null == Temp) {newNode = new node; / / to apply for memory space if (newNode! = Null) // memory application for the first node {m_head-> Next = NewNode; // Metet Pointer pointing to the first node
NewNode-> last = m_head; // The first node points to the header pointer
// Copy Node Element Value LEN = Strlen (Element); NewNode-> Element = New Char [LEN 1]; STRCPY (New Now-> Element, Element);
NewNode-> Next = null; // No subsequent node
m_count; // node number plus 1
Return True;
} ELSE // Memory application failed {Return False;}
} // end of if
// While 1, 1 and M_Count, when the node is inserted in the m_count position, while (TEMP! = Null) { index;
IF (position == index) // Find the matching position {newNode = new node; / / for the inserted node application memory space IF (newNode! = null) // memory application success {TEMP-> Last-> next = newnode ; // make the previous node point to the newly created node
NewNode-> Last = Temp-> last; // Remove the newly created node to a node
// Copy Node Element Value LEN = Strlen (Element); NewNode-> Element = New Char [LEN 1]; STRCPY (New Now-> Element, Element);
NewNode-> Next = Temp; / / The newly created node points to the original node in this location, that is, the next node.
Temp-> last = newNode; / / The original node (ie, the next node) at this location points to the newly created node
m_count; // node number plus 1
Return True;
} ELSE // Memory application failed {Return False;}
} // end of if
Back = TEMP; / / Save the pointer Temp = Temp-> next; // point to the next node} // end of while
/ / When the node is inserted on the position m_count 1, the NEWNODE = new node; // is applied for a memory space if (Newode! = NULL) // memory application for the inserted node {back-> next = newNode; // make the previous one The node points to the newly created node
NewNode-> Last = BACK; // Remove the newly created node to one node
// Copy Node Element Value LEN = Strlen (Element); NewNode-> Element = New Char [LEN 1]; STRCPY (New Now-> Element, Element);
NewNode-> Next = null; // No subsequent node
m_count; // node number plus 1
Return True;
} ELSE // Memory application failed {Return False;}
} ELSE // Header pointer is empty {Return False;}
}
/ / Add Node Bool Clinklist :: Insert (const char * beforeelelec, const char * element) {int LEN = 0;
// Temporary node pointer struct node * temp = null; struct node * newNode = NULL;
IF (m_head! = null) // The head pointer is not empty {TEMP = m_head-> next; // Point to the first node if (null == TEMP) // Link table is empty, no node {Return False; } Else // Link list is not empty {while {if (strcmp (beforeelelec, temp-> element == 0) // Find the matching node {newNode = new node; // is inserted node Apply for memory space if (newNode! = Null) // application success {TEMP-> Last-> next = newNode; // Point the previous node to the newly created node
NewNode-> Last = Temp-> last; // Remove the newly created node to a node
// Copy Node Element Value LEN = Strlen (Element); NewNode-> Element = New Char [LEN 1]; STRCPY (New Now-> Element, Element);
NewNode-> Next = Temp; // Remove the newly created node to the next node
Temp-> last = newnode; // Make the next node points to the newly created node m_count; // Number of nodes plus 1
Return True;
} ELSE // Memory application failed {Return False;}
} // end of if
Temp = Temp-> Next; // Refers to the next node
}
Return false; / / Return False when you can't find a match
}
} ELSE // Header pointer is empty {Return False;}
}
/ / Delete Nodes Bool Clinklist :: Delete (INT Position) {INT INDEX = 0;
Struct Node * Temp = NULL; // Temporary node pointer
IF ((Position <1) || (position> m_count)) // The specified location is incorrect {Return False;}
IF (m_head! = null) // Header pointer is not empty {temp = m_head-> next; // Point to the first node if (Temp! = null) // Link list is not empty {while (Temp! = null ) { index;
IF (position == index) // Find the matching position {TEMP-> last-> next = TEMP-> next; // Remove the previous node points the next node
IF (Temp-> Next! = null) // Presence Next Node {TEMP-> NEXT-> Last = TEMP-> last; // Remove the next node to the forward node}
/ / Release the memory space occupied by the node to be deleted, delete; delete temp;
--M_count; // Number of nodes minus 1
Return True;
}
Temp = Temp-> Next; // Refers to the next node
} // end of while
Return False;
} ELSE // Link list is empty {Return False;}
} ELSE // Header pointer is empty {Return False;}
}
/ / Delete the specified node BOOL CLINKLIST:: Delete (const char * element) {struct node * temp = null; // temporary node pointer
IF (m_head! = null) // Header pointer is not empty {temp = m_head-> next; // Point to the first node if (Temp! = null) // Link list is not empty {while (Temp! = null ) {IF (Element, Temp-> Element) == 0) // Find the matching node {TEMP-> last-> next = TEMP-> next; // Remove the previous node to the next node IF (Temp -> Next! = null) // Presence Next Node {TEMP-> Next-> Last = TEMP-> Last; // Remove the next node to a node}
/ / Release the memory space occupied by the node to be deleted, delete; delete temp;
--M_count; // Number of nodes minus 1
Return True;
} // end of if
Temp = Temp-> Next; // Refers to the next node
} // end of while
Return false; / / Return False when you can't find a match
} ELSE // Link list is empty {Return False;}
} ELSE // Header pointer is empty {Return False;}
}
/ / Modify the node Bool CLINKLIST :: Update (int position, const char * element) {int = 0; int Len = 0;
Struct Node * Temp = NULL; // Temporary node pointer
IF ((Position <1) || (position> m_count)) // The specified location is incorrect {Return False;}
IF (m_head! = null) // Header pointer is not empty {temp = m_head-> next; // Point to the first node if (Temp! = null) // Link list is not empty {while (Temp! = null ) { index;
IF (position == index) // Find the matching position {// Release the original node element value occupied by DELETE [] TEMP-> Element; Temp-> Element = NULL;
// Copy the new node element value len = strlen (element); temp-> element = new char [len 1]; strcpy (TEMP-> Element, Element);
Return True;
} // end of if
Temp = Temp-> Next; // Pointing the next node} // end of while
Return False;
} ELSE // Link list is empty {Return False;}
} ELSE // Header pointer is empty {Return False;}
}
/ / Modify the specified node as the new node value BOOL CLINKLIST :: Update (const char * OLDLEN = 0;
Struct Node * Temp = NULL; // Temporary node pointer
IF (m_head! = null) // Header pointer is not empty {temp = m_head-> next; // Point to the first node if (Temp! = null) // Link list is not empty {while (Temp! = null {IF (Strcmp (Oldelem, Temp-> Element) == 0) // Find the matching node {// Release the original node element value occupied by the memory space DELETE [] TEMP-> Element; Temp-> Element = null;
// Copy the new node element value len = strlen (newelem); temp-> element = new char [len 1]; strcpy (TEMP-> Element, newElem);
Return True;
}
Temp = Temp-> Next; // Refers to the next node
} // end of while
Return false; / / Return False when you can't find a match
} ELSE // Link list is empty {Return False;}
} ELSE // Header pointer is empty {Return False;}
}
/ / Returns the location of the specified node on the list BOOL CLINKLIST :: Search (const char * element, int & position) {int index = 0;
Struct Node * Temp = NULL; // Temporary node pointer
Position = 0;
IF (m_head! = null) // Header pointer is not empty {temp = m_head-> next; // Point to the first node if (Temp! = null) // Link list is not empty {while (Temp! = null ) { index;
IF (Strcmp (Element, Temp-> Element) == 0) // Find the matching node {position = index; // Return the correct position of the specified node
Return True;
}
Temp = Temp-> Next; // Refers to the next node
} // end of while
Return false; // Return false} else // linked list when you can't find the matching node is empty {Return False;}
} ELSE // Header pointer is empty {Return False;}
}
/ / Returns the node elements on the specified location Bool Clinklist :: Search (int position, char ** Element) {int index = 0; int LEN = 0;
Struct Node * Temp = NULL; // Temporary node pointer
IF ((Position <1) || (position> m_count)) // The specified location is incorrect {Return False;}
IF (m_head! = null) // Header pointer is not empty {temp = m_head-> next; // Point to the first node if (Temp! = null) // Link list is not empty {while (Temp! = null ) { index;
If (position == index) // Find the matching position {// Return to the found node element value STRCPY (* Element, Temp-> Element);
Return True;
} // end of if
Temp = Temp-> Next; // Refers to the next node
} // end of while
Return False;
} ELSE // Link list is empty {Return False;}
} ELSE // Header pointer is empty {Return False;}
}
Bool Clinklist :: Sort (Void) / / Sort the chain list (bubble sort) {int LEN = 0; char * tempelem = null;
// Temporary node pointer struct node * temp = null; struct node * back = null;
IF (m_head! = null) // Header pointer is not empty {temp = m_head-> next; // Point to the first node if (Temp! = null) // Link list is not empty {while (Temp! = null ) {Back = TEMP-> Next; // BACK points to the next node of the node points to the TEMP (back! = Null) {// Temp-> Element is larger than the back-> element, two node elements exchange IF (Strcmp-> Element, Back-> Element> 0) {// Copy Temp-> Element Points Node Element Values to the memory space points to the TempleEm to go to Len = Strlen (Temp-> Element); TempleLem = New Char [LEN 1]; STRCPY (TempleLem, Temp-> Element); delete [] Temp-> Element; temp-> element = null; len = 0;
// Copy the node element value pointed to the TEMP-> Element to the memory space points to the memory space to Len = Strlen (back-> element); TEMP-> Element = new char [LEN 1]; STRCPY TEMP-> Element, Back-> Element;
DELETE [] Back-> Element; back-> element = null; len = 0;
/ / Copy the node element value points to the TempleEm to the memory space points to the memory pointed to the memory space to len = strlen (TempleElem); back-> element = new char [len 1]; strcpy (back-> element, tempelem) );
DELETE [] TempleLem; TempleLem = NULL; LEN = 0;
} // end of if
Back = back-> next; // back point to the next node
} // end of while
Temp = Temp-> Next; // Temp points to the next node
} // end of while
Return True; // Return to TRUE
} ELSE // Link list is empty {Return False;}} Else // Header pointer is empty {Return False;}
}
// Sequential display element value Bool CLINKLIST :: Display (void) {struct node * temp = null; // temporary node pointer
IF (m_head! = null) // Header pointer is not empty {TEMP = m_head-> next; // Point to the first node if (null == TEMP) // Link list is empty {Return False;}
// Sequentially output Element value while (Temp! = Null) {cout << Temp-> Element << '/ t'; Temp = Temp-> Next; // Refers to the next node}
Cout << Endl;
Return True;
} ELSE // Header pointer is empty {Return False;}
}
///