Advertisement
metalni

APS Labs 5 Sortiranje so meurcinja kaj lista

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