Thread: Linked lists beginner

  1. #1
    NightOwl
    Guest

    Linked lists beginner

    I created a struct of List which included a string, double, and List *next. I have a function that looks for the mostExpensive and I created another pointer in my function to point to that node. So I find it, now I am confused how to return it to main. I know I need to return a pointer to the node...but am not sure what that means. On the last line of the function I return ...most. So...the function isn't void, but what do I put before the name of the function in the header? List *most.... *most.... ???

    Very confused. Have only written a couple functions so far, but they are just sorting or printing, so they are void.

    Plez respond, I have to finish this tonight!!! NightOwl

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    "Very confused. Have only written a couple functions so far, but they are just sorting or printing, so they are void."

    You're not even close so forget about it. Get some sleep and take your F. You need to write some simple functions first, and learn about passing by value and passing by reference, and how return types work the same way.

    In case you decide to continue:

    1)A linked list doesn't have a data member pointing to another list--what good would that do? A List class is going to have data members which are pointers to what are commonly called Nodes, and the Nodes are what actually contain the data in the List.

    2)You can't decide what type most is in the return statement. most has a type, and that is the only type you can return.
    Last edited by 7stud; 05-01-2003 at 11:35 PM.

  3. #3
    NightOwl
    Guest
    I may say it wrong..but I do understand about the nodes, maybe I just explained it wrong. most is a poiner, but only in the function, not in main. I want to return both the string and double it points to, but not sure, since the only pointer I take in the function is ... head. Do I need to declare the pointer in main also?... I don't give up easy....

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    If List is the name of your struct, and most is a pointer to the node you're looking for, it sounds like you're on the right track.


    Then the function prototype will look something like this
    Code:
    List *nameofSearchFunction (parameterList);
    and the end of the function will be
    Code:
    ...
    return most;
    }
    and in your main program after the List definition you will declare a List pointer and use it in the function call:

    Code:
    struct List {
    ...
    };
    
    int main()
    {
         List *ptr;
         ... // other code, blah blah blah
         ptr = nameofSearchFunction(parameters);
         ... //  blah blah blah
    }

  5. #5
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Originally posted by 7stud
    "Very confused. Have only written a couple functions so far, but they are just sorting or printing, so they are void."

    You're not even close so forget about it. Get some sleep and take your F. You need to write some simple functions first, and learn about passing by value and passing by reference, and how return types work the same way.

    In case you decide to continue:

    1)A linked list doesn't have a data member pointing to another list--what good would that do? A List class is going to have data members which are pointers to what are commonly called Nodes, and the Nodes are what actually contain the data in the List.

    2)You can't decide what type most is in the return statement. most has a type, and that is the only type you can return.
    I do not agree with you on point number 1. There are some problems which requires nodes to point to another list.. Like an Index.. Say you have 100's of lists.. 1 list has all the records from 1 yo 100 the next 101 to 200 and so on.. So you can use another list to index this list... Something like an index.. though trees can be used here.. Nodes pointing to lists do provide an advantage in some situations...

    In programming the data structures are not used for one situations.. they have multiple uses and new inovative ones when the need arrises...it all depends on you style of coding and solving problems...

  6. #6
    NightOwl
    Guest
    Actually... this is some of the code:
    struct List
    {
    string name;
    double price;
    List *next;
    };

    in main...

    int main()
    {
    double price;
    List *head = NULL;
    head = createList();
    if (head = NULL)
    cout <<"ERROR MSG" << endl;
    else
    printList();

    //(now I want to output the most expensive from my function


    return 0;
    }

    //other functions

    Toy * mostExpensive(Toy *head)
    {
    Toy *temp = head;
    Toy *most = head;
    while(temp != NULL)
    {
    if(temp-> price > most-> price)
    most = temp;
    temp = temp->next;
    }
    return most;
    }



    So something is not right....I can trace my function but I can't get it back into main....
    (Thanx for all the help!!!)

  7. #7
    NightOwl
    Guest
    The above code...but List instead of Toy in the function...lol

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    "I do not agree with you on point number 1."

    It was a context sensitive answer and not a general statement, so as not to muddy the waters.
    Last edited by 7stud; 05-02-2003 at 01:32 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  2. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  3. Linked Lists Integer addition ? HELP Please??
    By green_eel in forum C Programming
    Replies: 3
    Last Post: 03-12-2003, 04:36 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM