Filip_Markoski

[ADS] Bubble sort on a linked list (Swap Nodes)

Nov 20th, 2017
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.90 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Arrays;
  5. import java.util.Scanner;
  6.  
  7. class DLLNode<E> {
  8.     protected E element;
  9.     protected DLLNode<E> pred, succ;
  10.  
  11.     public DLLNode(E elem, DLLNode<E> pred, DLLNode<E> succ) {
  12.         this.element = elem;
  13.         this.pred = pred;
  14.         this.succ = succ;
  15.     }
  16.  
  17.     @Override
  18.     public String toString() {
  19.         return element.toString();
  20.     }
  21. }
  22.  
  23.  
  24. class DLL<E> {
  25.     private DLLNode<E> first, last;
  26.  
  27.     public void setFirst(DLLNode<E> first) {
  28.         this.first = first;
  29.     }
  30.  
  31.     public void setLast(DLLNode<E> last) {
  32.         this.last = last;
  33.     }
  34.  
  35.     public DLL() {
  36.         // Construct an empty SLL
  37.         this.first = null;
  38.         this.last = null;
  39.     }
  40.  
  41.     public void deleteList() {
  42.         first = null;
  43.         last = null;
  44.     }
  45.  
  46.     public int length() {
  47.         int ret;
  48.         if (first != null) {
  49.             DLLNode<E> tmp = first;
  50.             ret = 1;
  51.             while (tmp.succ != null) {
  52.                 tmp = tmp.succ;
  53.                 ret++;
  54.             }
  55.             return ret;
  56.         } else
  57.             return 0;
  58.  
  59.     }
  60.  
  61.     public void insertFirst(E o) {
  62.         DLLNode<E> ins = new DLLNode<E>(o, null, first);
  63.         if (first == null)
  64.             last = ins;
  65.         else
  66.             first.pred = ins;
  67.         first = ins;
  68.     }
  69.  
  70.     public void insertLast(E o) {
  71.         if (first == null)
  72.             insertFirst(o);
  73.         else {
  74.             DLLNode<E> ins = new DLLNode<E>(o, last, null);
  75.             last.succ = ins;
  76.             last = ins;
  77.         }
  78.     }
  79.  
  80.     public void insertAfter(E o, DLLNode<E> after) {
  81.         if (after == last) {
  82.             insertLast(o);
  83.             return;
  84.         }
  85.         DLLNode<E> ins = new DLLNode<E>(o, after, after.succ);
  86.         after.succ.pred = ins;
  87.         after.succ = ins;
  88.     }
  89.  
  90.     public void insertBefore(E o, DLLNode<E> before) {
  91.         if (before == first) {
  92.             insertFirst(o);
  93.             return;
  94.         }
  95.         DLLNode<E> ins = new DLLNode<E>(o, before.pred, before);
  96.         before.pred.succ = ins;
  97.         before.pred = ins;
  98.     }
  99.  
  100.     public E deleteFirst() {
  101.         if (first != null) {
  102.             DLLNode<E> tmp = first;
  103.             first = first.succ;
  104.             first.pred = null;
  105.             if (first == null)
  106.                 last = null;
  107.             return tmp.element;
  108.         } else
  109.             return null;
  110.     }
  111.  
  112.     public E deleteLast() {
  113.         if (first != null) {
  114.             if (first.succ == null)
  115.                 return deleteFirst();
  116.             else {
  117.                 DLLNode<E> tmp = last;
  118.                 last = last.pred;
  119.                 last.succ = null;
  120.                 return tmp.element;
  121.             }
  122.         }
  123.         // else throw Exception
  124.         return null;
  125.     }
  126.  
  127.     public E delete(DLLNode<E> node) {
  128.         if (node == first) {
  129.             deleteFirst();
  130.             return node.element;
  131.         }
  132.         if (node == last) {
  133.             deleteLast();
  134.             return node.element;
  135.         }
  136.         node.pred.succ = node.succ;
  137.         node.succ.pred = node.pred;
  138.         return node.element;
  139.  
  140.     }
  141.  
  142.     @Override
  143.     public String toString() {
  144.         String ret = new String();
  145.         if (first != null) {
  146.             DLLNode<E> tmp = first;
  147.             ret += tmp + "<->";
  148.             while (tmp.succ != null) {
  149.                 tmp = tmp.succ;
  150.                 ret += tmp + "<->";
  151.             }
  152.         } else
  153.             ret = "Prazna lista!!!";
  154.         return ret;
  155.     }
  156.  
  157.     public DLLNode<E> getFirst() {
  158.         return first;
  159.     }
  160.  
  161.     public DLLNode<E> getLast() {
  162.         return last;
  163.     }
  164.  
  165.     public void swapDLLNodes(DLLNode<E> i, DLLNode<E> j) {
  166.         DLLNode<E> iFollowing = i.succ;
  167.         DLLNode<E> jFollowing = j.succ;
  168.         DLLNode<E> iPrevious = i.pred;
  169.         DLLNode<E> jPrevious = j.pred;
  170.  
  171.         i.succ = jFollowing;
  172.         j.succ = iFollowing;
  173.         iPrevious.succ = j;
  174.         jPrevious.succ = i;
  175.  
  176.         jPrevious.pred = i;
  177.         iPrevious.pred = j;
  178.         i.pred = jPrevious;
  179.         j.pred = iPrevious;
  180.     }
  181.  
  182.     public void swapAdjacentDLLNodes(DLLNode<E> i, DLLNode<E> j) {
  183.         if (i == null || j == null) {
  184.             return;
  185.         }
  186.        
  187.         DLLNode<E> iFollowing = i.succ;
  188.         DLLNode<E> jFollowing = j.succ;
  189.         DLLNode<E> iPrevious = i.pred;
  190.         DLLNode<E> jPrevious = j.pred;
  191.  
  192.         if (i == null || j == null) {
  193.             return;
  194.         }
  195.  
  196.         i.succ = jFollowing;
  197.         j.succ = i;
  198.         iPrevious.succ = j;
  199.  
  200.         i.succ.pred = i;
  201.         j.pred = iPrevious;
  202.         i.pred = j;
  203.  
  204.     }
  205.  
  206.     public void swapFirstAndAdjacentDLLNodes(DLLNode<E> firstNode, DLLNode<E> j) {
  207.         if (firstNode == null || j == null) {
  208.             return;
  209.         }
  210.  
  211.         DLLNode<E> iFollowing = firstNode.succ;
  212.         DLLNode<E> jFollowing = j.succ;
  213.         DLLNode<E> iPrevious = firstNode.pred;
  214.         DLLNode<E> jPrevious = j.pred;
  215.  
  216.         j.succ = firstNode;
  217.         first = j;
  218.         firstNode.succ = jFollowing;
  219.  
  220.         jFollowing.pred = firstNode;
  221.         j.pred = null;
  222.         firstNode.pred = j;
  223.     }
  224.  
  225.     public void swapLastAndAdjacentDLLNodes(DLLNode<E> i, DLLNode<E> lastNode) {
  226.         if (i == null || lastNode == null) {
  227.             return;
  228.         }
  229.  
  230.         DLLNode<E> iFollowing = i.succ;
  231.         DLLNode<E> jFollowing = lastNode.succ;
  232.         DLLNode<E> iPrevious = i.pred;
  233.         DLLNode<E> jPrevious = lastNode.pred;
  234.  
  235.         i.succ = null;
  236.         lastNode.succ = i;
  237.         iPrevious.succ = lastNode;
  238.  
  239.         lastNode.pred = iPrevious;
  240.         i.pred = lastNode;
  241.         last = i;
  242.     }
  243.  
  244.     public DLLNode<E> get(int index) {
  245.         if (0 <= index && index < this.length()) {
  246.             DLLNode<E> temp = this.getFirst();
  247.             for (int i = 0; i < index; i++) {
  248.                 temp = temp.succ;
  249.             }
  250.             return temp;
  251.         } else {
  252.             return this.getFirst();
  253.         }
  254.     }
  255. }
  256.  
  257. /* Changed the class name from BubbleSortDLL to BubbleSortLL  */
  258. public class BubbleSortDLL {
  259.  
  260.     public static void bubbleSort(DLL<Integer> list) {
  261.         int length = list.length();
  262.         boolean sorted = false;
  263.  
  264.         for (int i = 0; i < length && !sorted; i++) {
  265.             sorted = true;
  266.  
  267.             for (DLLNode<Integer> j = list.getFirst(); j.succ != null; j = j.succ) {
  268.  
  269.                 if (j.element > j.succ.element) {
  270.  
  271.                     if (j == list.getFirst()) {
  272.                         list.swapFirstAndAdjacentDLLNodes(j, j.succ);
  273.                         sorted = false;
  274.                     } else if (j.succ == list.getLast()) {
  275.                         list.swapLastAndAdjacentDLLNodes(j, j.succ);
  276.                         j = j.pred;
  277.                         sorted = false;
  278.                     } else {
  279.                         list.swapAdjacentDLLNodes(j, j.succ);
  280.                         sorted = false;
  281.                     }
  282.                 }
  283.             }
  284.         }
  285.  
  286.         /* Print the List */
  287.         DLLNode<Integer> tmp = list.getFirst();
  288.         while (tmp != null) {
  289.             System.out.print(tmp.element);
  290.             if (tmp.succ != null)
  291.                 System.out.print(" ");
  292.             tmp = tmp.succ;
  293.         }
  294.  
  295.     }
  296.  
  297.     public static void main(String[] args) throws IOException {
  298.         DLL<Integer> list = new DLL<Integer>();
  299.  
  300.         Scanner scanner = new Scanner(System.in);
  301.  
  302.         int length = scanner.nextInt();
  303.         scanner.nextLine(); // Finish off the first line
  304.  
  305.         String line = scanner.nextLine();
  306.  
  307.         String parts[] = line.split(" ");
  308.  
  309.         for (int i = 0; i < length; i++) {
  310.             list.insertLast(Integer.parseInt(parts[i]));
  311.         }
  312.  
  313.         bubbleSort(list);
  314.  
  315.     }
  316.  
  317. }
Advertisement
Add Comment
Please, Sign In to add comment