Use the C ++ class to classify a one-way linked list. After saving the list, you can conveniently sort the numbers and insert them. The debugging is successful!
# Include using namespace STD;
Class listnode // create node Components
{
Public:
Listnode (INT datavalue) // constructor 1
{Value = datavalue ;}
Listnode () // constructor 2
{}
Int value; // node Value
Listnode * Previous; // pointer of the previous Node
Listnode * Next; // pointer to the next node
Listnode operator = (const listnode * ln) // defines this class of equal sign overload
{
Value = ln-> value;
Previous = ln-> previous;
Next = ln-> next;
}
};
Class list // create a linked list using listnode nodes
{
Public:
List () // constructor initialization object
{
Head = NULL;
Tail = NULL; // point the pointer to the header and tail to null
Listcount = 0; // initialize the number of nodes to zero.
Current = NULL; // The Node location of the current operation. The Initialization is empty.
}
Int listcount; // record variable that defines the number of nodes
Listnode * head; // defines the header pointer.
Listnode * tail; // defines the tail pointer.
Listnode * Current; // defines the pointer of the current operation
Void movehead (); // move to the header
Void movetail (); // move to the end;
Void movenext (); // move to the next one;
Void movefore (); // move a position up
Void append (INT); // Add new data to the end of the entire linked list
Void insert (INT); // Add new data before the data being operated
Bool isnull (); // determines whether the linked list is empty.
Bool isbof (); // determines whether the current pointer of the current operation is in the header.
Bool iseof (); // determines whether the current pointer of the current operation is at the end.
Int getcurrent (); // get the value of the value pointed to by the current operation pointer
Void insertascend (INT); // insert in ascending order
Void insertdescend (INT); // insert in descending order
Void printall (); // output all data from start to end to the screen
};
// The specific content of all functions is as follows:
Void list: movehead ()
{
Current = head; // point the current pointer to the header pointer
}
Void list: movetail ()
{
Current = tail; // point the current pointer to the tail pointer
}
Void list: movefore () // move a position up
{
If (! Isbof () // determines whether the pointer is in the header at this time. If it is not moved, it is not moved up.
{
Current = Current-> previous;
}
}
Void list: movenext () // move the cursor to the next position.
{
If (! Iseof () // determines whether the pointer is at the end at this time. If not, the pointer can be moved backward.
{Current = Current-> next ;}
}
Void list: append (INT datavalue) // Add data at the end of the entire linked list
{
Listnode * newnode = new listnode; // open up a new node
Newnode-> value = datavalue; // pay the parameter to the new node.
If (isnull () // determines whether the linked list is empty. If it is empty, add the data directly.
{
Head = newnode; // the header Pointer Points to the new node.
Tail = newnode; // The tail pointer also points to the new node.
Tail-> next = NULL; // The tail Pointer Points to null.
Head-> previous = NULL; // the header pointer should point to null when the previous node is saved.
Current = tail; // the pointer of the current operation points to the newly added data
Listcount ++; // Add a new number
}
Else // if not empty, add
{
Tail-> next = newnode; // Add the tail pointer to direct the tail pointer to the new node.
Newnode-> previous = tail; // do not forget to assign the pointer of the new node to the previous node.
Tail = newnode; // when the tail pointer is behind the tail-> next = NULL; current = tail; listcount ++;
}
}
Void list: insert (INT datavalue) // note that a new node is added before the value of the current operation.
{
Listnode * newnode = new listnode;
Newnode-> value = datavalue;
If (isnull () // same as above
{
Head = newnode;
Tail = newnode;
Tail-> next = NULL;
Head-> previous = NULL;
Current = head; listcount ++;
}
Else if (isbof () // determines whether the current operation pointer is in the header pointer
{Newnode-> next = head; // point the new node to the header node.
Head-> previous = newnode;
Newnode-> previous = NULL;
Head = newnode;
Current = head; // update the pointer value of the operation Node
}
Else // Add a new node before the current operation pointer
{
Newnode-> next = current;
Current-> previous-> next = newnode;
Newnode-> previous = Current-> previous;
Current-> previous = newnode;
Current = newnode;
Listcount ++;
}
}
Bool list: isnull () // a function that determines whether it is null.
{
If (listcount = 0)
Return true;
Return false;
}
Bool list: isbof () // determines whether it is a header pointer.
{
If (current = head)
Return true;
Return false;
}
Bool list: iseof () // judge whether it is a tail pointer
{
If (current = tail)
Return true;
Return false;
}
Int list: getcurrent () // get the value of the current operation value
{Return Current-> value ;}
Void list: insertascend (INT datavalue) // Insert new data in ascending order
{
Listnode * newnode = new listnode;
Newnode-> value = datavalue;
If (isnull ())
{
Head = newnode;
Tail = newnode;
Tail-> next = NULL;
Head-> previous = NULL;
Current = head;
Listcount ++;
}
Else if (Head-> value> datavalue) // determines whether the header pointer is greater than the value of the new node. If the value is greater than the value of the new node, the new node can be inserted directly in front of the header.
{
Movehead (); // move the pointer to the header
Insert (datavalue); // The above insert function is used
}
Else if (tail-> value <datavalue) // determines whether the header pointer is greater than the value of the new node. If the value is greater than the value of the new node, the new node can be inserted directly in front of the header.
{Movetail ();
Append (datavalue );
}
Else if (getcurrent ()> datavalue) // if the current operation value is greater than the new node Value
{
While (getcurrent ()> datavalue)
{Movefore (); // a pointer is taken up.
}
Movenext (); // because the pointer points to a value smaller than the new node, this is why the above loop jumps out, so we should move the pointer back to a position
Insert (datavalue );
}
Else
{
While (getcurrent () <= datavalue) // if the current Pointer Points to a value smaller than the new node value at this time
{Movenext (); // move a pointer backward until you find the first value greater than it.
}
Insert (datavalue); // Insert before the first value found greater than this value
}
}
Void list: insertdescend (INT datavalue) // insert data in descending order. Analysis is similar to insert data in ascending order.
{Listnode * newnode = new listnode;
Newnode-> value = datavalue;
If (isnull ())
{Head = newnode;
Tail = newnode;
Tail-> next = NULL;
Head-> previous = NULL;
Current = head;
Listcount ++;
}
Else if (tail-> value> datavalue)
{
Movetail ();
Append (datavalue );
}
Else if (Head-> value <datavalue)
{Movehead ();
Insert (datavalue );}
Else if (getcurrent ()> datavalue)
{While (getcurrent ()> datavalue)
{Movenext ();}
Insert (datavalue );
}
Else {While (getcurrent () <= datavalue)
{Movefore ();}
Movenext (); // whether there is a difference between the insertion in descending order and the insertion in ascending order in this position to return a pointer, careful analysis
Insert (datavalue );
}
}
Void list: printall () // print all data
{Movehead ();
Int I = 0;
While (current! = NULL)
{If (I> 7)
{
I = 0;
Cout <Endl;
}
If (I> 0)
Cout <"";
Cout <(current-> value );
Current = Current-> next;
I ++ ;}
}
// An example of using this L-Type Function
Int main ()
{
// Freopen ("classdata.txt", "r", stdin );
List num;
Int I, N;
Cin> N;
For (I = 0; I <n; I ++)
{
Int T;
Cin> T;
Num. insertdescend (t );
}
Num. printall ();
Return 0;
}