// does everything - finds closest node to the one we want to be next too and places it there private static void findBest(HashDequeue.Node n, HashDequeue.Node connect) { HashDequeue.Node t = n; while (t != null) if (t.data == connect.data) return; else t = t.next; t = n; while (t != null) if (t.data == connect.data) return; else t = t.prev; HashDequeue.Node Lconnect = fromLeft(connect); HashDequeue.Node Rconnect = fromRight(connect); HashDequeue.Node L = fromLeft(n); HashDequeue.Node R = fromRight(n); answer.insert(connect); if (L.data + Rconnect.data < R.data + Lconnect.data) { L.next.next = Rconnect.next; Rconnect.next.prev = L.next; return; } R.next.prev = Lconnect.next; Lconnect.next.next = R.next; }
private static HashDequeue.Node fromRight(HashDequeue.Node n) { int i = 0; while (n.prev != null) { n = n.prev; ++i; } return new HashDequeue.Node(n, null, i); }
private static HashDequeue.Node fromLeft(HashDequeue.Node n) { int i = 0; while (n.next != null) { n = n.next; ++i; } return new HashDequeue.Node(n, null, i); }