Advertisement
Guest User

Списки

a guest
Jan 21st, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 24.75 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3.  
  4. // Листы и все, что с ними связанно
  5.  
  6. public class Main {
  7.     private static int FICT_NUMBER = -9999;
  8.  
  9.         public static void main(String[] args) {
  10.            
  11.            
  12.             System.out.println("DL Lists");
  13.             System.out.println("List 1 an 0");
  14.             DLNode some = CreateDL(10, 0, 1);
  15.             printList(some);
  16.             some = order(some);
  17.             System.out.println("Orderind");
  18.             printList(some);
  19.             System.out.println("List from 10 to -10");
  20.             some = CreateDL(50, -10, 10);
  21.             some = makeFictList(some);
  22.             printList(some);
  23.            
  24.             some = shift(some, 23);
  25.             System.out.println("Shift");
  26.            
  27.            
  28.             DLNode ff = createDLNode(1);
  29.             DLNode neww = createDLNode(2);
  30.             ff = addFirst(ff, neww);
  31.             neww = createDLNode(2);
  32.             ff = addFirst(ff, neww);
  33.             neww = createDLNode(3);
  34.             ff = addFirst(ff, neww);
  35.             neww = createDLNode(4);
  36.             ff = addFirst(ff, neww);
  37.             neww = createDLNode(2);
  38.             ff = addFirst(ff, neww);
  39.             neww = createDLNode(5);
  40.             ff = addFirst(ff, neww);
  41.             neww = createDLNode(5);
  42.             ff = addFirst(ff, neww);
  43.             neww = createDLNode(5);
  44.             ff = addFirst(ff, neww);
  45.             neww = createDLNode(5);
  46.             ff = addFirst(ff, neww);
  47.             neww = createDLNode(2);
  48.             ff = addFirst(ff, neww);
  49.             neww = createDLNode(5);
  50.             ff = addFirst(ff, neww);
  51.             neww = createDLNode(5);
  52.             ff = addFirst(ff, neww);
  53.             neww = createDLNode(5);
  54.             ff = addFirst(ff, neww);
  55.             printList(ff);
  56.             int nn = NumBetween(ff, 2);
  57.             System.out.println("Num between 2 is " + nn);
  58. //          some = deleteBetween(some, 1);
  59. //          System.out.println("Delete between");
  60.             //printList(some);
  61. //          System.out.println("Delete Duplication");
  62.             some = deleteDuplicates(some);
  63. //          printList(some);
  64. //          System.out.println("Sorting");
  65. //          some = selectionSort(some);
  66. //          printList(some);
  67. //          System.out.println("Revers");
  68. //          some = DelFict(some);
  69. //          some = reverseList(some);
  70. //          printList(some);
  71. //          System.out.println("Delete not prime");
  72. //          some = delNotPrime(some);
  73. //          printList(some);
  74. //          System.out.println("Print without negative");
  75. //          printWithout(some);
  76. //          System.out.println("Delete Mid");
  77. //          some = DelMid(some);
  78. //          printList(some);
  79. //          SLNode newList = createSecondList(some, 6);
  80.            
  81.            
  82.             System.out.println("------------------------");
  83.             System.out.println("SLList is number");
  84.             int num = 13;
  85.             SLNode n = IntToList(num);
  86.             System.out.println("Number is " + num);
  87.             printList(n);
  88.             int num2 = 9999;
  89.             SLNode m = IntToList(num2);
  90.             System.out.println("Number is " + num2);
  91.             printList(m);
  92.            
  93.             SLNode sum = Sum(m, n);
  94.             System.out.println("Sum is " + (num + num2));
  95.             printList(sum);
  96.            
  97.             long res = twoBase(num);
  98.             System.out.println("Numer " + num + " in two base: " + res);
  99.            
  100.            
  101.            
  102.            
  103.             System.out.println("------------------------");
  104.             int x = 99223323;
  105.             SLNodeChar head = ChengeBase(x, 10);
  106.             System.out.println("Number " + x + " in 16-BASE");
  107.             printList(head);
  108.             head = MakeCircle(head);
  109.            
  110.             head = DeleteBetween(head, '2');
  111.             head = DelCircle(head);
  112.             printList(head);
  113.            
  114.            
  115.             System.out.println("------------------------");
  116.             System.out.println("SL Lists");
  117.             SLNode list = CreateSL(11, -10, 10);
  118.             System.out.println("List from 10 to -10");
  119.             printList(list);
  120.             list = deltaAfter(list);
  121.             System.out.println("Delta after");
  122.             printList(list);
  123.            
  124.             System.out.println("Sorting");
  125.             list = selectionSort(list);
  126.             printList(list);
  127.            
  128.             System.out.println("Fib");
  129.             SLNode fib = Fib(100);
  130.             printList(fib);
  131. //          System.out.println("Print without negative");
  132. //          printWithout(list);
  133. //          System.out.println("Delete Mid");
  134. //          list = DelMid(list);
  135. //          printList(list);
  136.  
  137.         }
  138.        
  139. //ПРОСТЕ ЧИСЛО
  140.                 public static boolean Prime(int val) {
  141.                     if(val <= 1) return false;
  142.                     boolean isPrime = true;
  143.                     for(int i = 2; i <= Math.sqrt(val) && isPrime; i++) {
  144.                         if(val % i == 0) isPrime = false;
  145.                     }
  146.                     return isPrime;
  147.                 }
  148.                
  149. //ДВОИЧНАЯ СИСТЕМА
  150.                 public static long twoBase(int ten) {
  151.                     if(ten == 0) return 0;
  152.                     int isNegative = 1;
  153.                     if(ten < 0) {
  154.                         isNegative = -1;
  155.                         ten = ten *isNegative;
  156.                     }
  157.                        SLNode head = null;
  158.                        while(ten != 0) {
  159.                            SLNode node = createSLNode(ten % 2);
  160.                            head = addFirst(head, node);
  161.                            ten = ten / 2;
  162.                        }
  163.                      long res = 0;
  164.                      while(head != null) {
  165.                          res = res * 10 + head.data;
  166.                          head = head.next;
  167.                      }
  168.                      return res * isNegative;
  169.                 }
  170.                
  171. //--------------------------------------------------------------------
  172. //                          SLList
  173. //--------------------------------------------------------------------
  174.        
  175. //Принт списка
  176.                 private static void printList(SLNode list) {
  177.                     if(list == null) {
  178.                         System.out.println("Your list is empty.");
  179.                     }else {
  180.                         while(list != null) {
  181.                             System.out.printf(list.data + "   ");
  182.                             list = list.next;
  183.                         }
  184.                     }
  185.                     System.out.println("");
  186.                 }
  187. // Принт без отрцательных
  188.                 private static void printWithout(SLNode list) {
  189.                     if(list == null || list.data == FICT_NUMBER) {
  190.                         System.out.println("Your list is empty.");
  191.                     }else {
  192.                         while(list != null && list.data != FICT_NUMBER) {
  193.                             if(list.data >= 0)
  194.                             System.out.printf(list.data + "   ");
  195.                             list = list.next;
  196.                         }
  197.                     }
  198.                     System.out.println("");
  199.                 }
  200. //Создание и заполнение списка рандомными числами
  201.                 private static SLNode CreateSL(int len, int from, int to) {
  202.                     SLNode head = null;
  203.                     SLNode tail = null;
  204.                     Random rand = new Random();
  205.                     for (int i = 0; i < len; i++) {
  206.                         SLNode node = createSLNode(rand.nextInt(to - from + 1) + from);
  207.                         if(head == null || tail == null) {
  208.                             head = node;
  209.                             tail = node;
  210.                         }
  211.                         else {
  212.                             tail.next = node;
  213.                             tail = node;
  214.                         }
  215.                     }
  216.                     return head;
  217.                 }
  218. //Просто создание узла
  219.                 private static SLNode createSLNode(int data) {
  220.                     SLNode newNode = new SLNode();
  221.                     newNode.data = data;
  222.                     newNode.next = null;
  223.                     return newNode;
  224.  
  225.                 }
  226.  
  227. //Из двух упорядоченных получить один упорядоченный
  228.                 public static SLNode mergeTwoLists(SLNode l1, SLNode l2) {
  229.                     if (l1 == null) {
  230.                         return l2;
  231.                     }
  232.                     else if (l2 == null) {
  233.                         return l1;
  234.                     }
  235.                     else if (l1.data < l2.data) {
  236.                         l1.next = mergeTwoLists(l1.next, l2);
  237.                         return l1;
  238.                     }
  239.                     else {
  240.                         l2.next = mergeTwoLists(l1, l2.next);
  241.                         return l2;
  242.                     }
  243.                    
  244.                 }
  245.        
  246. //Отнять от каждого элемента минимальный среди последующих
  247.                 public static SLNode deltaAfter(SLNode head) {
  248.                     if (head == null)
  249.                         return head;
  250.                     SLNode node = head;
  251.                     while(node.next != null) {
  252.                         SLNode min = minNode(node.next);
  253.                         node.data -= min.data;
  254.                         node = node.next;
  255.                     }
  256.                     return head;
  257.                 }
  258. //Реверс списка
  259.                     public static SLNode reverseList(SLNode head) {
  260.                         SLNode prev = null;
  261.                         SLNode curr = head;
  262.                         while (curr != null) {
  263.                             SLNode nextTemp = curr.next;
  264.                             curr.next = prev;
  265.                             prev = curr;
  266.                             curr = nextTemp;
  267.                         }
  268.                         return prev;
  269.                     }
  270.        
  271. //Сортировка
  272.                 private static SLNode selectionSort(SLNode h) {
  273.                     SLNode s = null;
  274.                     while (h != null) {
  275.                         SLNode max = maxNode(h);
  276.                         h = remove(h, max);
  277.                         max.next = null;
  278.                         s = addFirst(s, max);
  279.                     }
  280.                     return s;
  281.                 }
  282.                
  283.        
  284.  //Поиск максимальной и минимальной Ноды
  285.             public static SLNode maxNode(SLNode h) {
  286.                 if (h == null)
  287.                     return h;
  288.                 else {
  289.                     SLNode max = h;
  290.                     SLNode cur = h;
  291.                     while (cur != null) {
  292.                         if (cur.data > max.data) {
  293.                             max = cur;
  294.                         }
  295.                         cur = cur.next;
  296.                     }
  297.                     return max;
  298.                 }
  299.             }
  300.            
  301.             public static SLNode minNode(SLNode h) {
  302.                 if (h == null)
  303.                     return h;
  304.                 else {
  305.                     SLNode min = h;
  306.                     SLNode cur = h;
  307.                     while (cur != null) {
  308.                         if (cur.data < min.data) {
  309.                             min = cur;
  310.                         }
  311.                         cur = cur.next;
  312.                     }
  313.                     return min;
  314.                 }
  315.             }
  316.        
  317.  //Удаление элемента
  318.                     public static SLNode remove(SLNode h, SLNode x) {
  319.                         if(h == null || x == null) return h;
  320.                         if(h == x) {
  321.                             return h.next;
  322.                         }
  323.                         SLNode pres = h;
  324.                         while(pres != null && pres.next != x) {
  325.                             pres = pres.next;
  326.                         }
  327.                         if(pres == null) return h;
  328.                         pres.next = pres.next.next;
  329.                         return h;
  330.                     }
  331.  
  332. //Добавление в начало
  333.                 public static SLNode addFirst(SLNode h, SLNode newNode) {
  334.                     if(newNode == null) {
  335.                         return h;
  336.                     }
  337.                     newNode.next = h;
  338.                     return newNode;
  339.                 }
  340.                
  341.  
  342. //Поиск хвостика
  343.                 public static SLNode tailSearch(SLNode h) {
  344.                     if (h == null)
  345.                         return h;
  346.                     SLNode t = h;
  347.                     while(t.next != null) {
  348.                         t = t.next;
  349.                     }
  350.                     return t;
  351.                 }
  352. //Добавление после максимального
  353.                 private static SLNode addAfterMAX(SLNode head, SLNode node) {
  354.                     if(head == null) {
  355.                         return node;
  356.                     }
  357.                     if(node == null) {
  358.                         return head;
  359.                     }
  360.                     SLNode max = maxNode(head);
  361.                     node.next = max.next;
  362.                     max.next = node;
  363.                     return head;
  364.                 }
  365. // Число -> список
  366.                 public static SLNode IntToList(int val) {
  367.                    if(val < 0) return null;
  368.                    SLNode head = null;
  369.                    do {
  370.                        SLNode node = createSLNode(val % 10);
  371.                        head = addFirst(head, node);
  372.                        val = val / 10;
  373.                    }while(val != 0);
  374.                    return head;
  375.                 }
  376.                
  377. // Список -> число
  378.                 public static int ListToInt(SLNode val) {
  379.                     int res = 0;
  380.                      while(val != null) {
  381.                          res = res * 10 + val.data;
  382.                          val = val.next;
  383.                      }
  384.                    return res;
  385.                 }
  386.                
  387. //Удаление центральной ноды
  388.                 public static SLNode DelMid(SLNode head) {
  389.                     if(head ==null) return head;
  390.                    
  391.                     SLNode mid = head;
  392.                     SLNode tail = head;
  393.                     while(tail.next != null && tail.next.next != null) {
  394.                         mid = mid.next;
  395.                         tail = tail.next.next;
  396.                     }
  397.                     if(tail.next == null) {
  398.                         head = remove(head, mid);
  399.                     }
  400.                     return head;
  401.                 }
  402.        
  403. //Сума чисел
  404.                 public static SLNode Sum(SLNode l1, SLNode l2) {
  405.                     int one = ListToInt(l1);
  406.                     int two = ListToInt(l2);
  407.                     SLNode res = IntToList(one + two);
  408.                     return res;
  409.                 }
  410.                
  411.                
  412. //Фибоначи в список, плиз
  413.                 public static SLNode Fib(int N) {
  414.                     if(N < 0) return null;
  415.                     int l = 1;
  416.                     int h = 1;
  417.                     SLNode res = createSLNode(l);
  418.                     while(h <= N) {
  419.                         h = l + h;
  420.                         l = h - l;
  421.                         SLNode node = createSLNode(l);
  422.                         res = addFirst(res, node);
  423.                     }
  424.                     return res;
  425.                 }
  426.                
  427.                
  428. //-----------------------------------------------------------------
  429. //                  SLList CHAR
  430. //-----------------------------------------------------------------        
  431.                
  432.                 private static SLNodeChar createSLNode(char data) {
  433.                     SLNodeChar newNode = new SLNodeChar();
  434.                     newNode.data = data;
  435.                     newNode.next = null;
  436.                     return newNode;
  437.  
  438.                 }
  439.                 public static SLNodeChar addFirst(SLNodeChar h, SLNodeChar newNode) {
  440.                     if(newNode == null) {
  441.                         return h;
  442.                     }
  443.                     newNode.next = h;
  444.                     return newNode;
  445.                 }
  446.                
  447.                 private static void printList(SLNodeChar list) {
  448.                     if(list == null) {
  449.                         System.out.println("Your list is empty.");
  450.                     }else {
  451.                         while(list != null) {
  452.                             System.out.printf(list.data + " ");
  453.                             list = list.next;
  454.                         }
  455.                     }
  456.                     System.out.println("");
  457.                 }
  458. //Число в 16-ричной
  459.                public static SLNodeChar ChengeBase(int x, int base) {
  460.                     char [] systemElements = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  461.                             'A','B','C','D','E','F','G','H','I',
  462.                             'J','K','L','M','N','O','P','Q',
  463.                             'R','S','T','U','V','W','X','Y','Z'};
  464.                     int isNegative = 1;
  465.                     if(x < 0) isNegative = -1;
  466.                     x = x * isNegative;
  467.                     SLNodeChar head = null;
  468.                     do {
  469.                         SLNodeChar node = createSLNode(systemElements[x % base]);
  470.                         head = addFirst(head, node);
  471.                         x = x / base;
  472.                     }while (x != 0);
  473.                     if(isNegative == -1) {
  474.                         SLNodeChar node = createSLNode('-');
  475.                         head = addFirst(head, node);
  476.                     }
  477.                     return head;
  478.                 }
  479. //Делаем кольцо
  480.                public static SLNodeChar MakeCircle(SLNodeChar head) {
  481.                    SLNodeChar tail = head; 
  482.                        while(tail.next != null){
  483.                            tail = tail.next;
  484.                    }
  485.                        tail.next = head;
  486.                    
  487.                     return head;
  488.                 }
  489. //удаляем кольцо  
  490.                public static SLNodeChar DelCircle(SLNodeChar head) {
  491.                    SLNodeChar tail = head; 
  492.                        while(tail.next != head){
  493.                            tail = tail.next;
  494.                    }
  495.                        tail.next = null;
  496.                    
  497.                     return head;
  498.                 }
  499. //Удаляем в кольце между двумя вхождениями    
  500.                public static SLNodeChar DeleteBetween(SLNodeChar head, char X) {
  501.                    SLNodeChar searcher = head; 
  502.                    //First
  503.                    SLNodeChar f = null;
  504.                    if(head.data == X) f = head;
  505.                    else {
  506.                        searcher = searcher.next;
  507.                        while(searcher != head && f == null){
  508.                            if(searcher.data == X) f = searcher;
  509.                            searcher = searcher.next;
  510.                        }
  511.                    }
  512.                  //Second
  513.                    SLNodeChar s = null;
  514.                    if(f != null) {
  515.                            while(searcher != head){
  516.                            if(searcher.data == X) {
  517.                                s = searcher;
  518.                            }
  519.                            searcher = searcher.next;
  520.                        }
  521.                        if(s != null)
  522.                            f.next = s;
  523.                    }
  524.                    
  525.                    
  526.                    
  527.                     return head;
  528.                 }
  529.  
  530. //-----------------------------------------------------------------
  531. //                            DLList
  532. //-----------------------------------------------------------------
  533.        
  534. //Принт списка
  535.                 private static void printList(DLNode list) {
  536.                     if(list == null || list.data == FICT_NUMBER) {
  537.                         System.out.println("Your list is empty.");
  538.                     }else {
  539.                         while(list != null && list.data != FICT_NUMBER) {
  540.                             System.out.printf(list.data + "   ");
  541.                             list = list.next;
  542.                         }
  543.                     }
  544.                     System.out.println("");
  545.                 }
  546. // Принт без отрцательных
  547.                 private static void printWithout(DLNode list) {
  548.                     if(list == null || list.data == FICT_NUMBER) {
  549.                         System.out.println("Your list is empty.");
  550.                     }else {
  551.                         while(list != null && list.data != FICT_NUMBER) {
  552.                             if(list.data >= 0)
  553.                             System.out.printf(list.data + "   ");
  554.                             list = list.next;
  555.                         }
  556.                     }
  557.                     System.out.println("");
  558.                 }
  559.        
  560. //Создание и заполнение списка рандомом
  561.                 private static DLNode CreateDL(int len, int from, int to) {
  562.                     DLNode head = null;
  563.                     DLNode tail = null;
  564.                     Random rand = new Random();
  565.                     for (int i = 0; i < len; i++) {
  566.                         DLNode node = createDLNode(rand.nextInt(to - from + 1) + from);
  567.                         if(head == null || tail == null) {
  568.                             head = node;
  569.                             tail = node;
  570.                         }
  571.                         else {
  572.                             tail.next = node;
  573.                             node.prev = tail;
  574.                             tail = node;
  575.                         }
  576.                     }
  577.                     return head;
  578.                 }
  579. //Просто создание узла
  580.                 private static DLNode createDLNode(int data) {
  581.                     DLNode newNode = new DLNode();
  582.                     newNode.data = data;
  583.                     newNode.next = null;
  584.                     newNode.prev = null;
  585.                     return newNode;
  586.  
  587.                 }
  588.                
  589. //Удаление элементов между первым и последнив вхождением ЧИСЛА Х (НЕ РАБОТАЕТ)
  590.                 private static DLNode deleteBetween(DLNode head, int X) {
  591.                     if(head == null) return head;
  592.                     head = DelFict(head);
  593.                     DLNode first = head;
  594.                     DLNode last = tailSearch(head);
  595.                     while(first != last && first.data != X){
  596.                         first = first.next;
  597.                     }
  598.                     while(first != last && last.data != X) {
  599.                         last = last.prev;
  600.                     }
  601.                     if(first == last) return head;
  602.                     first.next = last;
  603.                     last.next = first;
  604.                     return head;
  605.  
  606.                 }
  607. //Удаление повторяющихся элементов из несортерованного списка
  608.                 public static DLNode deleteDuplicates(DLNode head) {
  609.                     head = selectionSort(head);
  610.                     head = DelFict(head);
  611.                     DLNode current = head;
  612.                     while (current != null && current.next != null) {
  613.                         if (current.next.data == current.data) {
  614.                             current.next = current.next.next;
  615.                         } else {
  616.                             current = current.next;
  617.                         }
  618.                     }
  619.                     head = makeFictList(head);
  620.                     return head;
  621.                 }
  622.  
  623. //Добавление в конец и в начало
  624.         private static DLNode addInTheEnd(DLNode head, DLNode node) {
  625.             if(head == null) {
  626.                 return node;
  627.             }
  628.             if(node == null) {
  629.                 return head;
  630.             }
  631.             DLNode tail = head;
  632.             while(tail.next != null) {
  633.                 tail = tail.next;
  634.             }
  635.             tail.next = node;
  636.             node.prev = tail;
  637.             return head;
  638.         }
  639.        
  640.         public static DLNode addFirst(DLNode h, DLNode newNode) {
  641.             if(newNode == null) return h;
  642.            
  643.             newNode.prev = null;
  644.             newNode.next = h;
  645.             if (h != null) {
  646.                 h.prev = newNode;
  647.             }
  648.             return newNode;
  649.         }
  650.  
  651. // 0 and 1
  652.         private static DLNode order(DLNode head) {
  653.             DLNode pres = head;
  654.             DLNode zero = null;
  655.             DLNode one = null;
  656.             DLNode tmp = null;
  657.             while(pres != null) {
  658.                     //Delete
  659.                     if (pres.next != null) {
  660.                         pres.next.prev = pres.prev;
  661.                     }
  662.                     tmp = pres;
  663.                     //Move
  664.                     pres = pres.next;
  665.                     tmp.next = null;
  666.                     //Add in the end
  667.                     if(tmp.data == 0) {
  668.                         zero = addInTheEnd(zero, tmp);
  669.                     }
  670.                     else {
  671.                         one = addInTheEnd(one, tmp);
  672.                     }
  673.             }
  674.             //Connection
  675.             zero = addInTheEnd(zero, one);
  676.             return zero;
  677.         }
  678.        
  679.  
  680.        
  681.        
  682. //  Selection sort
  683.         private static DLNode selectionSort(DLNode h) {
  684.            
  685.             DLNode s = null; // head of sorted list
  686.             //Гениально...
  687.             h = DelFict(h);
  688.             while (h != null) {
  689.                
  690.                 // find the node with maximum value in the unsorted list
  691.                 DLNode max = maxNode(h);
  692.                 // remove the node with maximum value from unsorted list
  693.                 h = remove(h, max);
  694.                 // insert the node with maximum value at the front of sorted list
  695.                 s = addFirst(s, max);
  696.             }
  697.             s = makeFictList(s);
  698.             return s;
  699.         }
  700. //Создание фиктивного узла
  701.         public static DLNode makeFictList(DLNode h) {
  702.             DLNode fict = createDLNode(FICT_NUMBER);
  703.             DLNode tail = tailSearch(h);
  704.             tail.next = fict;
  705.             fict.prev = tail;
  706.             h.prev = fict;
  707.             fict.next = h;
  708.             return h;
  709.         }
  710. //Удаление фиктивного узла
  711.         public static DLNode DelFict(DLNode h) {
  712.             if(h == null) return h;
  713.             if(h.prev == null) return h;
  714.             h.prev.prev.next = null;
  715.             h.prev = null;
  716.             return h;
  717.         }
  718. //Поиск хвостика
  719.         public static DLNode tailSearch(DLNode h) {
  720.             if (h == null)
  721.                 return h;
  722.             DLNode t = h;
  723.             while(t.next != null) {
  724.                 t = t.next;
  725.             }
  726.             return t;
  727.         }
  728. //Поиски максимального элемента
  729.         public static DLNode maxNode(DLNode h) {
  730.             if (h == null || h.data == FICT_NUMBER)
  731.                 return h;
  732.             else {
  733.                 DLNode max = h;
  734.                 DLNode cur = h;
  735.                 while (cur != null && cur.data != FICT_NUMBER) {
  736.                     if (cur.data > max.data) {
  737.                         max = cur;
  738.                     }
  739.                     cur = cur.next;
  740.                 }
  741.                 return max;
  742.             }
  743.         }
  744. //Удаление элемента
  745.         public static DLNode remove(DLNode h, DLNode x) {
  746.             if(h == null) return h;
  747.             // check if x is the head
  748.             if (x.prev != null) {
  749.                 x.prev.next = x.next;
  750.             } else {
  751.                 h = h.next;
  752.             }
  753.             // check if x is the tail
  754.             if (x.next != null) {
  755.                 x.next.prev = x.prev;
  756.             }
  757.             return h;
  758.         }
  759.        
  760. //Реверс
  761.         public static DLNode reverseList(DLNode head) {
  762.             DLNode prev = null;
  763.             DLNode curr = head;
  764.             while (head != null) {
  765.                 DLNode nextTemp = curr.next;
  766.                 head = remove(head, curr);
  767.                 prev = addFirst(prev, curr);
  768.                 curr = nextTemp;
  769.             }
  770.             return prev;
  771.         }
  772.        
  773. // Удаление всех непростых чисел     
  774.         public static DLNode delNotPrime(DLNode head) {
  775.             DLNode del = null;
  776.             DLNode pres = head;
  777.             while (pres != null) {
  778.                 if(!Prime(pres.data)) {
  779.                     del = pres;
  780.                     pres = pres.next;
  781.                     head = remove(head, del);
  782.                 }
  783.                 else {
  784.                     pres = pres.next;
  785.                 }
  786.             }
  787.             return head;
  788.         }
  789.        
  790. //Удаление центральной ноды
  791.         public static DLNode DelMid(DLNode head) {
  792.             if(head ==null) return head;
  793.            
  794.             DLNode mid = head;
  795.             DLNode tail = head;
  796.             while(tail.next != null && tail.next.next != null) {
  797.                 mid = mid.next;
  798.                 tail = tail.next.next;
  799.             }
  800.             if(tail.next == null) {
  801.                 head = remove(head, mid);
  802.             }
  803.             return head;
  804.         }
  805.        
  806. //Сдвиг списка на Н вправо
  807.         public static DLNode shift(DLNode head, int X) {
  808.             if(head ==null) return head;
  809.             DLNode fict = head.prev;
  810.             head.prev = head.prev.prev;
  811.             head.prev.next = head;
  812.             if(X > 0)
  813.             for(int i = 0; i < X; i++) {
  814.                 head = head.next;
  815.             }
  816.             else
  817.                 for(int i = 0; i > X; i--) {
  818.                     head = head.prev;
  819.                 }
  820.             fict.next = head;
  821.             fict.prev = head.prev;
  822.             head.prev.next = fict;
  823.             head.prev = fict;
  824.             return head;
  825.         }
  826.  
  827.        
  828.         public static int NumBetween(DLNode head, int X) {
  829.             int res = 0;
  830.             int counter = 0;
  831.             DLNode searcher = head;
  832.                //First
  833.                 DLNode f = null;
  834.                if(head.data == X) f = head;
  835.                else {
  836.                    searcher = searcher.next;
  837.                    while(searcher != null && f == null){
  838.                        if(searcher.data == X) f = searcher;
  839.                        searcher = searcher.next;
  840.                    }
  841.                }
  842.                
  843.                if(f != null) {
  844.                        while(searcher != null){
  845.                        if(searcher.data == X) {
  846.                            res += counter;
  847.                            counter = 0;
  848.                        }
  849.                        counter++;
  850.                        searcher = searcher.next;
  851.                    }
  852.                }
  853.                return res;
  854.                
  855.         }
  856. //Удаление из двонаправленного и вставка в однонаправленный (меньшиз, чем икс)
  857. //НУЖНО СДЕЛАТЬ ГЛОБАЛЬНУЮ ПЕРЕМЕННУЮ, ЧТОБ ОНО РАБОТАЛО
  858. //ИЛИ ВКЛЮЧИТЬ ВИЗУАЛИЗАЦИЮ
  859.         private static SLNode createSecondList(DLNode dlHead, int x) {
  860. //Визуализация
  861.             System.out.println("Delete all nodes less then " + x);
  862.             System.out.println("Before");
  863.             printList(dlHead);
  864. //Конец Визуализации
  865.            
  866.             SLNode newNode = null;
  867.             SLNode slHead = null;
  868.             DLNode present = dlHead;
  869.             while(present != null) {
  870.                 if(present.data < x) {
  871.                     newNode = createSLNode(present.data);  
  872.                     slHead = addFirst(slHead, newNode);
  873.                     dlHead = remove(dlHead, present);
  874.                 }
  875.                 present = present.next;
  876.             }
  877.            
  878. //Визуализация
  879.             System.out.println("After");
  880.             printList(dlHead);
  881.             System.out.println("New List");
  882.             printList(slHead);
  883. //Конец Визуализации
  884.            
  885.             return slHead;
  886.         }
  887.  
  888.  
  889.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement