Filip_Markoski

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

Nov 14th, 2017
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.48 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. class DLLNode<E> {
  6.     protected E element;
  7.     protected DLLNode<E> pred, succ;
  8.  
  9.     public DLLNode(E elem, DLLNode<E> pred, DLLNode<E> succ) {
  10.         this.element = elem;
  11.         this.pred = pred;
  12.         this.succ = succ;
  13.     }
  14.  
  15.     @Override
  16.     public String toString() {
  17.         return element.toString();
  18.     }
  19. }
  20.  
  21.  
  22. class DLL<E> {
  23.     private DLLNode<E> first, last;
  24.  
  25.     public DLL() {
  26.         // Construct an empty SLL
  27.         this.first = null;
  28.         this.last = null;
  29.     }
  30.  
  31.     public void deleteList() {
  32.         first = null;
  33.         last = null;
  34.     }
  35.  
  36.     public int length() {
  37.         int ret;
  38.         if (first != null) {
  39.             DLLNode<E> tmp = first;
  40.             ret = 1;
  41.             while (tmp.succ != null) {
  42.                 tmp = tmp.succ;
  43.                 ret++;
  44.             }
  45.             return ret;
  46.         } else
  47.             return 0;
  48.  
  49.     }
  50.  
  51.     public void insertFirst(E o) {
  52.         DLLNode<E> ins = new DLLNode<E>(o, null, first);
  53.         if (first == null)
  54.             last = ins;
  55.         else
  56.             first.pred = ins;
  57.         first = ins;
  58.     }
  59.  
  60.     public void insertLast(E o) {
  61.         if (first == null)
  62.             insertFirst(o);
  63.         else {
  64.             DLLNode<E> ins = new DLLNode<E>(o, last, null);
  65.             last.succ = ins;
  66.             last = ins;
  67.         }
  68.     }
  69.  
  70.     public void insertAfter(E o, DLLNode<E> after) {
  71.         if (after == last) {
  72.             insertLast(o);
  73.             return;
  74.         }
  75.         DLLNode<E> ins = new DLLNode<E>(o, after, after.succ);
  76.         after.succ.pred = ins;
  77.         after.succ = ins;
  78.     }
  79.  
  80.     public void insertBefore(E o, DLLNode<E> before) {
  81.         if (before == first) {
  82.             insertFirst(o);
  83.             return;
  84.         }
  85.         DLLNode<E> ins = new DLLNode<E>(o, before.pred, before);
  86.         before.pred.succ = ins;
  87.         before.pred = ins;
  88.     }
  89.  
  90.     public E deleteFirst() {
  91.         if (first != null) {
  92.             DLLNode<E> tmp = first;
  93.             first = first.succ;
  94.             first.pred = null;
  95.             if (first == null)
  96.                 last = null;
  97.             return tmp.element;
  98.         } else
  99.             return null;
  100.     }
  101.  
  102.     public E deleteLast() {
  103.         if (first != null) {
  104.             if (first.succ == null)
  105.                 return deleteFirst();
  106.             else {
  107.                 DLLNode<E> tmp = last;
  108.                 last = last.pred;
  109.                 last.succ = null;
  110.                 return tmp.element;
  111.             }
  112.         }
  113.         // else throw Exception
  114.         return null;
  115.     }
  116.  
  117.  
  118.     @Override
  119.     public String toString() {
  120.         String ret = new String();
  121.         if (first != null) {
  122.             DLLNode<E> tmp = first;
  123.             ret += tmp + "<->";
  124.             while (tmp.succ != null) {
  125.                 tmp = tmp.succ;
  126.                 ret += tmp + "<->";
  127.             }
  128.         } else
  129.             ret = "Prazna lista!!!";
  130.         return ret;
  131.     }
  132.  
  133.     public DLLNode<E> getFirst() {
  134.         return first;
  135.     }
  136.  
  137.     public DLLNode<E> getLast() {
  138.  
  139.         return last;
  140.     }
  141.  
  142. }
  143.  
  144. /* Changed the class name from BubbleSortDLL to BubbleSortLL  */
  145. public class BubbleSortDLL {
  146.  
  147.     public static void bubbleSort(DLL<Integer> list) {
  148.  
  149.         for (DLLNode<Integer> i = list.getFirst(); i != null; i = i.succ) {
  150.             for (DLLNode<Integer> j = i.succ; j != null; j = j.succ) {
  151.                 if (i.element > j.element) {
  152.                     int temp = i.element;
  153.                     i.element = j.element;
  154.                     j.element = temp;
  155.                 }
  156.             }
  157.         }
  158.  
  159.         /* Print the List */
  160.         DLLNode<Integer> tmp = list.getFirst();
  161.         while (tmp != null) {
  162.             System.out.print(tmp.element);
  163.             if (tmp.succ != null)
  164.                 System.out.print(" ");
  165.             tmp = tmp.succ;
  166.         }
  167.  
  168.     }
  169.  
  170.     public static void main(String[] args) throws IOException {
  171.         DLL<Integer> lista = new DLL<Integer>();
  172.         BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  173.         String s = stdin.readLine();
  174.         int N = Integer.parseInt(s);
  175.         s = stdin.readLine();
  176.         String[] pomniza = s.split(" ");
  177.         for (int i = 0; i < N; i++) {
  178.             lista.insertLast(Integer.parseInt(pomniza[i]));
  179.         }
  180.  
  181.         bubbleSort(lista);
  182.  
  183.     }
  184.  
  185. }
Advertisement
Add Comment
Please, Sign In to add comment