How to find a node in link list 
Author Message
 How to find a node in link list

Hello,

I'm using C compiler in unix. We have created a linked list and know
that one of the node does not point to next node but point to one of
the previous node. How can I find this node?

Marcus



Sun, 21 Oct 2001 03:00:00 GMT  
 How to find a node in link list
: Hello,

: I'm using C compiler in unix. We have created a linked list and know
: that one of the node does not point to next node but point to one of
: the previous node. How can I find this node?

Walk the list printing out the nodes contents?  I've missed something
somewhere, I think.

Will



Sun, 21 Oct 2001 03:00:00 GMT  
 How to find a node in link list

Quote:

> I'm using C compiler in unix. We have created a linked list and know
> that one of the node does not point to next node but point to one of
> the previous node. How can I find this node?

For example:

typedef struct node {
        struct node *next;
        /* whatever other data */

Quote:
} NODE, *PNODE;

PNODE   Head;   /* assuming this is filled before call to FindNode */

PNODE   FindNode(void)
{
        PNODE   p, q;

        for (p = Head, q = NULL; p; p = p->next) {
                if (p->next == q)
                        return p;
                q = p;
        }
        return NULL;

Quote:
}

        AriL
--
Humans may send email (if absolutely necessary) to the
obvious non-spam address.


Sun, 21 Oct 2001 03:00:00 GMT  
 How to find a node in link list

Quote:

> I'm using C compiler in unix. We have created a linked list and know
> that one of the node does not point to next node but point to one of
> the previous node. How can I find this node?

Marcus...

"Walk" the list and stop at each node. Pick up the pointer to the next
node. Look back over the portion of the list you've traveled so far and
see if the pointer you're holding is to any of those nodes (or to the
current node). If not, go to the next node and repeat.

If you have difficulty making the code work, post it here with
description of problem.

Morris Dovey
West Des Moines, Iowa USA



Sun, 21 Oct 2001 03:00:00 GMT  
 How to find a node in link list

Quote:

> Hello,

> I'm using C compiler in unix. We have created a linked list and know
> that one of the node does not point to next node but point to one of
> the previous node. How can I find this node?

If the nodes can store a 'mark field', the solution is (almost) trivial:

        node_t* find_node(node_t* head) {

                for (; head; head= head->next) {

                        head->mark= 1;

                        if (head->next && head->next.mark)
                                break;
                }

                return head;
        }

If there's no such thing as a 'mark field', things get a bit more
complicated,
but not all is lost -- the following function traverses a list (with a
loop
somewhere) using _two_ pointers p and q. Pointer p hops from an element
to the next element, while pointer q hops two elements at the time; when
the two pointers compare equal again, pointer p has traversed the loopy
part of the list once. While hopping the list every value of p's next
value
is checked agains a value r. Here it is:

        node_t* hop_list(node_t* r) {

                node_t* p= r;
                node_t* q= r;

                do {
                        if (p->next == r)
                                return p;

                        p= p->next;
                        q= q->next->next;
                }
                while (p != q);

                return NULL;
        }

The function above can be used to find the back-pointing element:

        node_t* find_back(node_t* head) {

                for (; head; head= head->next) {

                        if (hop_list(head) == head)
                                return head;
                }

                return NULL;
        }

kind regards,




Mon, 22 Oct 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. nodes linked listed and such

2. 2 nodes end up in a cycle in a single linked list

3. newbie question: removal of node in linked list

4. Realloc()ing data of a node from a linked list

5. why malloc for a new node in linked list

6. Destroying All Nodes in a Linked List

7. Destroying All Nodes is a Linked List

8. Removing node from linked list

9. Removing a NODE for good (double linked lists)

10. deleting a previous node in a singly linked list

11. Question about swapping nodes in a doubly linked list

12. Deleting a node in a linked list

 

 
Powered by phpBB® Forum Software