Node class #1ndef node_h
#define node_h
Template
{
PUBLIC:
TYPE DATA;
Node
Node (): data (Type ()), link (null) {}
Node (const type & item): Data (item), link (null) {}
Node (const type & item, node
}
#ENDIF
[Description] Because there are too many places used in this structure in the data structure, if you use the original book of the name of the friend, the statement doesn't know how much it is better than this class. It is better to open a member. In fact, this structure is just the Struct in C, except for the convenience of initialization, there is no need for any method, the original book is a snake. Here's it shows that the PUBLIC section of the linked list does not return a function of Node or Node *, so other classes are not likely to use this open interface to operate the node in the list.
[Important Modification] The default constructor of the original book is this Node (): Data (NULL), LINK (NULL) {}. I was originally written, the result When I expanded, I found that it was wrong. When Type is a structure rather than a simple type (int, ...), no NULL value cannot be simplified. Doing so makes the defined template can only be used for very few simple types. Obviously, the default constructor of Type should be called here. This also requires that the class here must have a default constructor. This default constructor is used when constructed the chain table below. Of course, here is the latch table of the agreement with the head node, please think yourself if you don't take the head node.
[Gossip] Please do not have any doubts about INT * P = New Int (1); this syntax is actually int rt can also see a Class.
Single-link table class
#ifndef list_h
#define list_h
#ifndef Ture
#define Ture 1
#ENDIF
#ifndef false
#define false 0
#ENDIF
Typedef int BOOL;
#include "node.h"
Template
{
/ / Basically no parameters of member function operations are current nodes, namely the node of Current fingers
// I think "1st Node" is the 0th node, please note that the last node is the 0th node when the length is 1.
PUBLIC:
List () {first = current = last = new node
~ List () {makeempty (); delete first;
void makeempty () // blanket
{
Node
While (first-> link! = null)
{
Q = first-> link;
First-> link = q-> link;
Delete q;
}
INITIALIZE ();
}
Bool isempty ()
{
IF (first-> link == null)
{
INITIALIZE ();
Return Ture;
}
Else Return False;
}
INT length () const // calculates a single-link length with a header node
{
Node
INT count = 0;
While (p! = null)
{
P = P-> link;
COUNT ;
}
Return count;
}
TYPE * GET () // Returns the address of the current node's data field
{
IF (current! = null) Return & Current-> Data;
Else Return NULL;
}
Bool Put (Type const & value) // change the DATA of the current node to make it value Value
{
IF (Current! = NULL)
{
Current-> Data = Value;
Return Ture;
}
Else Return False;
}
TYPE * getNext () // Returns the address of the data field of the next node of the current node, does not change the CURRENT
{
IF (current-> link! = null) Return & Current-> link-> data;
Else Return NULL;
}
TYPE * NEXT () // Moves Current to the next node, return the address of the node data field
{
IF (Current! = Null && Current-> Link! = NULL)
{
PRIOR = CURRENT;
Current = Current-> Link;
Return & Current-> Data;
}
Else
{
Return NULL;
}
}
Void insert (const type & value) // Insert the node behind the current node, does not change the current
{
Node
Current-> link = P;
}
Bool INSERTBEFORE (CONST TYPE & VALUE) / / Insert a node in front of the current node, does not change the Current, change the prior
{
Node
IF (Prior! = NULL)
{
P-> Link = Current;
prior-> link = p;
PRIOR = P;
Return Ture;
}
Else Return False;
}
Bool Locate (INT I) // Moves Current to iPatho
{
IF (i <= -1) Return False;
Current = first-> link;
For (int J = 0; current! = NULL && J link)
PRIOR = CURRENT;
IF (current! = null) Return Ture;
Else Return False;
}
Void first () // Moving Current to the head
{
Current = first;
PRIOR = NULL;
}
Void end () // Mobile current to the end
{
IF (Last-> Link! = NULL)
{
For (; current-> link! = null; current = current-> link) prior = current;
Last = CURRENT;
}
Current = last;
}
Bool Find (const type & value) // Move current to data equal to Value node
{
IF (ISEMPTY ()) Return False;
For (current = first-> link, prior = first; current! = null && current-> data! = value;
Current = Current-> Link)
PRIOR = CURRENT;
IF (current! = null) Return Ture;
Else Return False;
}
Bool remove () // Deletes the current node, and the current point to the next node, if the current is tailing, then the current = null
{
IF (Current! = Null && Prior! = NULL)
{
Node
prior-> link = p-> link;
Current = P-> link;
Delete P;
Return Ture;
}
Else Return False;
}
Bool removeafter () // Delete the next node of the current node, does not change the CURRENT
{
IF (current-> link! = null && current! = NULL)
{
Node
Current-> link = P-> link;
Delete P;
Return Ture;
}
Else Return False;
}
Friend Ostream & Operator << (Ostream & Strm, List
{
L.First ();
While (l.current-> link! = null) strM << * L.Next () << "
STRM << Endl;
L.First ();
Return Strm;
}
protected:
/ * Is mainly added to the efficient income algorithm. Because INSERT (), remove (), removeafter () is possible to change Last but do not change Last so this algorithm is not correct if this is not used in public unless it is not used. But in addition to being very useful in the queue, LAST is very useful in other times, there is no need to reduce INSERT (), remove () efficiency for this purpose, so put this part into protected, actually mainly inheriting the queue * / Void LastInsert (const type & value)
{
Node
Last-> link = P;
Last = P;
}
Void Initialize () // Resets the pointer when the table is empty
{
Current = last = first;
PRIOR = NULL;}
// This part of the function returns the type name Node
Node
{
Return Current;
}
Node
{
PRIOR = CURRENT;
Current = Current-> Link;
Return Current;
}
Node
{
Return Current-> Link;
}
Node
{
Return first;
}
Node
{
Return Last;
}
Node
{
Return prior;
}
Void Putlast (Node
{
Last = P;
}
// This part is inserted into the delete function that does not establish or delete the node, which is an in situ operation.
void insert (node
{
P-> Link = Current-> LINK;
Current-> link = P;
}
Void insertbefore (node
{
P-> Link = Current;
prior-> link = p;
PRIOR = P;
}
Void LastInsert (Node
{
P-> link = NULL;
Last-> link = P;
Last = P;
}
Node
{
IF (Current! = Null && Prior! = NULL)
{
Node
Prior-> link = current-> link;
Current = Current-> Link;
Return P;
}
Else Return NULL;
}
Node
{
IF (current-> link! = null && current! = NULL)
{
Node
Current-> Link = Current-> link-> link;
Return P;
}
Else Return NULL;
}
Private:
List (Const List
Node
}
#ENDIF
[Description] I put the function of the original book's cursor class Iterator in the chain table class, shielded the return value as Node and Node * type interface, such a simple, practical, and expandable performance.