Struct Communication and Linked Lists

This is a discussion on Struct Communication and Linked Lists within the C++ Programming forums, part of the General Programming Boards category; I suppose this question could also be placed within the C forum, but here goes. Say I have a linked ...

  1. #1
    Drunken Progammer CaptainMorgan's Avatar
    Join Date
    Feb 2006
    Location
    On The Rocks
    Posts
    45

    Struct Communication and Linked Lists

    I suppose this question could also be placed within the C forum, but here goes. Say I have a linked list for Cars that I have in a used car lot, how do I link another linked list containing past Buyers of the Car accordingly? Within the Buyer's list is a member called Name and I want this same Name to be found in the Cars list so I can link the two lists together so that when I pull up information regarding a Car, the past Buyers will be listed, or if I pull up a Buyer, the Cars owned will be shown.

    Not looking for a complete answer, more like a conceptual framework. I've constructed independent lists in the past with no problem, but linking the two together so as to create a database simulation is something Im not used to. Am I overlooking something small? ie - basic linked list concepts? or is there something else?

    Thank you.
    -Captain
    Last edited by CaptainMorgan; 09-10-2006 at 06:47 AM.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,383
    It is presumable to assume that two diferent persons have the same name, so you may want to connect the the two lists through an ID instead. The same goes for cars, if you want each instance of your Cars class to define not a specific brand/model, but an actual car.

    Regardless, to answer you question directly, one possible way is to create another linked list that is responsible for displaying/managing the relationships.

    Code:
    struct CarBuyers {
       Cars* vehicle;
       Buyers* owner;
       CarBuyers* next;
    }
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    Whilst lists seem good for specific things, they make certain other things a lot harder.
    Say, find all the owners of red cars.
    Using lists for one particular kind of query is just premature optimisation.

    A list would have a problem with this scenario
    A person buys a car, sells it, then buys it back again at some later time. Rare to be sure, but not impossible.

    http://en.wikipedia.org/wiki/Entity-relationship_model
    Say for example,
    "Car"
    + car registration
    - colour
    - engine size
    - current owner

    "Owner"
    - name
    - address
    + drivers licence number
    + car registration

    "Transaction"
    - date
    + car registration
    - seller
    - buyer
    - price

    The + indicates the key field of each entity. Within a given set of entities, this field is unique.
    The owner has two key fields to allow owners to own more than one car.

    Each entity is just a struct (or class), and the whole database is just three arrays of those structs.

    Draw out the diagram, and annotate the relationships with things like
    Code:
          owns:n             owned by:1
    car <------------------------------> owner
    A car is owned by one owner, but a single owner can own many cars.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    すまん Hikaru's Avatar
    Join Date
    Aug 2006
    Posts
    46
    Maybe a vector of pointers into the other list?
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    namespace Hikaru
    {
        using std::vector;
        using std::string;
    
        class Buyer;
    
        class Car
        {
        public:
            Car(const string& name): name_(name) {}
            void addBuyer(Buyer* buyer) { history_.push_back(buyer); }
            const vector<Buyer*>& history() { return history_; }
            const string& name() { return name_; }
        private:
            string name_;
            vector<Buyer*> history_;
        };
    
        class Buyer
        {
        public:
            Buyer(const string& name): name_(name) {}
            void addCar(Car* car) { history_.push_back(car); }
            const vector<Car*>& history() { return history_; }
            const string& name() { return name_; }
        private:
            string name_;
            vector<Car*> history_;
        };
    
        void setRelation(Car& car, Buyer& buyer)
        {
            car.addBuyer(&buyer);
            buyer.addCar(&car);
        }
    
        void showRelation(std::ostream& os, Car& car)
        {
            vector<Buyer*>::const_iterator iter = car.history().begin();
            vector<Buyer*>::const_iterator end = car.history().end();
    
            os << car.name() << "> ";
    
            while (iter != end)
            {
                os << (*iter)->name() << ' ';
                ++iter;
            }
    
            os << '\n';
        }
    
        void showRelation(std::ostream& os, Buyer& buyer)
        {
            vector<Car*>::const_iterator iter = buyer.history().begin();
            vector<Car*>::const_iterator end = buyer.history().end();
    
            os << buyer.name() << "> ";
    
            while (iter != end)
            {
                os << (*iter)->name() << ' ';
                ++iter;
            }
    
            os << '\n';
        }
    }
    
    int main()
    {
        using std::cout;
        using Hikaru::Car;
        using Hikaru::Buyer;
    
        std::vector<Car> cars;
        std::vector<Buyer> buyers;
    
        cars.push_back(Car("Car 1"));
        cars.push_back(Car("Car 2"));
        cars.push_back(Car("Car 3"));
        buyers.push_back(Buyer("Buyer 1"));
        buyers.push_back(Buyer("Buyer 2"));
        buyers.push_back(Buyer("Buyer 2"));
    
        // set up relationships
        setRelation(cars[0], buyers[2]);
        setRelation(cars[1], buyers[0]);
        setRelation(cars[2], buyers[1]);
        setRelation(cars[1], buyers[2]);
    
        // show relationships
        showRelation(cout, cars[0]);
        showRelation(cout, buyers[1]);
        showRelation(cout, cars[1]);
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

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