public class BubbleSwapList { private class Node { public E element; public Node next; public Node previous; public Node(E element) { this.element = element; this.next = null; this.previous = null; } } private Node head; public BubbleSwapList() { head = null; } public void add(E element) { Node toAdd = new Node(element); if (head != null) { head.previous=toAdd; } toAdd.next = head; head = toAdd; } private void bubbleSwap(Node a) { Node aprev=a.previous; Node anext=a.next; if (aprev!=null) { aprev.next=anext; if (anext!=null) anext.previous=aprev; } if (anext!=null) { Node anextnext= anext.next; anext.next = a; a.previous = anext; a.next = anextnext; if (anextnext!=null) { anextnext.previous=a; } if (a==head) { head=anext; } } } public String toString() { StringBuilder sb = new StringBuilder(); Node n = head; while (n!=null) { sb.append(n.element); sb.append(" "); n = n.next; } return sb.toString(); } public void swaptest1() { Node n = this.head.next; bubbleSwap(n); } public void swaptesth() { Node n = this.head.next; bubbleSwap(n); } public static void main(String[] args) { BubbleSwapList bsl = new BubbleSwapList<>(); bsl.add("AA"); bsl.add("BB"); bsl.add("C"); bsl.add("D"); System.out.println("LIST:" + bsl); bsl.swaptest1(); System.out.println("LIST:" + bsl); bsl.swaptest1(); System.out.println("LIST:" + bsl); bsl.swaptesth(); System.out.println("LIST:" + bsl); } }