explain linked lists returning a node address

This is a discussion on explain linked lists returning a node address within the C Programming forums, part of the General Programming Boards category; with following function prototype and structure of linked list, how do i write function to return an address/? I think ...

  1. #1
    Unregistered
    Guest

    explain linked lists returning a node address

    with following function prototype and structure of linked list, how do i write function to return an address/? I think what I got is wrong. please help

    prototype:

    struct node *search(struct node *start,int n);

    structure:

    struct node { int value; struct node * next;};

    Code:
    struct node *search(struct node *start,int n)
    {
    
       if ( n = node->value)  
        return node;
       else 
         return (\0);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Nope, that's correct. If you want to return a pointer to a structure, you just do:

    return node;

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Unregistered
    Guest
    i should be explicit in what the function needs to do:

    • if value n is found in some node, return address of this node
    • if value n is not found, return null pointer


    so is what i have okay??

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Right. That's what you want. That's what you've got.
    Code:
    Node *myFunc( Node* searchHere, int findThis )
    {
        Node *n;
        for( n = searchHere; n; n = n->next )
            if( n->value == findThis )
                return n;
        return NULL;
    }
    That's exactly what you have. You're doing it right.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 02:48 PM
  2. Help here with qsort!
    By xxxrugby in forum C Programming
    Replies: 11
    Last Post: 02-09-2005, 08:52 AM
  3. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 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