Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is wrong with this linked list?
    text
    copied!<p>The assignment was to write a function to swap 2 nodes in the list. If the function could swap the nodes regardless of the order, a 10% was awarded. I think my implementation is able to swap 2 elements regardless of the order in the list but I still did not received the bonus marks. Is there anything that I am missing?</p> <p>I was given a generic node class,</p> <pre><code>public class Node&lt;T&gt; { public T val; public Node&lt;T&gt; next; public Node(T val) { this.val = val; this.next = null; } } </code></pre> <p>I was also given an interface defined as below,</p> <pre><code>public interface SwapList&lt;T&gt; { public void add(T val); /** * Swaps two elements in the list, but only if @param val1 comes BEFORE @param * val2. Solve the problem regardless of the order, for 10% extra. list: A B * C -&gt; swap(A,B) will result in the list B A C list: A B C -&gt; swap(B,A) * will not swap. list: A C C -&gt; swap(A, D) will throw a * NoSuchElementException list: A B C B -&gt; swap (A, B) will result in the * list B A C B list: A B C A B B -&gt; swap (A,B) will result in the list B A * C A B B a list with one or zero elements cannot do a swap */ public void swap(T val1, T val2); public T get(int i); } </code></pre> <p>and I have my own implementation of this interface as below,</p> <pre><code>import java.util.NoSuchElementException; public class SwapListImpl&lt;T&gt; implements SwapList&lt;T&gt; { private Node&lt;T&gt; head; private Node&lt;T&gt; tail; private int counter; public SwapListImpl() { head = null; tail = null; counter = 0; } @Override public void add(T val) { Node&lt;T&gt; node = new Node&lt;T&gt;(val); if (head == null) { head = node; tail = node; } else { tail.next = node; tail = node; } counter++; } @Override public void swap(T val1, T val2) { if (counter &lt; 2 || val1.equals(val2)) return; Node&lt;T&gt; current = head; Node&lt;T&gt; currentPrev = null; Node&lt;T&gt; first = head; Node&lt;T&gt; firstPrev = null; Node&lt;T&gt; firstNext = first.next; Node&lt;T&gt; second = head; Node&lt;T&gt; secondPrev = null; Node&lt;T&gt; secondNext = second.next; boolean foundFirst = false; boolean foundSecond = false; boolean inOrder = false; while (current != null) { if (!foundFirst &amp;&amp; current.val.equals(val1)) { firstPrev = currentPrev; first = current; firstNext = current.next; if (!foundSecond) inOrder = true; foundFirst = true; } if (!foundSecond &amp;&amp; current.val.equals(val2)) { secondPrev = currentPrev; second = current; secondNext = current.next; if (foundFirst) inOrder = true; foundSecond = true; } if (foundFirst &amp;&amp; foundSecond) { if (!inOrder) { Node&lt;T&gt; temp = first; first = second; second = temp; temp = firstPrev; firstPrev = secondPrev; secondPrev = temp; temp = firstNext; firstNext = secondNext; secondNext = temp; } if (firstPrev == null) { head = second; if (first == secondPrev) { second.next = first; first.next = secondNext; } else { second.next = firstNext; secondPrev.next = first; first.next = secondNext; } } else { firstPrev.next = second; first.next = secondNext; if (first == secondPrev) { second.next = first; } else { second.next = firstNext; secondPrev.next = first; } } break; } currentPrev = current; current = current.next; } if (!foundFirst || !foundSecond) { throw new NoSuchElementException(); } } @Override public T get(int i) { if (i &lt; counter) { Node&lt;T&gt; node = head; for (int n = 0; n &lt; i; n++) { node = node.next; } return node.val; } else { throw new IndexOutOfBoundsException(); } } } </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