Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I add objects into a linked list?
    text
    copied!<p>I have been working on a project where I must implement a java class that implements the use of doubly linked lists. I have the LinkedList class finished with all my methods. I'm just unsure how to actually add node objects into the list. Here is my code so far with test at the bottom. Any help would be appreciated. Thanks</p> <pre><code>public class LinkedList { private Node first; private Node current; private Node last; private int currentIndex; private int numElements; public LinkedList() { this.first = null; this.last = null; this.numElements = 0; this.current = null; this.currentIndex = -1; } private class Node { Node next; Node previous; Object data; } public boolean hasNext() { return (current != null &amp;&amp; current.next != null); } public Object next() { if (!this.hasNext()) { throw new IllegalStateException("No next"); } current = current.next; return current.data; } public boolean hasPrevious() { return (current != null &amp;&amp; current.previous != null); } public Object previous() { if (!this.hasPrevious()) { throw new IllegalStateException("No previous"); } current = current.previous; return current.data; } int nextIndex() { int index = numElements; if (hasNext()) { index = this.currentIndex + 1; } System.out.println(index + "The current index is " + current); return index; } int previousIndex() { int index = -1; if (hasPrevious()) { index = this.currentIndex - 1; } System.out.println(index + "The current index is " + current); return index; } public void set(Object o) { if (this.current == null) { throw new IllegalStateException("No node found, cannot set."); } current.data = o; } public int size() { return numElements; } public void add(Object o) { Node newNode = new Node(); newNode.data = o; if (first == null) { first = newNode; last = newNode; newNode.next = null; } else if (first != null) { if (current == null) { newNode.previous = null; newNode.next = first; first.previous = newNode; first = newNode; } else if (current == last) { newNode.previous = current; newNode.next = null; current.next = newNode; last = newNode; } else { newNode.previous = current; newNode.next = current.next; current.next.previous = newNode; current.next = newNode; } } current = newNode; numElements++; currentIndex++; } public void remove() { if (current != null) { if (current == first &amp;&amp; current == last) { first = null; last = null; } else if (current == last) { current.previous = null; last = current.previous; } else if (current == last) { current.previous.next = null; last = current.previous; } else { current.previous.next = current.next; current.next.previous = current.previous; } current = current.next; numElements--; } } } import java.util.Scanner; public class LinkedListTest { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String name; int index; LinkedList&lt;Object&gt; listOne = new LinkedList&lt;Object&gt;(); listOne.add(object o); } } </code></pre>
 

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