Note that there are some explanatory texts on larger screens.

plurals
  1. POcircular single linkedList
    text
    copied!<p>I've done some exercises in Java and now I'm stuck at such a problem - my list works incorrectly. I am sure that <code>remove</code> works incorrectly and maybe you can help me (with advice or code) to implement a circular <strong>singly</strong> linked list in a correct way. I am not sure whether other functions work properly, but I've tried to do my best.</p> <p>Here is my code:</p> <pre><code>import java.util.*; public class Node { private Object value; private Object nextValue; private Node next; public Node(int data) { this.value = data; this.next = null; } public Object getValue() { return this.value; } public Node nextItem() { return this.next; } public void setNextItem(Node nextItem) { this.next = (Node) nextItem; this.next.setValue(nextItem.getValue()); } public void setValue(Object arg0) { this.value = arg0; } } ------------------------------------------------------------------- import java.util.*; public class CircularList { private Object[] array; private int arrSize; private int index; private Node head; private Node tail; public CircularList() { head = null; tail = null; } public boolean add(Node item) { if (item == null) { throw new NullPointerException("the item is null!!!"); } if (head == null) { head = item; head.setNextItem(head); arrSize++; return true; } Node cur = head; while(cur.nextItem() != head) { if(cur.getValue() == item.getValue()) { throw new IllegalArgumentException("the element already " + "exists!"); } cur = cur.nextItem(); } head.setNextItem(item); item.setNextItem(head); arrSize++; return true; } public Node getFirst() { return head; } public void insertAfter(Node item, Node nextItem) { if ((item == null) || (nextItem == null)) { throw new NullPointerException("the item is nul!!!"); } else if (this.contains(nextItem) == true) { throw new IllegalArgumentException("the item already exists!"); } Node cur = head; while(cur.nextItem() != head) { if(cur.getValue() == item.getValue()) { nextItem.setNextItem(item.nextItem()); item.setNextItem(nextItem); } else { cur = cur.nextItem(); } } } public boolean remove(Node item) { if(item == head) { Node cur = head; for(int i = 0; i &lt; arrSize-1; i++) { cur = cur.nextItem(); } head = head.nextItem(); for(int i = 0; i &lt; arrSize; i++) { cur = cur.nextItem(); } arrSize--; return true; } Node cur = head; int counter = 0; while(cur.nextItem() != head) { if(cur == item) { item = null; cur = cur.nextItem(); while(cur.nextItem() != head) { cur.setNextItem(cur.nextItem().nextItem()); } return true; } cur = cur.nextItem(); } return false; } public int size() { return arrSize; } public boolean contains(Object o) { if ((o == null) &amp;&amp; (arrSize == 0)) { return false; } Node cur = head; while(cur.nextItem() != head) { if(cur.getValue() == o) { return true; } cur = cur.nextItem(); } return false; } } </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