Data Structure Learning (C ++) - How to chain into different types of objects in a linked list

zhaozj2021-02-16  53

It seems that you also noticed, no matter how it is defined, it seems that the objects in a linked list are the same type. In fact, this is also a must, otherwise, what is the type of the return value of the function in the return node? However, the demands of people are endless ... (omitted that I am a few hundred words). The purpose of different object chains in a linked list is to be convenient, now remember this principle, and the back discussion is based on this principle, otherwise we are the technology madman - bias to achieve some seems impossible thing.

The principle of achieving this goal is actually very simple, as long as the different types of objects become the same type. Look at the structure definition below:

Struct MOBJECT

{

Void * p;

INT ObjectType;

}

When a subject is linked into a chain list, the pointer to this object is assigned to the P, and the object type is recorded. When this node is obtained, the object type of the P refer to the value of the ObjectType, thereby reducing the original instrument, and has obtained the original object.

This method is actually used by the broad menu mentioned later. Obviously, such MOBJECT support is predetermined, you maintain your ObjectType list, each addition of a type of support, you need to give its alternative value in the ObjectType list, then in the corresponding Switch (ObjectType) Give this type of CASE statement. I am very annoying, here is another method, in fact, this is the principle, the difference is that this annoying work is given to the compiler.

Remember the principle that you emphaded in front of it, why do we put different types of objects in a linked list? Obviously, we want to achieve such an effect: For example, we store the parameters of triangles, straight lines, circular graphics in a linked list. We hope to use the Draw () method for a node, redraw this graphic; use GET () gets the various parameters of this graphic; use PUT () to modify the parameters of the graph. It can be seen that these different objects actually have the same behavior, just different ways.

The polymorphism of C can achieve our ideas. For this regard, please refer to the related C books (I see "C Programming Thought"). Please see the following example:

#ifndef shape_h

#define shape_h

Class Shape

{

PUBLIC:

Virtual void input () = 0;

Virtual void print () = 0;

Shape () {};

Virtual ~ shape () {};

}

#ENDIF

[Description] Define an abstract base class, there are two behaviors, input () is an input graphical parameter, which print () is a print graphic parameter. The province is only a simple explanation of problems.

#ifndef point_h

#define point_h

Class Point

{

PUBLIC:

Void Put ()

{

COUT << "X coordinates are:";

CIN >> X;

Cout << "Y coordinates are:";

CIN >> Y;

}

Void get ()

{

COUT << Endl << "X coordinate is:" << x;

COUT << Endl << "Y coordinate is:" << Y;

}

Virtual ~ Point () {}; private:

INT X;

Int Y;

}

#ENDIF

[Description] Class definition and implementation of points.

#ifndef circle_h

#define circle_h

#include "shape.h"

#include "point"

Class Circle: Public Shape: PUBLIC SHAPE

{

PUBLIC:

Void input ()

{

COUT << Endl << "Input Round Parameters";

Cout << Endl << "Enter the coordinate of the center point:" << Endl;

Center.put ();

COUT << Endl << "input radius:";

CIN >> RADIUS;

}

Void print ()

{

COUT << Endl << "The parameters of the circle are";

COUT << Endl << "Cotonym:" << Endl;

CENTER.GET ();

COUT << Endl << "radius:" << Radius;

}

Virtual ~ circle () {};

Private:

int RADIUS;

Point center;

}

#ENDIF

[Description] Class definition and implementation of the circle. Inherit the behavior of the Shape class.

#ifndef line_h

#define line_h

#include "shape.h"

#include "point"

Class Line: Public Shape: PUBLIC Shape

{

PUBLIC:

Void input ()

{

Cout << Endl << "Input Straight Parameters";

COUT << Endl << "Enter the coordinate of the endpoint 1:" << endl;

Point1.put ();

COUT << Endl << "Enter the coordinate of the endpoint 2:" << Endl;

Point2.put ();

}

Void print ()

{

COUT << Endl << "The parameters of the straight line";

COUT << Endl << "The coordinates of endpoint 1:";

Point1.get ();

COUT << Endl << "The coordinates of endpoint 2:";

Point2.get ();

}

Virtual ~ line () {};

Private:

Point point1;

Point point2;

}

#ENDIF

[Description] Definition and implementation of linear class. Inherit the behavior of Shape.

#ifndef listtest_h

#define listtest_h

#include

#include "list.h"

#include "circle.h"

#include "line.h"

Void ListTest_Mobject ()

{

List a;

Shape * p1 = new circle;

Shape * p2 = new line;

P1-> INPUT ();

P2-> INPUT ();

a.insert (p1);

a.insert (p2);

Shape * p = * a.next (); p-> print ();

Delete P;

A.PUT (NULL);

p = * a.next ();

P-> Print ();

Delete P;

A.PUT (NULL);

}

#ENDIF

[Description] This is a test function, and the use method is to join the #include "listtest.h" in the header containing the main (), then call listtest_mObject (). This is a simple example. It can be seen that deleting such a list of chain tables requires two steps, first delete Link table Node Data domain pointer to the object, and then delete the chain table node. Similarly, when destructure is such a linked list, you also need to pay attention to this problem. Otherwise, your program runs less memory (possibly not the case, it is said that the operating system can recover dynamic memory when the program is stopped, but the following conclusion is right), if it is a frequently called function, when running a period of time Your system is awkward. Therefore, it is best to derive a new chain table class using such a list to achieve the corresponding operation. For example, this:

Class ShapeList: Public List

{

PUBLIC:

Bool Sl_remove ()

{

Shape * p = * get ();

Delete P;

Return remove ();

}

}

[Gossip] I don't know if you are on such statements Shape * p = * a.next (); p-> print (); do not understand, I still feel a bit of Luo Wei. Then try this statement * a.next () -> print (); can pass through.

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

New Post(0)