Crazy

Razigrana Lista

May 28th, 2018
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.24 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Scanner;
  5.  
  6. class DLLNode<E> {
  7.     protected E element;
  8.     protected DLLNode<E> pred, succ;
  9.  
  10.     public DLLNode(E elem, DLLNode<E> pred, DLLNode<E> succ) {
  11.         this.element = elem;
  12.         this.pred = pred;
  13.         this.succ = succ;
  14.     }
  15.  
  16.     @Override
  17.     public String toString() {
  18.         return element.toString();
  19.     }
  20. }
  21.  
  22. class DLL<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.    
  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.    
  174. }
  175.  
  176.  
  177. public class RazigranaLista {
  178.     private static void razigraj(DLL<String> lista)
  179.     {
  180.         DLLNode<String> pok1 = lista.getFirst();
  181.         DLLNode<String> pok2 = lista.getLast();
  182.        
  183.         int i=0;
  184.        
  185.         while(lista.length() != 1)
  186.         {
  187.             pok1 = lista.getFirst();
  188.             i = 1 ;
  189.             while(pok1 != null)
  190.             {
  191.                 if(i % 2== 0)
  192.                 {
  193.                     lista.delete(pok1);
  194.                 }
  195.                 pok1 = pok1.succ;
  196.                 i++;
  197.             }
  198.            
  199.             pok2 = lista.getLast();
  200.             i=1;
  201.            
  202.             while(pok2 != null)
  203.             {
  204.                 if(i % 2 == 0)
  205.                 {
  206.                     lista.delete(pok2);
  207.                 }
  208.                 pok2 = pok2.pred;
  209.                 i++;
  210.             }
  211.            
  212.            
  213.         }
  214.        
  215.     }
  216.    
  217.     public static void main(String[] args) throws IOException {
  218.         Scanner input=new Scanner(System.in);
  219.         DLL<String> lista = new DLL<>();
  220.                 String s=input.nextLine();
  221.         String[] niza = s.split(" ");
  222.         for(int i=0;i < niza.length;i++)
  223.         {
  224.             lista.insertLast(niza[i]);
  225.         }
  226.         razigraj(lista);
  227.                 DLLNode<String> pok = lista.getFirst();
  228.        
  229.         while(pok != null)
  230.         {
  231.             System.out.print(pok.element);
  232.             if(pok.succ != null)
  233.             {
  234.                 System.out.print(" ");
  235.             }
  236.             pok = pok.succ;
  237.         }
  238.     }
  239.  
  240. }
Advertisement
Add Comment
Please, Sign In to add comment