Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Let's think about what a linked list is, and what your code does.</p> <p>A linked list is a set of nodes chained together by a series of pointers. Based on what the expected behavior you have described, you would like a linked list built like this.</p> <pre><code>public class Test { public static void main(String[] args) { AClass aTest=new AClass(5); // Data: aTest ==&gt; (5) ==&gt; null System.out.println(aTest.data); // Prints 5. aTest.add(6); // Data: aTest ==&gt; (6) ==&gt; (5) ==&gt; null System.out.println(aTest.data); // Prints 6 System.out.println(aTest.rest.data); // Prints 5 } } </code></pre> <p>However, given your implementation, you never actually create a second node--you only ever have the original node, and you corrupt your linked list.</p> <pre><code>public class Test { public static void main(String[] args) { AClass aTest=new AClass(5); // Data: aTest ==&gt; (5) ==&gt; null System.out.println(aTest.data); // Prints 5. aTest.add(6); // Data: aTest ==&gt; (6) ==&gt; (6) ==&gt; (6) ==&gt; forever System.out.println(aTest.data); // Prints 6 System.out.println(aTest.rest.data); // Prints 6 } } </code></pre> <p>So you need <code>add</code> to create a new node:</p> <p><code>rest=this;</code> sets the next pointer to the current object, creating a circular linked list of exactly one element. You need to create a new element. You also have an interesting problem of what direction you assume you are adding items in. You can either add to the front of the list or the back of the list, but note that adding to the front means changing the pointer you have to the front. You can implement both <code>addFront</code> and <code>addBack</code> pretty easily:</p> <pre><code>public AClass addFront(int item) { AClass node = new AClass(item); node.rest = this; return node; } public void addBack(int item) { // Find the end of the list AClass temp = this; while (temp.rest != null) { temp = temp.rest; } temp.rest = new AClass(item); } </code></pre> <p>All that being said, consider using the built-in linked list: <a href="http://docs.oracle.com/javase/6/docs/api/java/util/LinkedList.html" rel="nofollow">http://docs.oracle.com/javase/6/docs/api/java/util/LinkedList.html</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload