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: Help with linked list problem

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help with linked list problem

    This is my code and after printing the result i get is 0 for each item
    import java.util.Scanner; 
     
    class Node {
        public int data;
        public Node next;
     
        //Link constructor
        public Node(int d1) {
    	    this.data=data;	    
        }
     
        //Print Link data
        public void printNode() {
    	    System.out.print(data);
        }
    }
     
    class LinkList {
        private Node first;
     
        //LinkList constructor
        public LinkList() {
    	    first=null;
        }
     
        //Returns true if list is empty
        public boolean isEmpty() {
    	    return first == null;
        }
     
        //Inserts a new Link at the first of the list
        public void insert(int d1) {
    	    Node newNode = new Node(d1);
    	    newNode.next = first;
    	    first = newNode;
        }
     
        //Deletes the link at the first of the list
        public Node delete() {
    	    Node temp = first;
    	    first = first.next;
    	    return temp;
        }
     
        //Prints list data
        public void printList() {
    	    Node current = first;
    	    System.out.print("List: ");
    	    while(current != null) {
    		    current.printNode();
    		    current = current.next;
    	    }
    	    System.out.println("");
        }
    }  
     
    class LinkListTest {
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
    	    LinkList list = new LinkList();
                int data=0;
     
                System.out.print("Please enter a succession of integers. Enter 0 to end ");
     
     
                while(data!=999){
                    data=scan.nextInt();
                    list.insert(data);
                }
     
    	    list.printList();
     
    	    while(!list.isEmpty()) {
    		    Node deletedLink = list.delete();
    		    System.out.print("deleted: ");
    		    deletedLink.printNode();
    		    System.out.println("");
    	    }
    	    list.printList();
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with linked list problem

    Where does the code assign a value to data?
    What does the constructor do with the arg that is passed to it in d1?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    71
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: Help with linked list problem

    what does this.data=data mean in your constructor? you only have one data member, public int data, defined so your constructor isn't assigning anything... your boolean method in the LinkList class i think would be better to have a conditional statement for first == null and then have it explicitly return true

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with linked list problem

    I think this is fine:
    return first == null;
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: October 25th, 2012, 02: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. Simple linked list problem
    By babe20042004 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 5th, 2011, 10:40 AM
  4. Linked List problem, please help.
    By Axeander in forum What's Wrong With My Code?
    Replies: 10
    Last Post: December 21st, 2010, 05:01 PM
  5. Recursive function based on Linked list
    By rosh72851 in forum Collections and Generics
    Replies: 1
    Last Post: March 9th, 2009, 06:23 PM