linked list from array problem

This is a discussion on linked list from array problem within the C++ Programming forums, part of the General Programming Boards category; I had to write a program that allows the user to type in information about real estate properties and stores ...

  1. #1
    dcs
    dcs is offline
    Registered User
    Join Date
    May 2004
    Posts
    3

    linked list from array problem

    I had to write a program that allows the user to type in information about real estate properties and stores each as a class object. It has a class function that can print all of them out in a formatted way. When I store them as an array of class objects I just increment the [i] and then when I print them out I go back through and index through and print them out. Well now I have to store them as a linked list of objects and I am having problems. Below is the constructor, everything works fine until I try to add a pointer that will link one object to the next (* theLink).

    RealEstate::RealEstate(string address, double TotalLivArea,
    double TotYardArea,bool IsCondo,bool rentable,
    RealEstate* theLink)
    :location(address),TotLivingArea(TotalLivArea), YardArea (TotYardArea),condo(IsCondo),rentable(rentable),li nk(theLink)


    When I stored the objects in an array I would get the input from the user and then do this:

    RealEstate property(locate,TotLiv,YardAre,condo,rentable);
    estate[i] = property;
    i++;

    Now with the linked list I am trying to do this:

    typedef RealEstate* EstatePtr;
    EstatePtr head;
    head = new RealEstate(locate,TotLiv,YardAre,condo,
    rentable,head);

    With an array I understand that if I increment [i] then the next item will be stored there but when I put a new RealEstate object at the head of the list how do I get the last one to point to the one before it and then how do I index through to print them out (like I do with the array)?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >how do I get the last one to point to the one before it
    Your constructor does the work for you:
    Code:
    #include <iostream>
    
    class node {
    public:
      node(int init, node *link);
      node *next_link();
      int   get_item();
    private:
      int   item;
      node *next;
    };
    
    node::node(
      int   init,
      node *link
      )
      : item(init)
      , next(link)
    {
      // No body
    }
    
    node *
    node::next_link()
    {
      return next;
    }
    
    int
    node::get_item()
    {
      return item;
    }
    
    int
    main()
    {
      node *head = 0;
    
      for (int i = 0; i < 10; i++) {
        head = new node(i, head);
      }
      for (node *it = head; it; it = it->next_link()) {
        std::cout<< it->get_item() <<std::endl;
      }
    }
    >how do I index through to print them out (like I do with the array)?
    See the last loop of the above code. That's the usual idiom for traversing a singly linked list.
    My best code is written with the delete key.

  3. #3
    dcs
    dcs is offline
    Registered User
    Join Date
    May 2004
    Posts
    3
    Thank you for the help, I have one question. I apologize if this sounds stupid but the node class is seperate from my RealEstate class correct? So I would create a new RealEstate object and then create a new node object? Such as:

    RealEstate property(locate,TotLiv,YardAre,condo,rentable);

    then:

    head = new node(property, head);

    but node is expecting an (int, pointer) so that wouldn't work. Am I totally missing the way this works?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >the node class is seperate from my RealEstate class correct?
    That's one way to do it. You could also add the necessary members and member functions to the RealEstate class, but that might not be as intuitive as a linked list of RealEstate objects.

    >but node is expecting an (int, pointer) so that wouldn't work.
    Well, naturally you would need to modify the code I showed you (as an example) to your needs.
    Code:
    class node {
    public:
      node(int init, node *link);
      node       *next_link();
      RealEstate  get_item();
    private:
      RealEstate  item;
      node       *next;
    };
    My best code is written with the delete key.

  5. #5
    dcs
    dcs is offline
    Registered User
    Join Date
    May 2004
    Posts
    3
    I think that I have all of the code switched over to the node/ linked list but I have one last question. I changed get_item() to return a RealEstate object (item) and I have a member function for RealEstate that handles formatting the output of the object (display) but I am not sure how to get the object from get_item. I get a "forming a pointer to member requires explicit use of address of operator and a qualified name" error when I compile this.



    void DisplayAllRealEstate()
    {
    RealEstate i ();

    for (node *it = head; it; it = it->next_link()) {
    i = it->get_item;
    i.display();
    }
    return;
    }

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    It would be nice to know which line the error message refers to, but if it isn't a typo, there should be () after it->getItem since it's a function and not a variable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. I need help on this particular Linked List problem
    By sangken in forum C Programming
    Replies: 11
    Last Post: 08-06-2006, 12:26 AM
  3. Linked list problem
    By mr_glass in forum C Programming
    Replies: 4
    Last Post: 03-07-2006, 01:12 AM
  4. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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