Thread: Help with swapping nodes in a linked list

  1. #1
    Learner Axel's Avatar
    Join Date
    Aug 2005

    Help with swapping nodes in a linked list

    I don't understand the logic behind swapping a node in a link list. Currently i have a simple list with customers their names are in a array which is stored in a struct on the linked list node. There's a pointer to the start, from there i just loop around till i get to the end custList->next etc.. all the names are printed.

    Now, i can get a reference to the start and next elements in the list. But how exactly do i reference a specific node that i want? and swap it?

  2. #2
    Registered User
    Join Date
    Jan 2005
    You would search thru the nodes untill you find one who's data matches what you're looking for. You can also reference a node by couting thru the list untill you reach a spcific node.
    You swap a node by changing the previous node's next pointer to point to the new node and then point the new node's next pointer to the node which followed the node you're swapping out.

  3. #3
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Hmm i haven't had any luck. Are there any simple examples? i.e. two nodes i just want them swapped, i want to get the basic concept figured out first.

    I'm trying to get the basic idea, but the actual problem is; i have a linked list with city names they have an x and y co-ordinate. What i want to do is find the shortest path around and reorder the list. I have the basic link list structure and the formula that will be used:

    sqrt( ( pow( (x1 - x2), 2) + pow( (y1 - y2), 2) ) );

    I'm just having trouble approaching the problem and comparing the link list elements i.e.

    if i have A, B, C and D in a map and i have to coordinates, if travelling from A to C to B to D is shorter than A-B-C-D or C-B-F then B and C should be swapped. I have the x and y co-ordinates in my struct of the node.
    Last edited by Axel; 10-18-2005 at 07:37 AM.

  4. #4
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Its a well known problem.just google for shortest path algorithm.
    You have to traverse the list.
    current =header of list 
    while(current->next!=required val)
    there are two methods to swap the link list variable:
    1.first change the pointers(next) as told above the values in structure.
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list question
    By mikeman in forum C Programming
    Replies: 1
    Last Post: 11-30-2008, 01:56 PM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  3. Linked List and Nodes
    By paperbox005 in forum C++ Programming
    Replies: 2
    Last Post: 08-04-2004, 09:12 AM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM