Filip_Markoski

[NP] Сортирана двојно поврзана листа

Nov 24th, 2017
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.17 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class SortedLinkedList<T extends Comparable<T>> {
  4.  
  5.     private class Node<T extends Comparable<T>> implements Comparable<Node<T>> {
  6.         T data;
  7.         Node<T> previous;
  8.         Node<T> next;
  9.  
  10.         public Node(T data, Node<T> previous, Node<T> next) {
  11.             this.data = data;
  12.             this.previous = previous;
  13.             this.next = next;
  14.         }
  15.  
  16.         @Override
  17.         public String toString() {
  18.             return data.toString();
  19.         }
  20.  
  21.         @Override
  22.         public int compareTo(Node<T> that) {
  23.             return this.data.compareTo(that.data);
  24.         }
  25.  
  26.         @Override
  27.         public boolean equals(Object o) {
  28.             if (this == o) return true;
  29.             if (o == null || getClass() != o.getClass()) return false;
  30.  
  31.             Node<?> node = (Node<?>) o;
  32.  
  33.             return data != null ? data.equals(node.data) : node.data == null;
  34.         }
  35.  
  36.         @Override
  37.         public int hashCode() {
  38.             return data != null ? data.hashCode() : 0;
  39.         }
  40.     }
  41.  
  42.     private class SortedLinkedListIterator<T extends Comparable<T>> implements Iterator<T> {
  43.  
  44.         private Node<T> previous, current, next;
  45.  
  46.         SortedLinkedListIterator() {
  47.             current = (Node<T>) first;
  48.             next = null;
  49.         }
  50.  
  51.         @Override
  52.         public boolean hasNext() {
  53.             return next != null;
  54.         }
  55.  
  56.         @Override
  57.         public T next() {
  58.             T data = current.data;
  59.             current = next;
  60.             next = next.next;
  61.             return data;
  62.         }
  63.     }
  64.  
  65.     private Node<T> first;
  66.     private Node<T> last;
  67.     private int size;
  68.  
  69.     SortedLinkedList() {
  70.         this.first = null;
  71.         this.last = null;
  72.         this.size = 0;
  73.     }
  74.  
  75.     public int size() {
  76.         return size;
  77.     }
  78.  
  79.     public boolean isEmpty() {
  80.         return size == 0;
  81.     }
  82.  
  83.     public void add(T element) {
  84.         /* Initial case*/
  85.         if (first == null) {
  86.             first = new Node<>(element, null, null);
  87.             last = first;
  88.             size++;
  89.         }
  90.         /* If element already exists, don't add it */
  91.         else if (!contains(element)) {
  92.            
  93.             Node<T> current = this.getFirst();
  94.             while (current.data.compareTo(element) < 0) {
  95.  
  96.                 /* If it's the biggest, add it at the end */
  97.                 if (current.next == null) {
  98.                    
  99.                     insertLast(element);
  100.                     size++;
  101.                     return;
  102.                 }
  103.                 current = current.next;
  104.             }
  105.  
  106.             /* The while moved us to a position where all left of current are less than current */
  107.             insertBefore(element, current);
  108.             size++;
  109.            
  110.         }
  111.     }
  112.  
  113.     public void insertFirst(T element) {
  114.         Node<T> insert = new Node<>(element, null, first);
  115.         if (first == null) {
  116.             last = insert;
  117.         } else {
  118.             first.previous = insert;
  119.         }
  120.         first = insert;
  121.     }
  122.  
  123.     public void insertLast(T element) {
  124.        
  125.         if (first == null) {
  126.             insertFirst(element);
  127.         } else {
  128.             Node<T> insert = new Node<>(element, last, null);
  129.             last.next = insert;
  130.             last = insert;
  131.         }
  132.     }
  133.  
  134.     public void insertAfter(T element, Node<T> after) {
  135.        
  136.         if (after == last) {
  137.             insertLast(element);
  138.             return;
  139.         }
  140.         Node<T> insert = new Node<>(element, after, after.next);
  141.         after.next.previous = insert;
  142.         after.next = insert;
  143.     }
  144.  
  145.     public void insertBefore(T element, Node<T> before) {
  146.        
  147.         if (before == first) {
  148.             insertFirst(element);
  149.             return;
  150.         }
  151.         Node<T> insert = new Node<>(element, before.previous, before);
  152.         before.previous.next = insert;
  153.         before.previous = insert;
  154.     }
  155.  
  156.     public T deleteFirst() {
  157.         if (first == null) return null;
  158.         Node<T> current = first;
  159.         first = first.next;
  160.         if (first != null) {
  161.             first.previous = null;
  162.         }
  163.         if (first == null) {
  164.             last = null;
  165.         }
  166.         return current.data;
  167.     }
  168.  
  169.     public T deleteLast() {
  170.         if (first == null) return null;
  171.         if (first.next == null) {
  172.             return deleteFirst();
  173.         } else {
  174.             Node<T> temp = last;
  175.             last = last.previous;
  176.             last.next = null;
  177.             return temp.data;
  178.         }
  179.     }
  180.  
  181.     public boolean remove(T element) {
  182.         Node<T> current = this.getFirst();
  183.         while (!current.data.equals(element)) {
  184.             if (current.next == null) return false;
  185.             current = current.next;
  186.         }
  187.         if (current == first) {
  188.             deleteFirst();
  189.         } else if (current == last) {
  190.             deleteLast();
  191.         } else {
  192.             current.previous.next = current.next;
  193.             current.next.previous = current.previous;
  194.         }
  195.         size--;
  196.         return true;
  197.     }
  198.  
  199.     public boolean contains(T element) {
  200.         Node<T> current = this.getFirst();
  201.         while (!current.data.equals(element)) {
  202.             if (current.next == null) {
  203.                 return false;
  204.             }
  205.             current = current.next;
  206.         }
  207.         return true;
  208.     }
  209.  
  210.     public Node<T> getFirst() {
  211.         return first;
  212.     }
  213.  
  214.     public Node<T> getLast() {
  215.         return last;
  216.     }
  217.  
  218.     public ArrayList<T> toArrayList() {
  219.         ArrayList<T> arrayList = new ArrayList<>(size);
  220.         Node<T> current = first;
  221.         while (current != null) {
  222.             arrayList.add(current.data);
  223.             current = current.next;
  224.         }
  225.         return arrayList;
  226.     }
  227.  
  228.     public void addAll(SortedLinkedList<? extends T> sortedLinkedList) {
  229.         Node<? extends T> current = (Node<? extends T>) sortedLinkedList.getFirst();
  230.         while (current != null) {
  231.             this.add(current.data);
  232.             current = current.next;
  233.         }
  234.         size += sortedLinkedList.size;
  235.     }
  236.  
  237.     public boolean containsAll(SortedLinkedList<? extends T> sortedLinkedList) {
  238.         Node<? extends T> current = (Node<? extends T>) sortedLinkedList.first;
  239.         while (current != null) {
  240.             if (!this.contains(current.data)) {
  241.                 return false;
  242.             }
  243.             current = current.next;
  244.         }
  245.         return true;
  246.     }
  247.  
  248.     @Override
  249.     public String toString() {
  250.         if (first == null) return "Empty List!";
  251.         StringBuffer sb = new StringBuffer();
  252.         Node<T> current = this.getFirst();
  253.         while (current != null) {
  254.             sb.append(current.data + " ");
  255.             current = current.next;
  256.         }
  257.         return sb.toString();
  258.     }
  259. }
  260.  
  261. public class SortedLinkedListTest {
  262.     public static void main(String[] args) {
  263.         Scanner jin = new Scanner(System.in);
  264.         int k = jin.nextInt();
  265.         System.out.println("Test#" + k);
  266.         System.out.print("testing SortedLinkedList::toArrayList():ArrayList<T> ,");
  267.         if (k == 0) {
  268.             System.out.println(" SortedLinkedList::add(T), SortedLinkedList::isEmpty():boolean , SortedLinkedList::remove(T):boolean , SortedLinkedList::size():int , T is Integer");
  269.  
  270.             SortedLinkedList<Integer> list = new SortedLinkedList<Integer>();
  271.             System.out.println("List is empty? " + list.isEmpty());
  272.             System.out.println("Adding elements:");
  273.             boolean flag = false;
  274.             while (jin.hasNextInt()) {
  275.                 System.out.print(flag ? " " : "");
  276.                 int i = jin.nextInt();
  277.                 list.add(i);
  278.                 System.out.print(i);
  279.                 flag = true;
  280.             }
  281.             System.out.println();
  282.             System.out.println("List size? " + list.size());
  283.             jin.next();
  284.             flag = false;
  285.             System.out.println("Removing elements:");
  286.             while (jin.hasNextInt()) {
  287.                 System.out.print(flag ? " " : "");
  288.                 int i = jin.nextInt();
  289.                 list.remove(i);
  290.                 System.out.print(i);
  291.                 flag = true;
  292.             }
  293.             System.out.println();
  294.             System.out.println("List size? " + list.size());
  295.             System.out.println("Final list: " + list.toArrayList());
  296.         }
  297.         if (k == 1) {
  298.             System.out.println(" SortedLinkedList::add(T) , T is Integer");
  299.             System.out.println("Adding elements:");
  300.             SortedLinkedList<Integer> list = new SortedLinkedList<Integer>();
  301.             boolean flag = false;
  302.             while (jin.hasNextInt()) {
  303.                 System.out.print(flag ? " " : "");
  304.                 int i = jin.nextInt();
  305.                 list.add(i);
  306.                 System.out.print(i);
  307.                 flag = true;
  308.             }
  309.             System.out.println();
  310.             System.out.print("Final list: ");
  311.             System.out.println(list.toArrayList());
  312.         }
  313.         if (k == 2) {
  314.             System.out.println(" SortedLinkedList::add(T) , SortedLinkedList::addAll(SortedLinkedList<? etends T>) , T is Integer");
  315.  
  316.             int num_testcases = jin.nextInt();
  317.             for (int w = 0; w < num_testcases; ++w) {
  318.                 System.out.println("Subtest #" + (w + 1));
  319.                 SortedLinkedList<Integer> list = new SortedLinkedList<Integer>();
  320.                 while (jin.hasNextInt()) {
  321.                     list.add(jin.nextInt());
  322.                 }
  323.                 SortedLinkedList<Integer> query = new SortedLinkedList<Integer>();
  324.                 jin.next();
  325.                 while (jin.hasNextInt()) {
  326.                     query.add(jin.nextInt());
  327.                 }
  328.                 System.out.println("List a=" + list.toArrayList());
  329.                 System.out.println("List b=" + query.toArrayList());
  330.                 list.addAll(query);
  331.                 System.out.println("Add all elements from b to a");
  332.                 System.out.println("List a=" + list.toArrayList());
  333.                 jin.next();
  334.             }
  335.         }
  336.         if (k == 3) {
  337.             System.out.println(" SortedLinkedList::add(T) , SortedLinkedList::containsAll(SortedLinkedList<? etends T>) , T is Integer");
  338.             int num_testcases = jin.nextInt();
  339.             for (int w = 0; w < num_testcases; ++w) {
  340.                 System.out.println("Subtest #" + (w + 1));
  341.                 SortedLinkedList<Integer> list = new SortedLinkedList<Integer>();
  342.                 while (jin.hasNextInt()) {
  343.                     list.add(jin.nextInt());
  344.                 }
  345.                 SortedLinkedList<Integer> query = new SortedLinkedList<Integer>();
  346.                 jin.next();
  347.                 while (jin.hasNextInt()) {
  348.                     query.add(jin.nextInt());
  349.                 }
  350.                 System.out.println("List a=" + list.toArrayList());
  351.                 System.out.println("List b=" + query.toArrayList());
  352.                 System.out.println("List a contains all elements in list b? " + list.containsAll(query));
  353.                 jin.next();
  354.             }
  355.         }
  356.         if (k == 4) {
  357.             System.out.println(" SortedLinkedList::add(T) , SortedLinkedList::remove(T):boolean , SortedLinkedList::contains(T) , T is String");
  358.             SortedLinkedList<String> list = new SortedLinkedList<String>();
  359.             TreeSet<String> control_list = new TreeSet<String>();
  360.             ArrayList<String> all = new ArrayList<String>();
  361.             all.add("Sample");
  362.             boolean same = true;
  363.             for (int i = 0; i < 1000; ++i) {
  364.                 double rand = Math.random();
  365.                 if (rand > 0.3) {
  366.                     String srand = randomString();
  367.                     if (Math.random() < 0.1) {
  368.                         srand = all.get((int) (Math.random() * all.size()));
  369.                     }
  370.                     control_list.add(srand);
  371.                     list.add(srand);
  372.                 }
  373.                 if (rand >= 0.3 && rand < 0.8) {
  374.                     String srand = randomString();
  375.                     if (Math.random() < 0.6) {
  376.                         srand = all.get((int) (Math.random() * all.size()));
  377.                     }
  378.                     same &= control_list.contains(srand) == list.contains(srand);
  379.                 }
  380.                 if (rand >= 0.8) {
  381.                     String srand = randomString();
  382.                     if (Math.random() < 0.8) {
  383.                         srand = all.get((int) (Math.random() * all.size()));
  384.                     }
  385.                     control_list.remove(srand);
  386.                     list.remove(srand);
  387.                 }
  388.             }
  389.             System.out.println("Your list outputs compared to the built in java structure were the same? " + same);
  390.  
  391.         }
  392.         if (k == 5) {
  393.             System.out.println(" SortedLinkedList::add(T) , SortedLinkedList::remove(T):boolean , T is Long");
  394.             int n = jin.nextInt();
  395.             SortedLinkedList<Long> list = new SortedLinkedList<Long>();
  396.             ArrayList<Long> all = new ArrayList<Long>();
  397.             all.add(684165189745L);
  398.             for (int i = 0; i < n; ++i) {
  399.                 double rand = Math.random();
  400.                 if (rand < 0.7) {
  401.                     Long srand = (long) (Math.random() * 45668948941984L);
  402.                     if (Math.random() < 0.1) {
  403.                         srand = all.get((int) (Math.random() * all.size()));
  404.                     }
  405.                     list.add(srand);
  406.                 }
  407.                 if (rand >= 0.7) {
  408.                     Long srand = (long) (Math.random() * 45668948941984L);
  409.                     if (Math.random() < 0.1) {
  410.                         srand = all.get((int) (Math.random() * all.size()));
  411.                     }
  412.                     list.remove(srand);
  413.                 }
  414.             }
  415.             System.out.println("Your program was really fast. You are a great developer!");
  416.         }
  417.     }
  418.  
  419.     private static String randomString() {
  420.         byte buf[] = new byte[(int) (Math.random() * 10) + 1];
  421.         new Random().nextBytes(buf);
  422.         return new String(buf);
  423.     }
  424.  
  425. }
Advertisement
Add Comment
Please, Sign In to add comment