[英]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.