Home > Net >  how to get the LinkedList address from other void functions
how to get the LinkedList address from other void functions

Time:04-14

  1. I want to reverse the linked list for giving head pointer argument to reverse2 function.

  2. I expect that the reverse2 function reverse the linkedlist and substitute the address to head pointer


    //linked list//
    
    typedef struct node* lsnode;
    typedef struct node 
    {
        int data;
        lsnode link;
    }node;
    
    
    //create the 3 nodes
    
    lsnode create3() 
    {
        lsnode first, second, last;
     
        first = (lsnode)malloc(sizeof(node));
        second = (lsnode)malloc(sizeof(node));
        last = (lsnode)malloc(sizeof(node));
        first->data = 30;
        first->link = second;
        second->data = 20;
        second->link = last;
        last->data = 10;
        last->link = NULL;
    
        return first;
    }
    
    //reverse the linkedlist
    void reverse2(lsnode head)
    {
        lsnode q,p,r;
        p = head;
        q = NULL;
        r = NULL;
        while (p != NULL)
        {
            r = q;
            q = p;
            p = p->link;
            q->link = r;
        }
        head = p;
    }
    
    
    int main(void)
    {
        lsnode head = create3();
        reverse2(head);
        while (head)
        {
            printf("%d\n", head->data);
            head = head->link;
        }
        return 0;
    }

the program prints the only 30 but, I want to print 10 20 30 what's the problem with my code..

CodePudding user response:

Following some of the suggestions in comments, the following modifies the reverse2 function to pass the address of the object being changed, as opposed to the object itself, allowing the modified object to be referenced in the calling function upon its return. Regarding @Some programmer dude's suggestions on the value of visualization, I found this to be helpful.
Also, to make the code easier to follow, this code expands the single letter variable names to symbols that can be more easily understood by anyone reading the code.

typedef struct node* lsnode;
typedef struct node 
{
    int data;
    lsnode next;//changed from link
}node;


//create the 3 nodes

lsnode create3() 
{
    lsnode first, second, last;
 
    first = (lsnode)malloc(sizeof(node));
    second = (lsnode)malloc(sizeof(node));
    last = (lsnode)malloc(sizeof(node));
    first->data = 30;
    first->next = second;
    second->data = 20;
    second->next = last;
    last->data = 10;
    last->next = NULL;

    return first;
}

//reverse the linkedlist
void reverse2(lsnode *head)//changed prototype, 
{
    //lsnode q,p,r;
    lsnode prev,current,next;//note these changes throughout
    current = *head;//note use of asterisk to reference 
                    //object being changed 
    prev = NULL;
    next = NULL;
    while (current != NULL)
    {
        next = current->next;
        current->next = prev;
        prev = current;
        current = next;
    }
    *head = prev;
}


int main(void)
{
    lsnode head = create3();
    reverse2(&head);//passes address of object to be modified
    while (head)
    {
        printf("%d\n", head->data);
        head = head->next;
    }
    return 0;
}

Outputs:

 10
 20
 30
  • Related