繁体   English   中英

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

[英]Adding object to a linked list 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并调用传递新Van对象的引用的insertNode函数,则它将不会调用void linkedList::InsertNode(Vehicle *pVehicle)但会调用void linkedList::InsertNode(Van *pVan)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM