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

Thread: need help implementing LinkedList

  1. #1
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default need help implementing LinkedList

    hi guys, I know how LinkedList works, but I've been given classes and methods headers to implement LinkedList, and I need a bit of help with this. Basically consist of three classes Students(which contains student attributes and methods... no help needed here), and the other two classes, which I'm showing below.
    The Node class. I implemented the constructor; however, I don't see what's the point of having "public Student value" here, or am I missing something?

    public class Node {
     
        //construct student object
        public Student value;
     
        public Node next;
     
        //constructor
        public Node(Student s)
        {
            this.next = new Node(s);
        }     
    }

    And here's the Class LinkedList. My main concern here is how to implement the constructor

    public class LinkedList {
     
        private Node first;
     
        public LinkedList()
        {
     
        }
     
        //add students to the list
        public void add(Student s)
        {
     
        }
     
        //remove duplicate records (return true if duplicate found)
        public boolean remove(String fn, String ln)
        {
     
        }
     
        //display list of student
        public void display()
        {
     
        }
     
    }

    Thanks


  2. #2
    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: need help implementing LinkedList

    The Node class has a value and a pointer to the next Node.
    Have you tried creating an instance of the Node class? You should be compiling and testing the code after each small step to check if there are any problems.

    The LinkedList class should control a list of Nodes. It would have methods to change the list of Nodes it controls.
    What is your problem with the constructor? What is it supposed to do? Are there overloads to it(different versions)?
    Last edited by Norm; June 6th, 2012 at 03:59 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: need help implementing LinkedList

    What type of LinkedList? Singly or Doubly?

    Singly just has a next variable but Doubly has both a next and a previous variable and so is a bit harder to do, though perhaps more efficient in some ways.

    Let me help you a bit.
    public class Node {
     
        //construct student object
        public Student value;
     
        public Node next;
     
     
        // default constructor
        public Node(){}
     
     
        // non-default constructor
     
     
        public Node(Student value)
        {
          //   this.next = new Node(s);  Probably not what you want.  Perhaps you should make Node an inner class, if you haven't already, stupid thing won't let me see your post and type a reply at the // same time
     
    setValue(value);
        }     
     
    public void setValue(Student value)
    {
    this.value = value;
    }
     
    public Student getValue()
    {
    return value;
    }
     
    public void setNext(Node next)
    {
    this.next = next;
    }
     
    public Node getNext()
    {
    return next;
    }
    }
    Last edited by javapenguin; June 6th, 2012 at 04:42 PM.

  4. #4
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: need help implementing LinkedList

    Quote Originally Posted by Norm View Post
    The Node class has a value and a pointer to the next Node.
    Have you tried creating an instance of the Node class? You should be compiling and testing the code after each small step to check if there are any problems.
    after reading your post kind of have an idea on how to implement the node constructor; however, I'm still in doubt how to create the link to the next node... given my class. And how would I test this?

    public Node(Student s)
        {
            this.value = s;
            this.next = new Node(s);
        }

    Don't know if it is completely correct, like you said I need to test it... but how?

  5. #5
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: need help implementing LinkedList

    it is a singly list, because it only contains the data, and the link to the next node

  6. #6
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: need help implementing LinkedList

    Quote Originally Posted by Norm View Post
    The LinkedList class should control a list of Nodes. It would have methods to change the list of Nodes it controls.
    What is your problem with the constructor? What is it supposed to do? Are there overloads to it(different versions)?
    no there are no overloads, but I'm kind of confuse as to what should include. I know linklist is compose of nodes(data, link), so the constructor should initialize the data and the link? I don't see how the "Node first" would fit in this class

    Thanks

  7. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,600
    Thanks
    24
    Thanked 314 Times in 294 Posts

    Default Re: need help implementing LinkedList

    You need to create a main to test it. And, I would put some second thought into that constructor if I were you. I can see an infinitely growing linked list in your future when you try to create a Node object.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  8. #8
    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: need help implementing LinkedList

    Test with code to create an instance of the Node class, compile and execute it.

    To visualize a linked list, take a piece of paper, draw some Nodes with data and next and chain the Nodes together. Have a first Node (will be in the linked list class) that points to the first Node in the list.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: need help implementing LinkedList

    this is what I implemented so far and the tester too.... you guys tell me if I'm on the right track

    public class Node {
     
        //construct student object
        public Student value;
     
        public Node next;
     
        //constructor
        public Node(Student s)
        {
            this.value = s;
        }  
     
        //set next
        public void setNext(Node next)
        {
            this.next = next;
        }
     
        //return value
        public Student getValue()
        {
            return value;
        }
     
        //return next
        public Node getNext()
        {
            return next;
        }

    public class Tester {
     
     
        public static void main(String[] args) {
     
            UnderGrad john = new UnderGrad("john", "doe", 2.7, "computer Science", "phisics");
            UnderGrad jim = new UnderGrad("james", "neil", 3.8, "computer Science", "programming");
     
            Node first = new Node(john);
            Node second = new Node(james);
            first.setNext(second);
     
     
        }
    }

  10. #10
    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: need help implementing LinkedList

    Does it compile and execute?

    For debugging I'd add a toString() method to the Node class that would return the value and next variables. Then you can print a Node and see it all.
    If you don't understand my answer, don't ignore it, ask a question.

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

    mia_tech (June 8th, 2012)

  12. #11
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: need help implementing LinkedList

    Quote Originally Posted by Norm View Post
    Does it compile and execute?

    For debugging I'd add a toString() method to the Node class that would return the value and next variables. Then you can print a Node and see it all.
    ok, I changed it around to fit the exercise, but still I'm not displaying anything to screen. I presume that my problem is either in the "add" or "display" method of my LinkedList class

    public class Node {
     
        //construct student object
        public Student value;
     
        public Node next;
     
        //constructor
        public Node(Student s)
        {
            this.value = s;
        }
     
    }

    public class LinkedList {
     
     
        private Node first;
     
        public LinkedList()
        {
            first.value = null;
        }
     
        //add students to the list
        public void add(Student s)
        {
            Node newNode = new Node(s);
            newNode.next = first;
            first = newNode;    
     
        }
     
        //display list of student
        public void display()
        {
            if(first == null)
                System.out.println("List is empty");
            else
                while(first != null)
                {
                    System.out.println(first.value);
                    first = first.next;
                }
        }
    }


    public class Tester {
     
     
        public static void main(String[] args) {
     
     
            LinkedList students = new LinkedList();
     
            UnderGrad john = new UnderGrad("john", "doe", 2.7, "computer Science", "phisics");
            UnderGrad jorge = new UnderGrad("jorge", "vazquez", 3.8, "computer Science", "programming");
     
            students.add(john);
            students.add(jorge);
     
            students.display();       
     
        }
    }

    this is the error I'm getting
    run:
    Exception in thread "main" java.lang.NullPointerException
    	at LinkedList.<init>(LinkedList.java:13)
    	at Tester.main(Tester.java:12)
    Java Result: 1
    Last edited by mia_tech; June 7th, 2012 at 12:50 PM.

  13. #12
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: need help implementing LinkedList

    never mind... I figured it out!

  14. #13
    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: need help implementing LinkedList

    Did you try adding a Public String toString() {} method to the Node class that returns a String with the value and next values. It allows you to print a node easily for debugging. For example if you printed first, it would print the list of nodes.

    run:
    Exception in thread "main" java.lang.NullPointerException
    at LinkedList.<init>(LinkedList.java:13)
    Look at line 13 in the LinkedList class and see what variable on that line has a null value. Then backtrack in the code to see why it does not have a valid non-null value.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #14
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: need help implementing LinkedList

    Quote Originally Posted by Norm View Post
    Did you try adding a Public String toString() {} method to the Node class that returns a String with the value and next values. It allows you to print a node easily for debugging. For example if you printed first, it would print the list of nodes.

    Look at line 13 in the LinkedList class and see what variable on that line has a null value. Then backtrack in the code to see why it does not have a valid non-null value.
    is this what you mean?

    public String toString()
        {
            return this.value.toString()+ " " + this.next.toString();
        }

  16. #15
    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: need help implementing LinkedList

    I would add ID Strings to make it clearer:

    return "Node: " +value + " next=" + next;

    The this & toString() are redundant
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Sorting LinkedList
    By wdh in forum What's Wrong With My Code?
    Replies: 28
    Last Post: April 29th, 2012, 12:52 PM
  2. addInOrder LinkedList
    By PeskyToaster in forum Collections and Generics
    Replies: 1
    Last Post: April 6th, 2012, 06:16 AM
  3. LinkedList Iterator
    By cpguy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 16th, 2011, 08:51 PM
  4. LinkedList Objects
    By thedolphin13 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: October 13th, 2010, 03:14 PM
  5. Implementing LinkedList as a user?
    By vluong in forum Collections and Generics
    Replies: 3
    Last Post: October 15th, 2009, 03:00 AM