Hi,
I am trying to store pointers to some inherited classes within a linked list, but am stuck with the first part.
I understand that each node contains two pointers to the next and previous, but how should I create the first 'head node' that points to the first object that will be added?
Here is my code:
header file :
Code:
class Vehicle
{
friend class listclass;
public:
Vehicle(int publicmpg = 0);
protected:
Vehicle *llink, *rlink;
int protectedmpg;
};
Vehicle::Vehicle(int publicmpg)
{
protectedmpg = publicmpg;
}
//-------------------------
//
//-------------------------
class Listclass
{
public:
//addvehicle()
Listclass(int publicfirst = 0);
protected:
int protectedfirst;
};
Listclass::Listclass(int publicfirst)
{
protectedfirst = publicfirst;
}
//-------------------------
//
//-------------------------
class SportsCar : public Vehicle
{
public:
SportsCar(int publicsixspeed = 0, int publicmpg = 0);
protected:
int protectedsixspeed;
};
SportsCar::SportsCar(int publicsixspeed, int publicmpg) : Vehicle(publicmpg)
{
protectedsixspeed = publicsixspeed;
}
main.cpp :
Code:
#include <iostream>
#include <list>
#include "Vehicle.h"
using namespace std;
int main()
{
list<Vehicle*> listname;
Listclass headnode;
// Should I create the 'head node' here before
// going on to creating actual objects?
int publicmpg = 11;
int publicsixspeed = 22;
SportsCar node(publicmpg, publicsixspeed);
SportsCar *ptrToSportsCar;
ptrToSportsCar = &node;
listname.push_back( ptrToSportsCar );
cout << "past push" << endl;
system ("PAUSE");
return 0;
How/should I create the 'head node' first, and then add the first items to the list?
I've read up and understand the 'head node' will only contain a pointer to the first node in the list, but how do I create it?.
I thought it might be easiest to create a single link list first, then try modify it into a doubly linked list.
Once I have the list established I think I'll be fine adding new items to the list, but I'm stuck 'getting it all setup' if you like.
Appreciate anyone's advice.
Thanks!