SlavkovB

[АПС] колоквиум 2018

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