top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to swap adjacent element of a single linked list?

0 votes
416 views
How to swap adjacent element of a single linked list?
posted Jul 7, 2017 by Ajay Kumar

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

You can use any standard swap function and pass the *(node->data) and *(node->next->data) something like -

void SwapDataWithNext(struct Node *node)
{
       /* Swap data of node with its next node's data */
        swap(&node->data, &node->next->data);
}

void swap(int *a, int *b)
{
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}
answer Jul 8, 2017 by Salil Agrawal
Similar Questions
+3 votes

How to delete the node that given as argument in single linked list?
Is there any system call to get prev pointer ?

Note: Here the goal is to delete the address not the node's value

...