Filip_Markoski

[ADS] Playful List

Nov 9th, 2017
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.25 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.StringTokenizer;
  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 extends Comparable<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 DLLNode<E> find(E o) {
  52.         if (first != null) {
  53.             DLLNode<E> tmp = first;
  54.             while (tmp.element != o && tmp.succ != null)
  55.                 tmp = tmp.succ;
  56.             if (tmp.element == o) {
  57.                 return tmp;
  58.             } else {
  59.                 System.out.println("Elementot ne postoi vo listata");
  60.             }
  61.         } else {
  62.             System.out.println("Listata e prazna");
  63.         }
  64.         return first;
  65.     }
  66.  
  67.     public void insertFirst(E o) {
  68.         DLLNode<E> ins = new DLLNode<E>(o, null, first);
  69.         if (first == null)
  70.             last = ins;
  71.         else
  72.             first.pred = ins;
  73.         first = ins;
  74.     }
  75.  
  76.     public void insertLast(E o) {
  77.         if (first == null)
  78.             insertFirst(o);
  79.         else {
  80.             DLLNode<E> ins = new DLLNode<E>(o, last, null);
  81.             last.succ = ins;
  82.             last = ins;
  83.         }
  84.     }
  85.  
  86.     public void insertAfter(E o, DLLNode<E> after) {
  87.         if (after == last) {
  88.             insertLast(o);
  89.             return;
  90.         }
  91.         DLLNode<E> ins = new DLLNode<E>(o, after, after.succ);
  92.         after.succ.pred = ins;
  93.         after.succ = ins;
  94.     }
  95.  
  96.     public void insertBefore(E o, DLLNode<E> before) {
  97.         if (before == first) {
  98.             insertFirst(o);
  99.             return;
  100.         }
  101.         DLLNode<E> ins = new DLLNode<E>(o, before.pred, before);
  102.         before.pred.succ = ins;
  103.         before.pred = ins;
  104.     }
  105.  
  106.     public E deleteFirst() {
  107.         if (first != null) {
  108.             DLLNode<E> tmp = first;
  109.             first = first.succ;
  110.             if (first != null) first.pred = null;
  111.             if (first == null)
  112.                 last = null;
  113.             return tmp.element;
  114.         } else
  115.             return null;
  116.     }
  117.  
  118.     public E deleteLast() {
  119.         if (first != null) {
  120.             if (first.succ == null)
  121.                 return deleteFirst();
  122.             else {
  123.                 DLLNode<E> tmp = last;
  124.                 last = last.pred;
  125.                 last.succ = null;
  126.                 return tmp.element;
  127.             }
  128.         }
  129.         // else throw Exception
  130.         return null;
  131.     }
  132.  
  133.     public E delete(DLLNode<E> node) {
  134.         if (node == first) {
  135.             deleteFirst();
  136.             return node.element;
  137.         }
  138.         if (node == last) {
  139.             deleteLast();
  140.             return node.element;
  141.         }
  142.         node.pred.succ = node.succ;
  143.         node.succ.pred = node.pred;
  144.         return node.element;
  145.  
  146.     }
  147.  
  148.     @Override
  149.     public String toString() {
  150.         String ret = new String();
  151.         if (first != null) {
  152.             DLLNode<E> tmp = first;
  153.             ret += tmp + "<->";
  154.             while (tmp.succ != null) {
  155.                 tmp = tmp.succ;
  156.                 ret += tmp + "<->";
  157.             }
  158.         } else
  159.             ret = "Prazna lista!!!";
  160.         return ret;
  161.     }
  162.  
  163.     public String toStringR() {
  164.         String ret = new String();
  165.         if (last != null) {
  166.             DLLNode<E> tmp = last;
  167.             ret += tmp + "<->";
  168.             while (tmp.pred != null) {
  169.                 tmp = tmp.pred;
  170.                 ret += tmp + "<->";
  171.             }
  172.         } else
  173.             ret = "Prazna lista!!!";
  174.         return ret;
  175.     }
  176.  
  177.     public DLLNode<E> getFirst() {
  178.         return first;
  179.     }
  180.  
  181.     public DLLNode<E> getLast() {
  182.  
  183.         return last;
  184.     }
  185.  
  186.     public void deleteDuplicates() {
  187.         /*if (first != null) {
  188.             DLLNode<E> tmp = first, tmp2 = tmp.succ;
  189.             while (tmp.succ != null) {
  190.                 while (tmp2 != null) {
  191.                     if (tmp.element.compareTo(tmp2.element) == 0) {
  192.                         tmp2 = tmp2.succ;
  193.                         this.delete(tmp2.pred);
  194.                     } else
  195.                         tmp2 = tmp2.succ;
  196.                 }
  197.                 tmp = tmp.succ;
  198.                 tmp2 = tmp.succ;
  199.             }
  200.         } else
  201.             return;*/
  202.  
  203.  
  204.         if (first != null) {
  205.             DLLNode<E> current = first;
  206.             DLLNode<E> following = first.succ;
  207.             while (current.succ != null) {
  208.                 while (following != null) {
  209.                     if (current.element.equals(following.element)) {
  210.                         following = following.succ;
  211.                         delete(following.pred);
  212.                     } else {
  213.                         following = following.succ;
  214.                     }
  215.                 }
  216.                 current = current.succ;
  217.                 following = current.succ;
  218.             }
  219.         }
  220.         return;
  221.     }
  222.  
  223.  
  224. }
  225.  
  226. public class WeightedSum {
  227.  
  228. /*
  229. Sample input
  230. 10 3
  231. 1 9 2 3 6 1 3 2 1 3
  232.  
  233. Sample output
  234. 37
  235. */
  236.  
  237.     /* 1⋅првиотброј+2⋅вториотброј+…+K⋅K−тиотброј */
  238.     static int solve(int numbers[], int len, int choose) {
  239.         int matrix[][] = new int[len][choose];
  240.         int result = 0;
  241.  
  242.         for (int i = 1; i < len; i++) {
  243.             for (int j = 1; j < choose; j++) {
  244.                 for (int k = 0; k < i; k++) {
  245.                     int abs = j * numbers[k];
  246.                     if (matrix[i][j] < matrix[k][j - 1] + abs) {
  247.                         matrix[i][j] = matrix[k][j - 1] + abs;
  248.                     }
  249.                 }
  250.                 if (matrix[i][j] > result) {
  251.                     result = matrix[i][j];
  252.                 }
  253.             }
  254.         }
  255.  
  256.         for (int i = 0; i < matrix.length; i++) {
  257.             for (int j = 0; j < matrix[0].length; j++) {
  258.                 System.out.print(matrix[i][j] + " ");
  259.             }
  260.             System.out.println();
  261.         }
  262.  
  263.  
  264.         return result;
  265.     }
  266.  
  267.     public static void swap(final int a[], final int i, final int j) {
  268.         final int temp = a[i];
  269.         a[i] = a[j];
  270.         a[j] = temp;
  271.     }
  272.  
  273.     public static void printArray(final int a[]) {
  274.         for (int i = 0; i < a.length; i++) {
  275.             System.out.print(a[i] + " ");
  276.         }
  277.         System.out.println();
  278.     }
  279.  
  280.     public static void printArray(final String a[]) {
  281.         for (int i = 0; i < a.length; i++) {
  282.             System.out.print(a[i] + "");
  283.         }
  284.         System.out.println();
  285.     }
  286.  
  287.     public static void main(String[] args) throws Exception {
  288.  
  289.         /*BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  290.         StringTokenizer st = new StringTokenizer(br.readLine());
  291.         int N = Integer.parseInt(st.nextToken());
  292.         int K = Integer.parseInt(st.nextToken());
  293.  
  294.         int numbers[] = new int[N];
  295.  
  296.         st = new StringTokenizer(br.readLine());
  297.         for (int i = 0; i < N; i++) {
  298.             numbers[i] = Integer.parseInt(st.nextToken());
  299.         }
  300.         int res = solve(numbers, N, K);
  301.         System.out.println(res);
  302.         br.close();
  303.         */
  304.  
  305.         /*int digits[] = new int[10];
  306.         int size = 7;
  307.         for (int i = 0; i < size; i++) {
  308.             digits[i] = i;
  309.         }
  310.         printArray(digits);
  311.  
  312.         for (int i = size; i > 0; i--) {
  313.             swap(digits, i, 2 + i);
  314.         }
  315.         printArray(digits);
  316.  
  317.         int index = 3;
  318.         int elemsToMove = size - index + 1;
  319.         System.arraycopy(digits, index, digits, index+1, elemsToMove);
  320.         printArray(digits);*/
  321.  
  322.         /*
  323.         // Deletes Duplicates
  324.         DLL<Integer> list = new DLL<>();
  325.         for (int i = 0; i < 5; i++) {
  326.             list.insertLast(i);
  327.         }
  328.         for (int i = 1; i < 6; i++) {
  329.             list.insertLast(i);
  330.         }
  331.         System.out.println(list);
  332.  
  333.         list.deleteDuplicates();
  334.  
  335.         System.out.println(list);*/
  336.  
  337.         DLL<Character> list = new DLL<>();
  338.         String letters = "abcdefghij";
  339.         char characters[] = letters.toCharArray();
  340.  
  341.         for (int i = 0; i < characters.length; i++) {
  342.             list.insertLast((Character) characters[i]);
  343.         }
  344.         System.out.println(list);
  345.  
  346.         // From the beginning
  347.         //DLLNode first = list.getFirst();
  348.         //DLLNode last = list.getLast();
  349.         /*for (DLLNode first = list.getFirst();  first != null; first = first.succ){
  350.             list.delete(first.succ);
  351.         }
  352.         System.out.println(list);*/
  353.         System.out.println(list.length());
  354.         while (list.length() != 1) {
  355.             DLLNode first = list.getFirst();
  356.             //System.out.println("First Element: " + first.element);
  357.             while (first != null) {
  358.                 if (first.succ != null) {
  359.                     list.delete(first.succ);
  360.                 }
  361.                 first = first.succ;
  362.             }
  363.             System.out.println("From Front");
  364.             System.out.println(list);
  365.  
  366.             DLLNode last = list.getLast();
  367.             //System.out.println("Last Element: " + last.element);
  368.             while (last != null) {
  369.                 //System.out.println("While Last Element: " + last.element);
  370.                 if (last.pred != null) {
  371.                     list.delete(last.pred);
  372.                 }
  373.                 last = last.pred;
  374.  
  375.             }
  376.             System.out.println("From Back");
  377.             System.out.println(list);
  378.         }
  379.  
  380.         System.out.println("\nFINAL\n");
  381.         System.out.println(list);
  382.     }
  383.  
  384. }
Advertisement
Add Comment
Please, Sign In to add comment