Results 1 to 2 of 2
  1. #1
    rsrana78 is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Post whats wrong wid this??? it is showing only last node data not others.

    import java.util.*;
    class Node
    {
    String data;
    Node next;
    }
    class Singly
    {
    public static void main(String args[])
    {
    Node first=null;
    Node current=null;
    Node last=new Node();
    Scanner s=new Scanner(System.in);
    for(int i=0;i<3;i++)
    {
    System.out.print("Enter data:");
    last.data=s.nextLine();
    last.next=null;
    if(first==null)
    first=last;
    else
    {
    current=first;
    while(current.next!=null)
    current=current.next;
    current.next=last;
    }

    Node print=first;
    for(int j=0;j<3;j++)
    {
    System.out.println(print.data);
    print=print.next;
    }

    }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,382
    Rep Power
    5

    Default Re: whats wrong wid this??? it is showing only last node data not others.

    For starters, you need to allocate a new Node and assign it to next. You only did this once, 3rd statement in main()

    Also, please place your code between tags [code][/code] tags.

    Regards,
    Jim
    Last edited by jim829; 03-11-2013 at 07:28 PM. Reason: code tags
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Whats wrong??
    By javaa in forum New To Java
    Replies: 3
    Last Post: 08-29-2012, 01:47 PM
  2. whats wrong with my GUI?
    By EscSequenceAlpha in forum New To Java
    Replies: 12
    Last Post: 05-31-2012, 08:20 PM
  3. Whats wrong with this?
    By Alkor in forum AWT / Swing
    Replies: 7
    Last Post: 04-03-2012, 05:49 AM
  4. Replies: 11
    Last Post: 02-08-2012, 02:43 PM
  5. so whats going on? (things aren't showing up)
    By Adrien in forum AWT / Swing
    Replies: 9
    Last Post: 02-20-2010, 07:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •