Doubly Linked Lists

This is a discussion on Doubly Linked Lists within the C++ Programming forums, part of the General Programming Boards category; Hi, I am trying to create a doubly linked list and was hoping for some advice on if my classes ...

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

    Doubly Linked Lists

    Hi,

    I am trying to create a doubly linked list and was hoping for some advice on if my classes are correctly declared.

    Code:
    class Car
    {
    public:
    	AddaNewDieselHardTop();//All functions for adding new nodes declared here in Car. This is OK because they are Friend Classes?
            AddaNewDiesleSoftTop();
    private:
    	CarNode *head;
    };
    
    ////////////////////////////////
    
    class Diesel
    {
    	friend class Car;
    };
    
    ///////////////////////////////
    
    
    class DieselHardTop
    {
    
    	friend class Diesel;
    	friend class Car;
    private:
    	int MPG;
    	DieselHardTop *llink, *rlink;
    };
    
    class DieselSoftTop
    {
    	friend class Diesel;
    	friend class Car;
    private:
    	int MPG;
    	DieselSoftTop *llink, *rlink;
    };
    My classes are '3 deep' as shown in the attached image. I want to hold all the objects from all the classes within a single doubly linked list.

    The user will be able to add new instances of DieselHardTop and DieselSoftTop, and I'm wondering if I can declare the 'AddNewDieselHardTop()' and 'AddNewDieselSoftTop' functions just once within the Car class declaration, or if I would need to declare these functions in their own respective classes.

    The user will select 'Add a new car' from a menu, and I was going to use a Switch statement, so if the user chose to 'Add a new DieselSoftTop', I would then call that function, which if correct, would be declared within Car, and not the DieselSoftTop class.

    Hope that makes sense!

    Many thanks!!!
    Attached Images Attached Images  
    Last edited by Swerve; 03-23-2009 at 10:39 AM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This design doesn't seem to make much sense. Base classes shouldn't know about classes derived from them. Also there doesn't seem any reason in C++ to implement your own doubly-linked list.

    Without all in-house linked list implementation non-sense:

    Code:
    #include <iostream>
    #include <list>
    #include <algorithm>
    #include <functional>
    
    class Car
    {
    public:
        virtual ~Car() {}
        virtual void print() const = 0;
    };
    
    class Diesel: public Car
    {
    };
    
    class Petrol: public Car
    {
    public:
        void print() const { std::cout << "Petrol\n"; }
    };
    
    class DieselHardTop: public Diesel
    {
    public:
        void print() const { std::cout << "DieselHardTop\n"; }
    };
    
    class DieselSoftTop: public Diesel
    {
    public:
        void print() const { std::cout << "DieselSoftTop\n"; }
    };
    
    template <class T>
    void Delete(const T* t)
    {
        delete t;
    }
    
    int main()
    {
        enum { petrol = 1, diesel_hardtop, diesel_softtop, max_car_type };
        std::list<Car*> cars;
        int choice;
        while (std::cout << "Enter " << petrol << " for Petrol, " << diesel_hardtop << " for DieselHardTop, "
            << diesel_softtop << " for DieselSoftTop or other to quit: "
            && std::cin >> choice && choice > 0 && choice < max_car_type) {
            switch (choice) {
                case petrol:
                    cars.push_back(new Petrol);
                    break;
                case diesel_hardtop:
                    cars.push_back(new DieselHardTop);
                    break;
                case diesel_softtop:
                    cars.push_back(new DieselSoftTop);
                    break;
            }
        }
        std::for_each(cars.begin(), cars.end(), std::mem_fun(&Car::print));
        std::for_each(cars.begin(), cars.end(), Delete<Car>);
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Many thanks anon!

    That has thrown a 'spanner in the works' ! hehe.

    For the last couple of days I've been reading about linked lists and vectors etc and the examples in the books have shown them this way.

    I was planning declaring the different functions within the base class (add a car, delete a car etc) as virtual functions, and then specify the different implementations of the functions within the seperate inherited classes. This would give me a single interface from where I could add all the objects into the linked list.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Re: the design, I'd use composition instead of inheritance. A "car has-a soft top" makes more sense to me than a "car is-a soft top".

  5. #5
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Many thanks medievalelks

    I had never heard of composition and it isn't something we've done in class yet either, but the reading on it I've just done suggests both ways would work, but perhaps composition is the better option, but for now I feel I will just stick with inheritance until I have a better understanding of the differences.

    again, many thanks



    EDIT - If I declare a virtual function inside of the Diesel class, and call it from within the DieselHardTop class, would the base class be the Diesel class? even though Diesel class is inherited from the Car class?

    Or put another way, is the base class of a virtual function always where the function is first declared, even if the class inwhich it is declared in may be an inherited class it'self?
    Last edited by Swerve; 03-23-2009 at 01:14 PM.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    DieselHardTop is both a Diesel and a Car, but if you define new virtual functions at the Diesel level, they can't be called via the Car interface (without downcasting).

    i.e., you can't do this:

    Code:
    Car * car = new DieselHardTop;
    
    car->DieselOnlyMemberFunction();
    But of course, you can do this:

    Code:
    Diesel * car = new DieselHardTop;
    
    car->DieselOnlyMemberFunction();

  7. #7
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Thanks again medievalelks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doubly linked lists
    By mohanlon in forum C Programming
    Replies: 8
    Last Post: 12-08-2010, 01:01 AM
  2. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 05:32 PM
  3. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 09:57 PM
  4. doubly linked lists
    By cworld in forum C++ Programming
    Replies: 2
    Last Post: 04-21-2002, 10:33 AM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 07:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21