Thread: Lists, the first step. Create the 'head node'?

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    Lists, the first step. Create the 'head node'?

    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!
    Last edited by Swerve; 05-20-2009 at 07:50 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Usually the variable called "head" is not a node itself, just a pointer to whatever happens to be the first in line. If the list is empty it points to NULL. That becomes a special case in your insert function.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Thanks.

    By 'insert' you mean use push_back to add a null pointer to the list, yes?

    should this be hard-coded into the program where I wrote the //comments in main.cpp?

    I know it's simple, but I just don't seem to get it right.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You would never add a null pointer to the list under any circumstances. When you construct the list you should set a pointer to NULL, and this pointer will be NULL when and only when the list is empty. At any other time it will point to the front of the list.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  2. Can't figure out problem with code
    By Beast() in forum C Programming
    Replies: 4
    Last Post: 04-16-2005, 05:27 PM
  3. Big help in Astar search code...
    By alvifarooq in forum C++ Programming
    Replies: 6
    Last Post: 09-24-2004, 11:38 AM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM