Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: swapping nodes in a linked list

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default swapping nodes in a linked list

    i have a question regarding swapping nodes in a linked list. Suppose we have a linked list : 2->5->7->4->6

    Suppose we want to swap node with value 5 and node with value 4. What do we pass as parameters to our function? If my function is defined as swap(Node n1, Node n2), what do i pass in my main function so it knows which nodes to swap?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: swapping nodes in a linked list

    Is the linked list class your code or the one from the Java SE? That would determine what methods are available to do the swap.

    You ask the questions.
    How to swap elements in the list based on the contents of the objects in the list: values 4 and 5
    How to swap n1 and n2.
    In the first case pass the contents of the objects
    In the second case pass references to objects

    The swap method(s) would have to search the list to find the two elements to be swapped and then could swap them depending on the methods available.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: swapping nodes in a linked list

    im using my own code......lets say i have my function: swap(Node n1, Node n2);
    since i want to swap nodes, how do i pass the nodes that i want to swap? if i want to pass node with content 5 and node with content 4, what do i pass as my parameters?
    what i did was pass function that look for nodes at specific indexes: if list is 2->5->7->4->6 and i want to swap node with content 5 and node with content 4, then i call my function as: swap(findNodeAtIndexN(1), findNodeAtIndexN(3))
    is there a simpler way to pass nodes in my swap function?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: swapping nodes in a linked list

    What methods does your linked list have that could be used for swapping?

    how do i pass the nodes that i want to swap
    As arguments to the swap method: swap(node1, node2);

    with content 5 and node with content 4
    As args: swap(5,4);

    If the findNodeAtIndexN method returns a Node object, then
    swap(findNodeAtIndexN(1), findNodeAtIndexN(3));
    would be calling the same method as the first one above: swap(node1, node2);

    I don't know what would be simpler way to specify what nodes to swap than those two methods.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Switch position of nodes on list
    By Neobs in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 9th, 2012, 07:10 AM
  2. linked list
    By javasohard in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 18th, 2011, 02:22 AM
  3. Linked list Schminked list help with Nodes Please
    By Bially in forum Collections and Generics
    Replies: 1
    Last Post: September 29th, 2011, 03:20 PM
  4. Linked List Help
    By BuhRock in forum Collections and Generics
    Replies: 3
    Last Post: September 26th, 2011, 08:43 AM
  5. Help with linked list
    By joecool594 in forum Collections and Generics
    Replies: 3
    Last Post: November 28th, 2010, 12:33 PM