将对象添加到链接列表c++

Adding object to a linked list c++

本文关键字:列表 c++ 链接 对象 添加      更新时间:2023-10-16

我正试图将一个对象添加到我的链表中,问题是我不确定该对象本身是如何成为一个带有子类的基类的。车辆和轿车、面包车。

该程序的理念是一个车辆租赁系统,用户可以将车辆添加到系统中,并设置有关车辆的某些变量,如:注册号发动机大小等,但如果车辆是面包车或汽车,则可以根据是汽车还是面包车设置其他变量。

例如,如果我想添加一辆福特面包车,我会输入发动机尺寸、型号等详细信息,然后我会指定这是一辆面包车,这样我就可以添加面包车子类中包含的更多详细信息。

如果你们中的任何人能帮助我将一个对象添加到链接列表中,使其符合标准,我将非常感谢。

我的代码插入一个新节点。

void linkedList::InsertNode(int regNo,double engine,string model,vehicle_type make)
{
Vehicle<int> vehicle(int,double,string,vehicle_type);
//create new node
nodePtr n=new node;
//make next point to null
n->next=NULL;
n->data=vehicle(regNo,engine,model,make);
//if we have a list set current pointer
if(head!=NULL)
{
current=head;
//checks if end
while(current->next!=NULL)
{
current=current->next;
}
//connect last node to new node
current->next=n;
}
else
{
//new node is front of list if empty
head=n;
}
}

链接列表.h

#pragma once
#include <string>
using namespace std;

class linkedList
{
template<class regNo>;
friend class Vehicle<int>;
typedef enum{ Car,Van} vehicle_type;
private:
typedef struct node                                                
{                                                               
int data;               
node* next;             
}*nodePtr;
nodePtr head;
nodePtr current;
nodePtr temp;
public:
linkedList();
void InsertNode(int regNo ,double engine,string model,vehicle_type make);
void SearchNode(int);
void PrintList();
void DeleteNode(int delData);
~linkedList();
};

车辆.h

#pragma once
template<class regNo=int>
class Vehicle
{
public:
typedef enum{ Car,Van} vehicle_type;
protected:
vehicle_type make;
string model;
int regNo;
double engineSize;
bool rented;
public:
Vehicle(vehicle_type  make):model(""),regNo(reg),engineSize(engine),make(make),rented(false){};
char getMakeModel();
int getRegNo();
int getEngineSize();
bool getRented();
bool setRented(bool rented);
~Vehicle();
void listVehicle();
void addVehicle();
void removeVehicle(int delData);
void bookVehicle();
void displayDetails(int regNo);
void listCarsNotRented();
void listFiveDoor();
void listFordVansRented();
};

为了澄清,链接列表将包含许多对象的列表,这些对象包含有关车辆的信息,我需要做的是将车辆(对象)添加到列表中。

当尝试将节点中的数据设置为我想要的对象时,会发生错误。

首先,我不知道这将如何工作:

Vehicle<int> vehicle(int,double,string,vehicle_type);

我想你是想打这个:

Vehicle<int> vehicle(regNo,engine,model,vehicle_type);

如果你没有关于链表的任务或东西,你必须自己实现链表,我建议你使用Boost链表,而不是实现你自己的:链接

对于实际问题本身:

如果你想知道你要添加到链表中的对象是什么类型的,只需重写它们的方法来支持子类。因此,不要使用基元(int、double等)添加到链表中,而是使用Vehicle对象或Van对象来添加到列表中。我不知道代码,但它是这样的:

void linkedList::InsertNode(Van *pVan)
{
Van newVan(*pVan); // You need a copy constructor for this
// Set Van specific attributes like this:
newVan->setEngineSize (123);
//create new node
nodePtr n = new node;
//make next point to null
n->next=NULL;
n->data= newVan; // You need a copy constructor for this
//if we have a list set current pointer
if(head!=NULL)
{
current=head;
//checks if end
while(current->next!=NULL)
{
current=current->next;
}
//connect last node to new node
current->next=n;
} else
{
//new node is front of list if empty
head=n;
}
}
void linkedList::InsertNode(Car *pCar)
{
Car newCar(*pCar); // You need a copy constructor for this
// Set Car specific attributes like this:
newCar->setEngineSize (123);
//create new node
nodePtr n = new node;
//make next point to null
n->next=NULL;
n->data= newCar; // You need a copy constructor for this
//if we have a list set current pointer
if(head!=NULL)
{
current=head;
//checks if end
while(current->next!=NULL)
{
current=current->next;
}
//connect last node to new node
current->next=n;
} else
{
//new node is front of list if empty
head=n;
}
}

因此,重写方法以支持不同的类。当你编译代码时,它会自动为你调用正确的方法。例如,如果您有一个Van并调用insertNode函数来传递新Van对象的引用,它不会调用void linkedList::InsertNode(Vehicle *pVehicle),而是会调用void linkedList::InsertNode(Van *pVan)

我的建议:-制作一个指向列表末尾的"尾部"指针,这样每次添加对象时就不会经过while循环。-在实现任何其他容器时,请对数据类型使用"模板"。