Data structure learning (C ++) - two-way linked list

zhaozj2021-02-16  52

The original book is much more content, and at least a lot relative to the circular chain list. I believe that when you make a single-stranded pointer domain, this part should be difficult to fall. Now my question is, can you send two-way linked list from a single-link table?

You can have several practices:

One is to define a double-link node - but, its name must be called node, this is something wrong; otherwise you have to copy a single-link realization file, put the Node all to your double The name of the link, but this is not called inherited.

Another approach is to define a structure for example:

Template Class NewType

{

PUBLIC:

TYPE DATA;

Node * link;

}

When you derive a two-way linked list, you write Template Class DBLLST: Public List >, pay attention to the continuous two ">" There is a space. Or do not define such a structure, take the Node type directly, for example, I will give it below. However, please note that you want to complete the "==" overload, otherwise you have to rewrite the Find function, and other certain operations are not convenient.

Before starting your bidirectional linked list, you want to add the interface to the current pointer and the current forward-drive pointer to the base class, as shown below:

protected:

Void Put (Node * p) // Try not to use, the two-way linked list will use this complete movement

{

Current = P;

}

Void Putprior (Node * p) // Try not to use, the reason is the same

{

PRIOR = P;

}

Because this interface is very dangerous, it is almost not available, so I have not given it in front, but I have to complete the two-way linked list of "outstanding" advantages - moving the current pointer forward, must be used. In addition, I have never planned from a single-link watch to derive double-linked list, below you will see that this process is very annoying, or even rewriting a provincial thing, the effectiveness is not very good, this labor force What does it mean to do if it does not want? Indeed, I also think I am in the rhombus. (Don't take me with eggs)

Definition and implementation

#ifndef dbllist_h

#define dbllist_h

#include "list.h"

Template class dbllist: public list >

{

PUBLIC:

TYPE * GET ()

{

IF (Pget ()! = null) Return & Pget () -> Data.Data;

Else Return NULL;

}

TYPE * NEXT ()

{

PNEXT ();

Return get ();

}

TYPE * PRIOR ()

{

IF (PgetPrior! = NULL)

{

PUT (PgetPrior ());

Putprior (Node > *) pget () -> data.link;

Return get ();

}

Return NULL;

}

Void insert (const type & value)

{

Node newdata (value, (node ​​ *) pget ());

List > :: insert (newdata); if (pgetnext () -> link! = Null)

PgetNext () -> link-> data.link = (node ​​ *) pgetnext ();

}

BOOL Remove ()

{

IF (list > :: remove ())

{

Pget () -> data.link = (node ​​ *) PgetPrior ();

Return Ture;

}

Return False;

}

}

#ENDIF

[Description] Completed only the most important Insert and Remove functions and the most featured PRIOR () function, which did not re-implement. So, you use a single-link list here, I don't guarantee certain correct. Moreover, the pointer type conversion here is dependent on the compiler, and I can't affirm that other compiler can be compiled and correct. For DATAs who don't let PRIOR return to head nodes, I consider it three. Use first (); get (); such a combination can also return, so I don't care about him, so if you use prior to return NULL, it will The head node of the DATA output is coming.

[Supplement] As for the two-way circular chain list, you can also derive from this two-way linked list (in the method of derived loop linked list); or derive from the loop list (in the method of derivating the two-way linked list), it will take the same example (then doing this, I It's really troubled to vomit blood). At this point, it can be concluded that various structures of the linked list can be derived from a single-link table. In other words, the single-link table is fundamentally, if the study is a single-link table, various chain structures are not difficult.

A small test program

Void dbllisttest_int ()

{

DBLLIST a;

For (int i = 10; i> 1; i -) a.insert (i);

For (i = 10; i> 1; I -) cout << * a.next () << "

a.first ();

Cout << Endl;

Cout << * a.next () << endl;

Cout << * a.next () << endl;

Cout << * a.next () << endl;

Cout << * a.next () << endl;

A.Remove ();

Cout << * a.get () << Endl;

Cout << * a.prior () << Endl;

Cout << * a.prior () << Endl;

Cout << * a.prior () << Endl;

}

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

New Post(0)