Advertisement
Guest User

Untitled

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