将对象添加到链接列表c++
Adding object to a linked list c++
我正试图将一个对象添加到我的链表中,问题是我不确定该对象本身是如何成为一个带有子类的基类的。车辆和轿车、面包车。
该程序的理念是一个车辆租赁系统,用户可以将车辆添加到系统中,并设置有关车辆的某些变量,如:注册号发动机大小等,但如果车辆是面包车或汽车,则可以根据是汽车还是面包车设置其他变量。
例如,如果我想添加一辆福特面包车,我会输入发动机尺寸、型号等详细信息,然后我会指定这是一辆面包车,这样我就可以添加面包车子类中包含的更多详细信息。
如果你们中的任何人能帮助我将一个对象添加到链接列表中,使其符合标准,我将非常感谢。
我的代码插入一个新节点。
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循环。-在实现任何其他容器时,请对数据类型使用"模板"。
- Pybind11:将元组列表从Python传递到C++
- 从链接列表c++中删除一个项目
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- C++如何通过用户输入删除列表元素
- 读取文件的最后一行并输入到链接列表时出错
- 复制列表初始化的隐式转换的等级是多少
- LNK2038、MSVS2017 MAGMA的原因列表
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 没有为自己的结构调用列表推回方法
- 使用简单类型列表实现的指数编译时间.为什么
- 一对向量构造函数:初始值设定项列表与显式构造
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- 通过for循环使用用户输入填充列表
- C++:如何使函数只返回作为列表一部分的字符串
- 概念中的cv限定符需要表达式参数列表
- 下面是我为检测链接列表中的循环而制作的代码
- 建议在运行时将带有类实例的列表从c++导入qml
- 如何维护资源管理器项目视图中当前可见的项目列表
- 在卡萨布兰卡形成编码参数的列表
- 在没有参数列表的情况下使用模板名称"Event"无效,模板问题