atanasovetr

Untitled

Jun 24th, 2021
540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.36 KB | None | 0 0
  1. public class LinkedList <Т>{
  2.     Node head;
  3.  
  4.     void addElementAtBegining(Т el) {
  5.         Node n = new Node(el);
  6.         n.next = head;
  7.         head = n;
  8.     }
  9.  
  10.     void addElementAtEnd(Т el) {
  11.         Node n = new Node(el);
  12.  
  13.         Node current = head;
  14.         while(current.next != null) {
  15.             current = current.next;
  16.         }
  17.         current.next = n;
  18.     }
  19.  
  20.     void addElementWithIndex(int index, Т el) {
  21.         if(index > this.size()-1){
  22.             return;
  23.         }
  24.         int counter = 0;
  25.         Node n = new Node(el);
  26.  
  27.         Node current = head;
  28.  
  29.         while(counter+1 != index) {
  30.             current = current.next;
  31.             counter++;
  32.  
  33.         }
  34.         n.next = current.next;
  35.         current.next = n;
  36.  
  37.     }
  38.  
  39.     void deleteWithIndex(int index) {
  40.         int counter = 0;
  41.  
  42.         Node current = head;
  43.  
  44.         while(counter+1 != index) {
  45.             current = current.next;
  46.             counter++;
  47.  
  48.         }
  49.         current.next = current.next.next;
  50.     }
  51.  
  52.     Node sortedMerge(Node a, Node b)
  53.     {
  54.         Node result = null;
  55.  
  56.         if (a == null)
  57.             return b;
  58.         if (b == null)
  59.             return a;
  60.  
  61.         if ((int)a.data <= (int)b.data) {
  62.             result = a;
  63.             result.next = sortedMerge(a.next, b);
  64.         }
  65.         else {
  66.             result = b;
  67.             result.next = sortedMerge(a, b.next);
  68.         }
  69.         return result;
  70.     }
  71.  
  72.     Node mergeSort(Node h)
  73.     {
  74.         if (h == null || h.next == null) {
  75.             return h;
  76.         }
  77.  
  78.         Node middle = getMiddle(h);
  79.         Node nextofmiddle = middle.next;
  80.  
  81.         middle.next = null;
  82.  
  83.         Node left = mergeSort(h);
  84.  
  85.         Node right = mergeSort(nextofmiddle);
  86.  
  87.         Node sortedlist = sortedMerge(left, right);
  88.         return sortedlist;
  89.     }
  90.  
  91.     public static Node getMiddle(Node head)
  92.     {
  93.         if (head == null)
  94.             return head;
  95.  
  96.         Node slow = head, fast = head;
  97.  
  98.         while (fast.next != null && fast.next.next != null) {
  99.             slow = slow.next;
  100.             fast = fast.next.next;
  101.         }
  102.         return slow;
  103.     }
  104.  
  105.     LinkedList[] split() {
  106.         LinkedList[] splittedArr = new LinkedList[2];
  107.         int counter = 0;
  108.         Node current = head;
  109.         splittedArr[0] = new LinkedList();
  110.         splittedArr[0].addElementAtBegining(current.data);
  111.  
  112.         while(counter+1 != this.size()/2) {
  113.             splittedArr[0].addElementAtEnd(current.next.data);
  114.             current = current.next;
  115.             counter++;
  116.         }
  117.         current = current.next;
  118.         splittedArr[1] = new LinkedList();
  119.         splittedArr[1].addElementAtBegining(current.data);
  120.         while(counter+2 != this.size()) {
  121.             splittedArr[1].addElementAtEnd(current.next.data);
  122.             current = current.next;
  123.             counter++;
  124.         }
  125.         return splittedArr;
  126.  
  127.     }
  128.     int size() {
  129.         int counter = 1;
  130.         Node current = head;
  131.         while(current.next != null) {
  132.             current = current.next;
  133.             counter++;
  134.         }
  135.         return counter;
  136.     }
  137.     public void print() {
  138.         Node current = head;
  139.         while(current != null) {
  140.             System.out.print(current.data + " ");
  141.             current = current.next;
  142.         }
  143.         System.out.println();
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment