Data structure Learning (c + +) bidirectional linked list

Source: Internet
Author: User
Tags data structures
This part of the original book is a lot, at least relative to the circular list is a lot. Believe that when you make a list of the pointer field clear, this part should not fail you. Now my question is, can you derive a two-way list from a single linked list? <?xml:namespace prefix = o ns = "Urn:schemas-microsoft-com:office:office"/>


you can have several practices:


one is to define a two-link node first--but, its name must be called node, this is no way, otherwise you will have to copy a single list of the implementation file, the node all of which are replaced by your two-link node name, but this is not called inheritance.


Another approach is to first define a structure such as this:


Template class Newtype
{
Public
Type data;
Node *link;
}



When you derive a two-way list, this writes template <CALSS type> class Dbllist:public List<newtype <Type>, noting that there are spaces between the two ">". Or simply do not define such a structure, directly to the node type to do, for example, I give the following. However, please note that to complete the "= =" overload, otherwise, you have to rewrite the Find function, and some other operation is not convenient.


Add the interface to modify the current pointer and the current predecessor pointer in the base class of a single linked list before you begin to complete a two-way list of links that you derive from a single list, as follows:


Protected
void put (Node *p)//As far as possible, bidirectional linked list will use this to complete forward movement
{
current = P;
}
void Putprior (Node *p)/As far as possible, for the same reason
{
Prior = P;
}



because this interface is dangerous and almost impossible to use, I didn't give it in the front, but to do the best thing about the two-way list-moving the current pointer forward must be used. Besides, I never planned to derive a double linked list from a single list, as you will see, this process is annoying, or even better to rewrite a come to the trouble, execution efficiency is not very good, this thankless thing to do it what meaning? Yes, I think I'm in a dead alley too.


definition and Implementation


#ifndef Dbllist_h
#define Dbllist_h
#include "List.h"
Template class Dbllist:public list< Node >
{
Public
Type *get ()
{
if (Pget ()!= NULL) return &pget ()->data.data;
else return NULL;
}
Type *next ()
{
Pnext ();
Return to get ();
}
Type *prior ()
{
if (Pgetprior!= NULL)
{
Put (Pgetprior ());
Putprior (node< Node >*) pget ()->data.link);
Return to get ();
}
return NULL;
}
void Insert (const Type &value)
{
Node newdata (value, (Node *) pget ());
list< Node >::insert (NewData);
if (Pgetnext ()->link!= NULL)
Pgetnext ()->link->data.link = (Node *) Pgetnext ();
}
BOOL Remove ()
{
if (list< Node >::remove ())
{
Pget ()->data.link = (Node *) Pgetprior ();
return ture;
}
return FALSE;
}
};
#endif



"description" completes only the most important insert and remove functions and the most characteristic prior () function, and the others are not implemented again. So, the other way you use a single linked list here, I'm not sure it's right. Also, the pointer type conversion here relies on the compiler implementation, and I am not sure that other compilers can be compiled correctly. For data that doesn't allow prior to return the header node, I think twice, anyway, with first (); Get (), such a combination can return, so do not care about him, so if you use prior traversal until the return of NULL, the head node will be the data output.


"added" as to the two-way circular chain list, you can also derive from this doubly linked list (in the form of a derived cyclic list), or derive from a circular list (in the way of a derived two-way list), not one of the examples (in this case, I'm really upset about vomiting blood). At this point, we can draw a conclusion that the various structures of the linked list can be derived from the single linked list. In other words, single linked list is the fundamental, if the study of a single linked list, all kinds of chain structure is not difficult.


a small section of the test program


void Dbllisttest_int ()
{
Dbllist A;
for (int i = 1 I, i--) A.insert (i);
for (i = 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;
}



"PostScript" from my two-way linked list irresponsible implementation, I do not want to achieve two-way linked list, I just try how to maximize the use of existing classes to achieve this type. Practice has proved better to rewrite one. Others look good, and they don't have to be so annoying to write. However, this process gives me a deeper understanding of the invocation and return of functions. If you can write the first time on the Insert function here, I believe you must have a certain feeling of C + +. I also think that only by doing some innovation, can the most mature things more in-depth understanding. For example, these data structures, in the C + + standard library (STL) can be used directly, why we have worked hard to write, the result is not as good as the original. In order to learn, this is the reason, this is all look very stupid things happen.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.