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

托尼

我试图将一个对象添加到我的链表中,问题是我不确定该对象的自身也就是带有子类的基类。汽车和小汽车,货车。

该程序的想法是一个车辆租赁系统,用户可以在其中添加车辆,并设置有关车辆的某些变量,例如:reg号,发动机尺寸等,而且如果车辆是厢式货车还是轿车,则可以进一步设置取决于它是轿车还是货车的变量。

例如,如果我想添加一辆福特面包车,我将输入详细信息,例如发动机尺寸,型号等,然后我将其指定为一辆面包车,从而允许我添加包含在面包车子类中的更多详细信息。

如果有谁能帮助我将对象添加到链接列表中,使其符合条件,我将非常感激。

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

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;
}
}

链表

#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();
};

车辆

#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)

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在C#中将对象添加到列表

来自分类Dev

将对象添加到通用列表

来自分类Dev

将对象添加到列表

来自分类Dev

在一个类中将对象添加到链接列表的末尾

来自分类Dev

如何在Java中按字母顺序将对象添加到链接列表?

来自分类Dev

根据对象类型将对象添加到列表

来自分类Dev

将对象密钥添加到href URL链接的末尾?

来自分类Dev

在列表中将对象的字段添加到C#中的列表中

来自分类常见问题

将对象字段添加到列表的最佳方法

来自分类Dev

OOP解析并将对象添加到列表

来自分类Dev

使用GUI将对象添加到数组列表

来自分类Dev

将对象添加到循环列表的末尾

来自分类Dev

在创建时或创建后将对象添加到列表

来自分类Dev

将对象添加到带有for循环的列表中?

来自分类Dev

将对象添加到列表时发生重载错误

来自分类Dev

可能的逻辑问题将对象添加到数组列表

来自分类Dev

无法将对象添加到我的列表中

来自分类Dev

将对象添加到Java列表中

来自分类Dev

在初始化时将对象添加到列表

来自分类Dev

如何使用 setState 将对象添加到数组列表

来自分类Dev

使用函数将对象添加到列表中

来自分类Dev

添加到链接列表C的后面

来自分类Dev

关于C#使用foreach循环将对象添加到列表

来自分类Dev

C#在将对象添加到列表时做一些事情

来自分类Dev

将对象数组添加到 C# 中的列表视图

来自分类Dev

C ++:将对象添加到当前对象

来自分类Dev

在Objective-C中将对象添加到NSMutableArray

来自分类Dev

C ++:将对象添加到集合中

来自分类Dev

C#:如何将对象添加到数组?