Function Arguement is Part of a Linked List - > How to access element in linked list?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CodeTilYaDrop
    New Member
    • Aug 2007
    • 66

    Function Arguement is Part of a Linked List - > How to access element in linked list?

    Hello,

    I can not figure this code out! I have a function that needs to access an element of the the linked list as an argument. Could someone give me an example of the syntax? Do you have any examples of using an element as the arguement in a linked list in a function???

    Here is a simplified verison of what I am trying to do:

    struct ListNode
    {
    int value;
    struct ListNode *next
    }

    main {
    ListNode L;
    // menu here to let user select
    char choice;
    if (choice == 'a') {
    cin choice;
    L.average(value ); // How do you get the value out of the Linked List into the arguement of the function????

    I need to know how to work the value into the function???? - ty --CTYD
  • fual
    New Member
    • Feb 2008
    • 28

    #2
    [CODE=cpp]struct ListNode
    {
    int value;
    ListNode *next
    }

    main {
    ListNode L;

    // menu here to let user select
    char choice;
    if (choice == 'a') {
    cin choice;
    L.average(value ); // How do you get the value out of the Linked List into the arguement of the function????
    }[/CODE]
    "average" is something that you want to do TO the whole list, it is not a property OF a single ListNode. What you want to do is something like this (psudeo code)

    [CODE=cpp]#include <algorithm>

    struct __average
    {
    average( ) : items(0), sum(0) { }
    void operator( )( const int& value ){ /* code */ }
    operator double ( ) const { /* code */ }
    int items;
    int sum;
    };

    double average( const linked_list& __ll ) {
    return double( std::for_each( __ll.begin(), __ll.end(), __average( ) ) );
    }[/CODE]Now average will compute the average of linked list (which is presumably what you want to do). What you need is a linked list object that wraps your ListNodes up so they are easy to use (ie something that provides an iterator pointing to the beginning and end).

    Comment

    • CodeTilYaDrop
      New Member
      • Aug 2007
      • 66

      #3
      This is not exactly what I was looking for. I found another way to solve this problem, and I am coding it now. It is probably not the best way though. I will try to put a better example of what I am looking for a little later today when I get more time. This was not a good example. I know there is a better way to do this than what I got now though. ty- CTYD

      Comment

      Working...