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 5 of 5

Thread: Switch position of nodes on list

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Switch position of nodes on list

    I have a Generic Linked list in Java.
    class Node{          
       T info;     
      Node next;   
     
      Node (T x){         
      info = x;            
      next = null;   
     } 
    }

    I want to switch positions of two nodes on a list. Where a was I want to put b and where b was I want to put a.
     
    public void switcherT a, T b){   
    // Stuck.
    }

    Please help.
    Last edited by Neobs; April 5th, 2012 at 08:49 AM.


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

    Default Re: Switch position of nodes on list

    To figure out the logic for this kind of problem I take piece of paper and draw a linked list with several nodes on it. Then work out the logic for removing a node, remember where it was, remove the second node, put the first node in the list where the second node was and then putting the second node into the list where the first node was.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Switch position of nodes on list

    Thanks for mocking me on both my threads...

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

    Default Re: Switch position of nodes on list

    You are mistaken. What I suggested is the way that I would solve the problem. Before you write any code you must understand what the code is supposed to do. Drawing diagrams on paper of the before and after condition and then working out how to do the steps to get from the before to the after condition is how you get to understand the problem. You must do those steps.

    If you have already done that step and are now ready to write the code, can you explain which step in the list of steps the program must do that you are having problems with?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Switch position of nodes on list

    Quote Originally Posted by Neobs View Post
    Thanks for mocking me on both my threads...
    Nobody is mocking you, despite your rude tone directed towards somebody who is trying to help you, for free, in his own spare time.

    He's trying to help you think through the problem, which is exactly what you should be doing. This is a technical forum for technical questions, not general "please help" type questions. He could have simply told you to google it or look it up on wikipedia, but instead he's trying to help you step through the logic of your problem. I highly suggest you pay more attention and give more respect and appreciation to him.

    I'm much less tolerant with your kind of rudeness than Norm has been so far, so if you don't adjust your attitude, I'm going to ban you.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. where to put the nodes into the array?
    By blaster in forum Algorithms & Recursion
    Replies: 9
    Last Post: March 11th, 2012, 07:03 PM
  2. 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
  3. JTree - Remove All Nodes
    By aussiemcgr in forum Java Theory & Questions
    Replies: 4
    Last Post: December 9th, 2010, 05:27 PM
  4. How to populate a switch statement with a linked list
    By macnasty in forum Collections and Generics
    Replies: 2
    Last Post: May 6th, 2010, 10:47 PM
  5. Having trouble redirecting nodes
    By KingLane in forum Collections and Generics
    Replies: 6
    Last Post: October 19th, 2009, 06:46 PM