Advertisement
Mitrezzz

Двојно поврзана листа: Раздели по парност

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