The introduction of the original book is very simple, and the implementation is incomplete (of course, if the complete is also repeated). And I didn't think there was anything else to use, and he should be generated for a special issue, this is just a personal opinion. I have a loop chain list from the list class, which requires a few details.
1. Constructor: When the class is instantiated, the construction function of the base class first; therefore, the work of the initialization loop linked list is to point to the head node of the header node of the empty chain table with the header to form a circle.
2. Destructive function: When the object is released, the secting function of the derived class will first call the destructor of the base class. Therefore, the release of the loop chain list only needs to turn the loop chain to a normal single-link list, and then this single-linked list will be released by the destructor of the base class. Here, it is assumed not to use this statement base * p = new drived; delete p; because I didn't add Virtual before ~ List (). You can refer to all kinds of C books to make such problems.
3. Decoction function: The condition is not whether the LINK of the detection head node is empty, but does not point to the header node.
4. Set the empty function: The original obvious cannot work, in fact, as long as it is deleted from the position of the head, it is possible.
5. next (): Incounted to the head node to jump over.
6. REMOVE (): The current node cannot be deleted, delete the consequences of the header node, because the prior pointer is not necessarily empty in the loop chain list - it should be certain, but due to inheritance Part of the list function, so it is not necessarily, so that the original remove () check may be invalid); if deleted is the tail node, the current pointer will point to the header node after delete, and you want to jump into the next one. In short, when using next () and remove (), the existence of the header node cannot be made to the outside world, otherwise, when you cycle count, the header node is counted.
7. "<<" must be rewritten, otherwise, when you perform COUT << Circlist; this kind of thing, God, you want to go; of course, only need to copy over and modify the loop judgment.
8. End () will not work, considering that if the original function is implemented, the efficiency is low, and the use is not large, so modifying the end () function is defined as a corrected LAST pointer. To avoid confusion, put it in private, and do not provide this feature.
Definition and implementation
#ifndef circlist_h
#define Circlist_H
Include "List.h"
Template
{
PUBLIC:
Circlist () {pgetfirst () -> link = pgetfirst ();
~ Circlist () {end (); pgetlast () -> link = null;}
Bool isempty ()
{
Node
Return P-> Link == P;
}
TYPE * NEXT ()
{
IF (pnext () == pgetfirst ()) PNEXT ();
Return get ();
}
BOOL Remove ()
{
IF (! iSempty ())
{
IF (Pget () == pgetfirst ()) Return False; list
IF (pget () == pgetfirst ()) pnext ();
Return Ture;
}
Return False;
}
void makeempty ()
{
First ();
While (! iSempty ()) RemoveAfter ();
}
Void LastInsert (Const Type & Value)
{
End ();
List
}
Private:
void end ()
{
IF (pgetlast () -> link! = pgetfirst ())
{
Node
For (Node
PUTLAST (P);
}
}
Friend Ostream & Operator << (Ostream & Strm, Circlist
{
Cl.first ();
Node
While (cl.pget () -> link! = pfirst) strm << * cl.next () << "
STRM << Endl;
Cl.first ();
Return Strm;
}
}
#ENDIF
[Description] For the future of Joseph, I added a LastInsert. If INSERT is inserted, it is certainly inverted input to solve it, but such a practice will be suspected, and constant Locate is obviously too low. Obviously, Find, LOACTE, LENGTH is inherited, and it will eventually think about it, I don't have a reinforcement here:
Find
You can copy the original code to modify the loop determination, but you can also do it. The method is to use the lookup table described later, set the value in the header node, so it will be successful. Then check if the current node is a header node to determine if it is really looking for success. If you complete this function yourself, there will be a lot of gains. Give an example:
Bool Find (Const Type & Value)
{
Pgetfirst () -> DATA = Value;
List
IF (pget () == pgetfirst ()) Return False;
Return Ture;
}
Locate
The original realization is in fact, there is no semantic problem, nothing more than the turning circle, of course, how to change you. Suggestions
Length
Check the legal law of the positioning value, which can be killed in advance.
Length
How long do you say that the circular list is? Just like how long long-distance running competition is
400
The runway of the rice is the same. It is recommended to add a private data member.
Length
Adjusted at each inserted deletion, because the change of the change is more, so I am lazy, mainly thinking less.
Joseph has almost mentioned that the loop chain is always mentioned to Joseph, and I still told me to solve this problem to construct a circular chain list, of course, is a static linked list in Basic. It seems that the loop chain is existed for this problem. In order to take care of people who have never heard of it, I briefly introduce this question: Saying is a travel agency to select a lucky passenger from N famous passengers and provide free global travel services. Method is to stand into a circle, then select one M, start the number from 1st person, report to M, this person OUT, then start from the next person to 1 报 number, repeat this process, until the last left A person is a lucky star. The question is who is lucky? Or how you can win the award.
I tested the correctness of each member function of the circular chain table in this problem, the corresponding functions are as follows:
Void circlisttest_int ()
{
Circlist
COUT << Endl << "Integrated Circulatory Lin Test: Solving about Joseph Problem" << Endl;
INT N, M; // N is the total number, M is reported value
COUT << "Enter the total number of people:";
CIN >> N;
Cout << "Enter the value of the number:";
Cin >> m;
For (int i = 1; i <= n; i ) a.lastinsert (i);
Cout << a;
a.locate (0);
For (i = 0; i { For (int J = 0; J COUT << "" "<< * a.get () <<" The passenger was eliminated "<< Endl; A.Remove (); } Cout << "" "<< * a.get () <<" "bit passengers win" << Endl; Cout << a; A.Makeempty (); Cout << a; } [Post "is the derived class of the loop chain. I discover many hidden dangers, such as the original find, in the integer linked list, no exception will stop in the head, So what you see now is now a big change in writing, or some, I haven't found it yet, welcome to correct.