Advertisement
Latkoski

razigrana

May 26th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.53 KB | None | 0 0
  1. package listaodlisti;
  2.  
  3. import java.beans.DefaultPersistenceDelegate;
  4. import java.io.BufferedReader;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7.  
  8. class DLL<E> {
  9.     private DLLNode<E> first, last;
  10.  
  11.     public DLL() {
  12.         // Construct an empty SLL
  13.         this.first = null;
  14.         this.last = null;
  15.     }
  16.  
  17.     public void deleteList() {
  18.         first = null;
  19.         last = null;
  20.     }
  21.  
  22.     public int length() {
  23.         int ret;
  24.         if (first != null) {
  25.             DLLNode<E> tmp = first;
  26.             ret = 1;
  27.             while (tmp.succ != null) {
  28.                 tmp = tmp.succ;
  29.                 ret++;
  30.             }
  31.             return ret;
  32.         } else
  33.             return 0;
  34.  
  35.     }
  36.  
  37.     public DLLNode<E> find(E o) {
  38.         if (first != null) {
  39.             DLLNode<E> tmp = first;
  40.             while (tmp.element != o && tmp.succ != null)
  41.                 tmp = tmp.succ;
  42.             if (tmp.element == o) {
  43.                 return tmp;
  44.             } else {
  45.                 System.out.println("Elementot ne postoi vo listata");
  46.             }
  47.         } else {
  48.             System.out.println("Listata e prazna");
  49.         }
  50.         return first;
  51.     }
  52.  
  53.     public void insertFirst(E o) {
  54.         DLLNode<E> ins = new DLLNode<E>(o, null, first);
  55.         if (first == null)
  56.             last = ins;
  57.         else
  58.             first.pred = ins;
  59.         first = ins;
  60.     }
  61.  
  62.     public void insertLast(E o) {
  63.         if (first == null)
  64.             insertFirst(o);
  65.         else {
  66.             DLLNode<E> ins = new DLLNode<E>(o, last, null);
  67.             last.succ = ins;
  68.             last = ins;
  69.         }
  70.     }
  71.  
  72.     public void insertAfter(E o, DLLNode<E> after) {
  73.         if (after == last) {
  74.             insertLast(o);
  75.             return;
  76.         }
  77.         DLLNode<E> ins = new DLLNode<E>(o, after, after.succ);
  78.         after.succ.pred = ins;
  79.         after.succ = ins;
  80.     }
  81.  
  82.     public void insertBefore(E o, DLLNode<E> before) {
  83.         if (before == first) {
  84.             insertFirst(o);
  85.             return;
  86.         }
  87.         DLLNode<E> ins = new DLLNode<E>(o, before.pred, before);
  88.         before.pred.succ = ins;
  89.         before.pred = ins;
  90.     }
  91.  
  92.     public E deleteFirst() {
  93.         if (first != null) {
  94.             DLLNode<E> tmp = first;
  95.             first = first.succ;
  96.             if (first != null)
  97.                 first.pred = null;
  98.             if (first == null)
  99.                 last = null;
  100.             return tmp.element;
  101.         } else
  102.             return null;
  103.     }
  104.  
  105.     public E deleteLast() {
  106.         if (first != null) {
  107.             if (first.succ == null)
  108.                 return deleteFirst();
  109.             else {
  110.                 DLLNode<E> tmp = last;
  111.                 last = last.pred;
  112.                 last.succ = null;
  113.                 return tmp.element;
  114.             }
  115.         }
  116.         // else throw Exception
  117.         return null;
  118.     }
  119.  
  120.     public E delete(DLLNode<E> node) {
  121.         if (node == first) {
  122.             deleteFirst();
  123.             return node.element;
  124.         }
  125.         if (node == last) {
  126.             deleteLast();
  127.             return node.element;
  128.         }
  129.         node.pred.succ = node.succ;
  130.         node.succ.pred = node.pred;
  131.         return node.element;
  132.  
  133.     }
  134.  
  135.     @Override
  136.     public String toString() {
  137.         String ret = new String();
  138.         if (first != null) {
  139.             DLLNode<E> tmp = first;
  140.             ret += tmp + "<->";
  141.             while (tmp.succ != null) {
  142.                 tmp = tmp.succ;
  143.                 ret += tmp + "<->";
  144.             }
  145.         } else
  146.             ret = "Prazna lista!!!";
  147.         return ret;
  148.     }
  149.  
  150.     public String toStringR() {
  151.         String ret = new String();
  152.         if (last != null) {
  153.             DLLNode<E> tmp = last;
  154.             ret += tmp + "<->";
  155.             while (tmp.pred != null) {
  156.                 tmp = tmp.pred;
  157.                 ret += tmp + "<->";
  158.             }
  159.         } else
  160.             ret = "Prazna lista!!!";
  161.         return ret;
  162.     }
  163.  
  164.     public DLLNode<E> getFirst() {
  165.         return first;
  166.     }
  167.  
  168.     public DLLNode<E> getLast() {
  169.  
  170.         return last;
  171.     }
  172.  
  173.     public void izvadiDupliIPrebroj() {
  174.  
  175.     }
  176. }
  177.  
  178. class DLLNode<E> {
  179.     protected E element;
  180.     protected DLLNode<E> pred, succ;
  181.  
  182.     public DLLNode(E elem, DLLNode<E> pred, DLLNode<E> succ) {
  183.         this.element = elem;
  184.         this.pred = pred;
  185.         this.succ = succ;
  186.     }
  187.  
  188.     @Override
  189.     public String toString() {
  190.         return element.toString();
  191.     }
  192. }
  193.  
  194. public class PlayfulList {
  195.  
  196.     public static void razigraj(DLL<String> lista) {
  197.         int brojac = 0;
  198.         DLLNode<String> front = lista.getFirst();
  199.         DLLNode<String> back = lista.getLast();
  200.         while (lista.length() != 1) {
  201.             brojac = 0;
  202.             front = lista.getFirst();
  203.             while (front != null) {
  204.                 brojac++;
  205.                 if (brojac % 2 == 0) {
  206.                     lista.delete(front);
  207.                     front = front.succ;
  208.                 } else
  209.                     front = front.succ;
  210.             }
  211.             back = lista.getLast();
  212.             brojac = 0;
  213.             while (back != null) {
  214.                 brojac++;
  215.                 if (brojac % 2 == 0) {
  216.                     lista.delete(back);
  217.                     back = back.pred;
  218.                 } else
  219.                     back = back.pred;
  220.             }
  221.         }
  222.         front = lista.getFirst();
  223.         System.out.println(front.element);
  224.     }
  225.  
  226.     public static void main(String[] args) throws IOException {
  227.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  228.         DLL<String> lista = new DLL<String>();
  229.         String[] str = br.readLine().split(" ");
  230.         for (int i = 0; i < str.length; i++) {
  231.             lista.insertLast(str[i]);
  232.         }
  233.         razigraj(lista);
  234.     }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement