简体   繁体   中英

Swapping 2 Nodes in a Linked List

Good day guys, im new here to C and am trying to learn linked lists. I been trying to swap 2 nodes from within a linked list but so far have been having trouble getting it to work. The code I been trying to use causes an endless circular loop, but I don't think it is because of the if or while statement.

Take a look? Any pointers here? Help would be greatly appreciated.

Basically, the code uses a user input to search for a node based on the data inside, then it should swap the node with the data inside with the next node. Been at this for 3 hours, can anybody help? Thanks!

/ conductor is the name im using of the pointer for the current node /

#include <stdio.h>
#include <stdlib.h>

struct node {
  int x;
  struct node *next;
  struct node *prev;
};

struct node *root;
struct node *conductor;
struct node *counter;
struct node *newnode;
struct node *back;

struct node *swapper;
struct node *swappee;
struct node *blanker;

int add = 0;
int initialization = 0;
int query = 0;

int swap ()
{

printf("enter data to search from within the nodes: ");
fflush(stdin);
scanf("%d", &query);

conductor = root;
while ( conductor->next != 0)
    {
        if(conductor->x == query)
        {
        printf("\n%d\n", query);
        swapper = conductor;
        swappee = conductor->prev;
        conductor = swappee;
        conductor->next = swapper;
        break;
        }
        else
        {
        conductor = conductor->next;
        }
    }

mainMenu ();

}

A double linked list (like the one you have) is basically an array of node, each node pointing to its neighbors. Let's say we have nodes -ABCD- (AB means that A points to B and B points to A). Let's say you want to swap B and C. You have to make 4 changes:

  • Make A point to C
  • Make C point to B and A
  • Make B point to D and B
  • make D point to B

You make only the second and the third change. So, you need to add A->next = B and D->prev=C . I hope it is clear enough.

Also, you should not fflush input streams.

If you want to swap the data:

if (conductor->x == query) {
    int temp = conductor->x;
    if (conductor->next)
        conductor->x = conductor->next->x;
        conductor->next->x = temp;
    }
}

Typically that is what you will want to do. If you have a structure with several members instead of the 1 int , swapping the pointers may seem less messy in theory, but it isn't, primarily due to the fact that you must test for existence of a next/previous node so often. In truth, you'd probably want a pointer to a separate structure in such a case.

Given three nodes — previous , current , and next , pointing to current->prev , current , and current->next respectively — you must update at most 6 pointers:

  1. next->prev = previous
  2. previous->next = next
  3. current->prev = next
  4. current->next = next->next
  5. next->next = current
  6. current->next->prev = current

Step 2 is not necessary if previous is NULL . Step 7 is unnecessary if current->next is NULL .

The entire thing is unnecessary if next is NULL .

If you want to swap with the previous node instead of the next, exchange any instance of the variable previous with the variable next and vice-versa as well as exchanging any instance of ->prev with ->next and vice-versa.

Overall, this requires a fair bit of branching code, which can be slow. This is why it is usually better to swap the data rather than messing with the pointers. It gets even messier when you want to swap with the previous node and you only have a singly-linked list that points to the next node because you must store yet another pointer for the equivalent of previous->prev , assuming previous exists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM