SlavkovB

[АПС] Јуни 2017 термин 1 - листа

Nov 21st, 2018
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.97 KB | None | 0 0
  1. /**(1 Термин):
  2. Дадена еднострано поврзана листа, се бара N пати да се избрише средината. Ако листата е со парен број елементи од 2та средишни елементи се брише помалиот, а ако се исти се брише првиот.
  3. Влез: првата линија број на елементи на листата,вториот ред елементите на листата и во третиот ред број колку пати да се избрише средината
  4.  
  5. Sample input:       Sample output:
  6. 5                   1 4 5
  7. 1 2 3 4 5
  8. 2
  9. */
  10.  
  11. //  CODE
  12.  
  13.  
  14. import java.util.Scanner;
  15.  
  16. class SLLNode<E> {
  17.     protected E element;
  18.     protected SLLNode<E> succ;
  19.  
  20.     public SLLNode(E elem, SLLNode<E> succ) {
  21.         this.element = elem;
  22.         this.succ = succ;
  23.     }
  24.  
  25.     @Override
  26.     public String toString() {
  27.         return element.toString();
  28.     }
  29. }
  30.  
  31.  
  32. class SLL<E> {
  33.     private SLLNode<E> first;
  34.  
  35.     public SLL() {
  36.         // Construct an empty SLL
  37.         this.first = null;
  38.     }
  39.  
  40.     public void deleteList() {
  41.         first = null;
  42.     }
  43.  
  44.     public int length() {
  45.         int ret;
  46.         if (first != null) {
  47.             SLLNode<E> tmp = first;
  48.             ret = 1;
  49.             while (tmp.succ != null) {
  50.                 tmp = tmp.succ;
  51.                 ret++;
  52.             }
  53.             return ret;
  54.         } else
  55.             return 0;
  56.  
  57.     }
  58.  
  59.     @Override
  60.     public String toString() {
  61.         String ret = new String();
  62.         if (first != null) {
  63.             SLLNode<E> tmp = first;
  64.             ret += tmp + " ";
  65.             while (tmp.succ != null) {
  66.                 tmp = tmp.succ;
  67.                 ret += tmp + " ";
  68.             }
  69.         } else
  70.             ret = "Prazna lista!!!";
  71.         return ret;
  72.     }
  73.  
  74.     public void setFirst(SLLNode<E> first) {
  75.         this.first = first;
  76.     }
  77.  
  78.     public void insertFirst(E o) {
  79.         SLLNode<E> ins = new SLLNode<E>(o, first);
  80.         first = ins;
  81.     }
  82.  
  83.     public void insertAfter(E o, SLLNode<E> node) {
  84.         if (node != null) {
  85.             SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  86.             node.succ = ins;
  87.         } else {
  88.             System.out.println("Dadenot jazol e null");
  89.         }
  90.     }
  91.  
  92.     public void insertBefore(E o, SLLNode<E> before) {
  93.  
  94.         if (first != null) {
  95.             SLLNode<E> tmp = first;
  96.             if (first == before) {
  97.                 this.insertFirst(o);
  98.                 return;
  99.             }
  100.             // ako first!=before
  101.             while (tmp.succ != before)
  102.                 tmp = tmp.succ;
  103.             if (tmp.succ == before) {
  104.                 SLLNode<E> ins = new SLLNode<E>(o, before);
  105.                 tmp.succ = ins;
  106.             } else {
  107.                 System.out.println("Elementot ne postoi vo listata");
  108.             }
  109.         } else {
  110.             System.out.println("Listata e prazna");
  111.         }
  112.     }
  113.  
  114.     public void insertLast(E o) {
  115.         if (first != null) {
  116.             SLLNode<E> tmp = first;
  117.             while (tmp.succ != null)
  118.                 tmp = tmp.succ;
  119.             SLLNode<E> ins = new SLLNode<E>(o, null);
  120.             tmp.succ = ins;
  121.         } else {
  122.             insertFirst(o);
  123.         }
  124.     }
  125.  
  126.     public E deleteFirst() {
  127.         if (first != null) {
  128.             SLLNode<E> tmp = first;
  129.             first = first.succ;
  130.             return tmp.element;
  131.         } else {
  132.             System.out.println("Listata e prazna");
  133.             return null;
  134.         }
  135.     }
  136.  
  137.     public E delete(SLLNode<E> node) {
  138.         if (first != null) {
  139.             SLLNode<E> tmp = first;
  140.             if (first == node) {
  141.                 return this.deleteFirst();
  142.             }
  143.             while (tmp.succ != node && tmp.succ.succ != null)
  144.                 tmp = tmp.succ;
  145.             if (tmp.succ == node) {
  146.                 tmp.succ = tmp.succ.succ;
  147.                 return node.element;
  148.             } else {
  149.                 System.out.println("Elementot ne postoi vo listata");
  150.                 return null;
  151.             }
  152.         } else {
  153.             System.out.println("Listata e prazna");
  154.             return null;
  155.         }
  156.  
  157.     }
  158.  
  159.     public SLLNode<E> getFirst() {
  160.         return first;
  161.     }
  162.  
  163.     public SLLNode<E> find(E o) {
  164.         if (first != null) {
  165.             SLLNode<E> tmp = first;
  166.             while (tmp.element != o && tmp.succ != null)
  167.                 tmp = tmp.succ;
  168.             if (tmp.element == o) {
  169.                 return tmp;
  170.             } else {
  171.                 System.out.println("Elementot ne postoi vo listata");
  172.             }
  173.         } else {
  174.             System.out.println("Listata e prazna");
  175.         }
  176.         return first;
  177.     }
  178.  
  179. }
  180.  
  181. public class BrisiSredina {
  182.     public static void removeMiddleNode(SLL<Integer> list, int timesDeleted) {
  183.  
  184.         while (timesDeleted > 0) {
  185.  
  186.             SLLNode<Integer> current = list.getFirst();
  187.             int counter = 1;
  188.  
  189.             if (timesDeleted >= list.length()) {
  190.                 list.setFirst(null);
  191.                 break;
  192.             } else {
  193.                 while (current != null) {
  194.                     int listLen = list.length();
  195.                     if (listLen % 2 != 0) {
  196.                         if (counter == (listLen / 2) + 1) {
  197.                             list.delete(current);
  198.                         }
  199.                     } else if (listLen % 2 == 0) {
  200.                         if (counter == listLen / 2) {
  201.                             if (current.element <= current.succ.element) {
  202.                                 list.delete(current);
  203.                             } else if (current.element > current.succ.element) {
  204.                                 list.delete(current.succ);
  205.                             }
  206.                         }
  207.                     }
  208.                     ++counter;
  209.                     current = current.succ;
  210.                 }
  211.                 --timesDeleted;
  212.             }
  213.         }
  214.         System.out.println(list);
  215.     }
  216.  
  217.     public static void main(String[] args) {
  218.         Scanner input = new Scanner(System.in);
  219.  
  220.         SLL<Integer> list = new SLL<Integer>();
  221.         int n = input.nextInt();
  222.  
  223.         for (int i = 0; i < n; ++i)
  224.             list.insertLast(input.nextInt());
  225.         removeMiddleNode(list, input.nextInt());
  226.         input.close();
  227.     }
  228. }
Add Comment
Please, Sign In to add comment