Latkoski

Разиграна листа

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