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

Thread: Replacing Node Value

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Replacing Node Value

    Hey Guys, Could somebody point me in the right direction to replacing a node in a linked list?

    I have no clue on where to start, thus. I have no code to supply.

    I current have, a destory method, a find, a destroyFirstNode, a destroyLastNode, print, insert, etc etc... But I just have no clue on how to replace the nodes value....


    Any pointers?

    I don't want code, I actually want to learn, not memorise.

    Cheers, Pete.


  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: Replacing Node Value

    To replace one node in a linked list with another node, copy the pointers and set the pointers that are used to put the new node in the list.
    To see what pointers need to be changed, take a piece of paper, draw some nodes in a linked list with the pointers that connect the nodes. Draw a new node outside of the linked list next to a node in the list. Then see what pointers need to be changed to put the new node in the list replacing the node that is in the list.

    Whoops, the above is to replace a node.
    To change the value that a node holds, find the node in the list and call the node's set method to change its contents.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    ProgrammablePeter (March 30th, 2014)

  4. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Replacing Node Value

    public Node replace(Object data, Object value) {
    		Node current = head;
     
    		while(current != null) {
    			if(current.getData() == data) {
    				current.setData(value);
    				return current;
    			}
    			current = current.getNext();
     
    		}
    		return null;
    	}

    This works! Thanks man! Actually took me a while to figure it out, cheers! You're a star!

Similar Threads

  1. Replacing a JButton
    By dougie1809 in forum AWT / Java Swing
    Replies: 3
    Last Post: April 10th, 2013, 07:52 AM
  2. [SOLVED] Replacing dots with spaces
    By dTi in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 17th, 2011, 08:53 PM
  3. LIST NODE TROUBLES
    By YONATAN in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 26th, 2011, 03:35 AM
  4. [SOLVED] replacing subString
    By nasi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 21st, 2010, 09:47 PM
  5. TreeNode vs. Node
    By Kumarrrr in forum Java Theory & Questions
    Replies: 1
    Last Post: March 27th, 2010, 06:06 AM