Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think the problem is the swap itself: you forgot to set the tail.</p> <p>Here is a small test for exactly that problem:</p> <pre><code>@Test public void test() { SwapListImpl&lt;String&gt; list = new SwapListImpl&lt;String&gt;(); list.add("A"); list.add("B"); list.add("C"); list.swap("A", "C"); assertEquals("C", list.get(0)); assertEquals("C", list.getHead().val); assertEquals("B", list.get(1)); assertEquals("A", list.get(2)); assertEquals("A", list.getTail().val); list.add("D"); assertEquals("C", list.get(0)); assertEquals("C", list.getHead().val); assertEquals("B", list.get(1)); assertEquals("A", list.get(2)); assertEquals("D", list.get(3)); assertEquals("D", list.getTail().val); list.swap("A", "C"); assertEquals("A", list.get(0)); assertEquals("A", list.getHead().val); assertEquals("B", list.get(1)); assertEquals("C", list.get(2)); assertEquals("D", list.get(3)); assertEquals("D", list.getTail().val); list.swap("C", "B"); assertEquals("A", list.get(0)); assertEquals("A", list.getHead().val); assertEquals("C", list.get(1)); assertEquals("B", list.get(2)); assertEquals("D", list.get(3)); assertEquals("D", list.getTail().val); } </code></pre> <p>You see I added two methods to the list, for getting the head and tail, but that's not important - the test would even fail without the explicit test for head and tail. The extra methods for the list are really simple:</p> <pre><code> public Node&lt;T&gt; getTail() { return this.tail; } public Node&lt;T&gt; getHead() { return this.head; } </code></pre> <p>The problem of not setting tail occurs when swapping the last element of the list and then adding another element.</p> <p>Here is a fixed version of the actual swap:</p> <pre><code> if (foundFirst &amp;&amp; foundSecond) { if (second == this.tail) { this.tail = first; } else if (first == this.tail) { this.tail = second; } if (first == this.head) { this.head = second; } else if (second == this.head) { this.head = first; } if (firstPrev == second) { first.next = second; } else { if (firstPrev != null) { firstPrev.next = second; } first.next = secondNext; } if (secondPrev == first) { second.next = first; } else { if (secondPrev != first &amp;&amp; secondPrev != null) { secondPrev.next = first; } second.next = firstNext; } break; } </code></pre> <p>You see I didn't add lines to your code - instead I wrote the code in another way. I think it's more readable, but you also can try just to set the tail in the correct way. But it was too complex for me, so I reduced the complexity of that code - that's the reason why I rewrote it.</p> <p>I would suggest, that you use first and second for the first/second occurence and not for the first/second argument. I think that would improve the readability of the method. But that's another point ;-)</p> <p>Hope that helps - so the order IMHO is not the problem, but the tail.</p>
 

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