ramennoodles
ramennoodles

Reputation: 138

Confusion on pointers C (Linked list)

I'm trying to swap the addresses of two adjacent nodes in a linked list. I tried swapping their values, using an int temp variable and it works perfectly fine. But now, I want to swap two addresses through pointers. Unfortunately, it created an infinite loop inside my while loop. Here's my code snippet:

Using int: //Worked perfectly fine

node* swapNumbers(node* head, int data){
    int temp;
    node *cursor = head;

    while(cursor!=NULL){
        if(cursor->data == data){
            temp = cursor->data;
            cursor->data = cursor->next->data;
            cursor->next->data = temp;
            //printf("1: %d\n", cursor->data);
            //printf("2: %d\n", cursor->next->data);

            return cursor;      
        } 
        cursor = cursor->next;
    }
    return NULL;
}

Using addresses: // This created an infinite loop!

node* swapNumbers(node* head, int data){
    node *temp = NULL;
    node *cursor = head;

    while(cursor!=NULL){
        if(cursor->data == data){
            temp = cursor;
            cursor = cursor->next;
            cursor->next = temp;
        return cursor;      
        } 
        cursor = cursor->next;
    }
    return NULL;
}

my typedef struct contains the following:

typedef struct node
{
    int data;
    struct node* next;
} node;

I'm new to C and pointers are still confusing me. Any help will be appreciated!

Upvotes: 5

Views: 772

Answers (3)

Mayur Navadia
Mayur Navadia

Reputation: 241

There are three case you have to handle in you code.

  1. If data node is First node. You have to change the head pointer. As you are passing only pointer you can not change else head is second element.

  2. If data node is last node. You can not exchange.

  3. If data node is middle node. You required previous of cursor, so you can point it to proper node. Assume if you have prev node

        if(cursor->data == data)
        {
            temp = cursor;
            cursor = cursor->next;
            if (NULL == cursor)
                return NULL;
            temp->next = cursor->next;
            prev->next = cursor;
            cursor->next = temp;
            return cursor;      
        } 
    

Upvotes: 0

beyrem
beyrem

Reputation: 436

For it not to go into an infinite loop, you need to save the predecessor value of cursor in a temporary value pointed by another pointer.

Upvotes: 1

Nobilis
Nobilis

Reputation: 7448

Can I suggest a simple approach to swapping two nodes in a singly linked list?

/* p points to the node before a, a and b are the nodes to be swapped */
void swap_address(node* p, node* a, node* b) 
{
    node* n = b->next; /* save the address of the node after b */

    if (!p || !a || !b)
    {
        printf("One of the arguments is NULL!\n");
        return;
    }

    p->next = b; /* p points to b */
    b->next = a; /* b points to a */
    a->next = n; /* a points to the node that was originally after b */
}

On my machine I tried it out with the following struct definition:

typedef struct node
{
    struct node* next;
    int val;
} node;

And I used it like this:

swap_address(b, c, d);

I had nodes head -> a -> b -> c -> d that had the values 1, 2, 3 and 4 in this order.

After the swap the order changed to 1 -> 2 -> 4 -> 3.

Is this what you are after?

Upvotes: 0

Related Questions