CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Threaded View

  1. #1
    Join Date
    Dec 2002
    Location
    iasi, romania
    Posts
    7

    Redirect a simple linked list

    Hello all.

    I've just succeeded in creating a recursive function which
    redirects a simple linked list ... but ...

    I'm wondering if there exists a better recursive function to redirect a simple linked list ... (i still believe that the answer is NO)

    Example:
    If we have the list:
    Node1->Node2->Node3
    The redirected list is:
    Node3->Node2->Node1

    My very simple program is:

    #include <stdio.h>

    typedef struct tagNODE
    {
    int info;
    tagNODE* next;
    } NODE;

    void Listing(NODE* node)
    {
    while (node)
    {
    printf("%d\n", node->info);
    node = node->next;
    }
    }

    NODE* Redirect(NODE* parent, NODE* node)
    {
    if (node->next == NULL)
    {
    node->next = parent;
    return node;
    }
    else
    {
    NODE* p = Redirect(node, node->next);
    node->next = parent;
    return p;
    }
    }

    void main(void)
    {
    NODE *node1 = new NODE, *node2 = new NODE;
    NODE *node3 = new NODE, *node4 = new NODE;

    node1->info = 1;
    node2->info = 2;
    node3->info = 3;
    node4->info = 4;
    node1->next = node2;
    node2->next = node3;
    node3->next = node4;
    node4->next = NULL;

    Listing(Redirect(NULL, node1));
    }

    Thanks a lot...

    Dragos
    Last edited by dragost; December 29th, 2002 at 07:13 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured