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: java LinkedList problem........plz have a look and tell me the reason

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

    Default java LinkedList problem........plz have a look and tell me the reason

    public class LinkedList 
    {
    	static Node start,end;
    	public static void main(String... args)
    	{
    		toNode(args);
     
    	}
     
    static void toNode(String... arr)
    	{
    	end=start=new Node(Integer.parseInt(arr[0]));
    	Node p=start;
    	for(int i=1;i<arr.length;i++)
    	{
    		p.next=new Node(Integer.parseInt(arr[i]));
    		System.out.println(arr[i]+"inserted at "+p.next);
     
    	}
    	System.out.println("-------------------\nlocation of start: "+start);
    	System.out.println("location of start.next: "+start.next);
    	System.out.println("location of end: "+end+"\n\n");
     
     
    //-------------------problem-----------------------------	
    	System.out.println("And output of traversing is: \n");
    	 for (p = start; p != null; p = p.next) 
    	 {
    		      System.out.println(p.data);
    		     }
    //---------------------------------------------------------	
    	}
     
    }
     
     
     
    class Node
    {	int data;
    	Node next;
    	Node(int data)
    	{
    		this.data=data;
    	}	
    }
    and output is:
    45inserted at Node@3e25a5
    78inserted at Node@19821f
    23inserted at Node@addbf1
    45inserted at Node@42e816
    19inserted at Node@9304b1
    24inserted at Node@190d11
    687inserted at Node@a90653
    47inserted at Node@de6ced
    -------------------
    location of start: Node@c17164
    location of start.next: Node@de6ced
    location of end: Node@c17164


    And output of traversing is:

    12
    47


    plz tell me why it is having only two node and it is not behaving like a LinkedList according to me there is no problem..................plz tell me


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,170
    My Mood
    Hungover
    Thanks
    141
    Thanked 599 Times in 514 Posts

    Default Re: java LinkedList problem........plz have a look and tell me the reason

    I moved this thread: This Forum is for Saying Hi, not for Asking Technical Questions (READ BEFORE POSTING)

    Have you stepped through this with a debugger, or at the very least added some print statements, to figure out what's going on? When does the flow of the program differ from what you expect?
    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!

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,170
    My Mood
    Hungover
    Thanks
    141
    Thanked 599 Times in 514 Posts

    Default Re: java LinkedList problem........plz have a look and tell me the reason

    Also, don't post multiple copies of the same question. That doesn't increase your chances of getting help. I've deleted your duplicate thread.
    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!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: java LinkedList problem........plz have a look and tell me the reason

    Add some more printlns to show the values of ALL the variables that are used in the code when you are changing the values. If you print them all out, you will see the problem.

Similar Threads

  1. LinkedList Iterator
    By cpguy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 16th, 2011, 08:51 PM
  2. Reason for swing component overlapping?
    By Furious5k in forum AWT / Java Swing
    Replies: 1
    Last Post: November 6th, 2011, 07:40 AM
  3. LinkedList Objects
    By thedolphin13 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: October 13th, 2010, 03:14 PM
  4. Replies: 1
    Last Post: March 31st, 2010, 09:42 PM
  5. hmm for some reason the char.At isnt working
    By dvsumosize in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 2nd, 2010, 03:27 AM

Tags for this Thread