Advertisement
ekrajchevska

Brishi podlista

Aug 15th, 2017
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.88 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package sllizbrishipodlista;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.IOException;
  10. import java.io.InputStreamReader;
  11. import java.util.Iterator;
  12. import java.util.NoSuchElementException;
  13. import java.util.Objects;
  14.  
  15. class SLL<E> {
  16.     private SLLNode<E> first;
  17.  
  18.     public SLL() {
  19.         // Construct an empty SLL
  20.         this.first = null;
  21.     }
  22.  
  23.     public void deleteList() {
  24.         first = null;
  25.     }
  26.  
  27.     public int length() {
  28.         int ret;
  29.         if (first != null) {
  30.             SLLNode<E> tmp = first;
  31.             ret = 1;
  32.             while (tmp.succ != null) {
  33.                 tmp = tmp.succ;
  34.                 ret++;
  35.             }
  36.             return ret;
  37.         } else
  38.             return 0;
  39.  
  40.     }
  41.  
  42.     @Override
  43.     public String toString() {
  44.         String ret = new String();
  45.         if (first != null) {
  46.             SLLNode<E> tmp = first;
  47.             ret += tmp + "->";
  48.             while (tmp.succ != null) {
  49.                 tmp = tmp.succ;
  50.                 ret += tmp + "->";
  51.             }
  52.         } else
  53.             ret = "Prazna lista!!!";
  54.         return ret;
  55.     }
  56.  
  57.     public void insertFirst(E o) {
  58.         SLLNode<E> ins = new SLLNode<E>(o, first);
  59.         first = ins;
  60.     }
  61.  
  62.     public void insertAfter(E o, SLLNode<E> node) {
  63.         if (node != null) {
  64.             SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  65.             node.succ = ins;
  66.         } else {
  67.             System.out.println("Dadenot jazol e null");
  68.         }
  69.     }
  70.  
  71.     public void insertBefore(E o, SLLNode<E> before) {
  72.        
  73.         if (first != null) {
  74.             SLLNode<E> tmp = first;
  75.             if(first==before){
  76.                 this.insertFirst(o);
  77.                 return;
  78.             }
  79.             //ako first!=before
  80.             while (tmp.succ != before)
  81.                 tmp = tmp.succ;
  82.             if (tmp.succ == before) {
  83.                 SLLNode<E> ins = new SLLNode<E>(o, before);
  84.                 tmp.succ = ins;
  85.             } else {
  86.                 System.out.println("Elementot ne postoi vo listata");
  87.             }
  88.         } else {
  89.             System.out.println("Listata e prazna");
  90.         }
  91.     }
  92.  
  93.     public void insertLast(E o) {
  94.         if (first != null) {
  95.             SLLNode<E> tmp = first;
  96.             while (tmp.succ != null)
  97.                 tmp = tmp.succ;
  98.             SLLNode<E> ins = new SLLNode<E>(o, null);
  99.             tmp.succ = ins;
  100.         } else {
  101.             insertFirst(o);
  102.         }
  103.     }
  104.  
  105.     public E deleteFirst() {
  106.         if (first != null) {
  107.             SLLNode<E> tmp = first;
  108.             first = first.succ;
  109.             return tmp.element;
  110.         } else {
  111.             System.out.println("Listata e prazna");
  112.             return null;
  113.         }
  114.     }
  115.  
  116.     public E delete(SLLNode<E> node) {
  117.         if (first != null) {
  118.             SLLNode<E> tmp = first;
  119.             if(first ==node){
  120.                 return this.deleteFirst();
  121.             }
  122.             while (tmp.succ != node && tmp.succ.succ != null)
  123.                 tmp = tmp.succ;
  124.             if (tmp.succ == node) {
  125.                 tmp.succ = tmp.succ.succ;
  126.                 return node.element;
  127.             } else {
  128.                 System.out.println("Elementot ne postoi vo listata");
  129.                 return null;
  130.             }
  131.         } else {
  132.             System.out.println("Listata e prazna");
  133.             return null;
  134.         }
  135.  
  136.     }
  137.  
  138.     public SLLNode<E> getFirst() {
  139.         return first;
  140.     }
  141.    
  142.     public SLLNode<E> find(E o) {
  143.         if (first != null) {
  144.             SLLNode<E> tmp = first;
  145.             while (tmp.element != o && tmp.succ != null)
  146.                 tmp = tmp.succ;
  147.             if (tmp.element == o) {
  148.                 return tmp;
  149.             } else {
  150.                 System.out.println("Elementot ne postoi vo listata");
  151.             }
  152.         } else {
  153.             System.out.println("Listata e prazna");
  154.         }
  155.         return first;
  156.     }
  157.    
  158.     public Iterator<E> iterator () {
  159.     // Return an iterator that visits all elements of this list, in left-to-right order.
  160.         return new LRIterator<E>();
  161.     }
  162.  
  163.     // //////////Inner class ////////////
  164.  
  165.     private class LRIterator<E> implements Iterator<E> {
  166.  
  167.         private SLLNode<E> place, curr;
  168.  
  169.         private LRIterator() {
  170.             place = (SLLNode<E>) first;
  171.             curr = null;
  172.         }
  173.  
  174.         public boolean hasNext() {
  175.             return (place != null);
  176.         }
  177.  
  178.         public E next() {
  179.             if (place == null)
  180.                 throw new NoSuchElementException();
  181.             E nextElem = place.element;
  182.             curr = place;
  183.             place = place.succ;
  184.             return nextElem;
  185.         }
  186.  
  187.         public void remove() {
  188.             //Not implemented
  189.         }
  190.     }
  191.    
  192.     public void mirror(){
  193.         if (first != null) {
  194.             //m=nextsucc, p=tmp,q=next
  195.             SLLNode<E> tmp = first;
  196.             SLLNode<E> newsucc = null;
  197.             SLLNode<E> next;
  198.            
  199.             while(tmp != null){
  200.                 next = tmp.succ;
  201.                 tmp.succ = newsucc;
  202.                 newsucc = tmp;
  203.                 tmp = next;
  204.             }
  205.             first = newsucc;
  206.         }
  207.        
  208.     }
  209.    
  210.     public void merge (SLL<E> in){
  211.         if (first != null) {
  212.             SLLNode<E> tmp = first;
  213.             while(tmp.succ != null)
  214.                 tmp = tmp.succ;
  215.             tmp.succ = in.getFirst();
  216.         }
  217.         else{
  218.             first = in.getFirst();
  219.         }
  220.     }
  221. }
  222.  class SLLNode<E> {
  223.     protected E element;
  224.     protected SLLNode<E> succ;
  225.  
  226.     public SLLNode(E elem, SLLNode<E> succ) {
  227.         this.element = elem;
  228.         this.succ = succ;
  229.     }
  230.  
  231.     @Override
  232.     public String toString() {
  233.         return element.toString();
  234.     }
  235. }
  236.  
  237. public class SLLIzbrishiPodlista {
  238.  
  239.    public static SLL<Integer> brishiPodlista(SLL<Integer> lista, SLL<Integer> podlista)
  240.    {
  241.        SLL<Integer> novalista = new SLL<>();
  242.        SLLNode<Integer> curr1 = lista.getFirst();
  243.        SLLNode<Integer> curr2 = podlista.getFirst();
  244.        SLLNode<Integer> pocetok, kraj, novPocetok=null;
  245.        boolean najdenNovP = false;
  246.        
  247.        while(curr1!=null)
  248.        {
  249.            if(curr1.element.equals(curr2.element))
  250.            {
  251.                pocetok = curr1; //najden e element kako prviot na podlistata
  252.                curr1=curr1.succ;
  253.                curr2=curr2.succ;
  254.                
  255.                while(curr1!=null && curr2!=null && curr1.element.equals(curr2.element)){
  256.                    
  257.                    if(curr1.element.equals(pocetok.element) && !najdenNovP){     //ako vo podlistata postoi uste eden element ist kako prviot
  258.                        novPocetok = curr1;                                                                  // i se pronajde takov i vo listata
  259.                        najdenNovP=true;
  260.                    }
  261.                    
  262.                    curr1=curr1.succ;
  263.                    curr2=curr2.succ;                      
  264.                }
  265.                
  266.                if(curr2!=null) //ne sme ja izminale podlistata dokraj, znaci od pocetok do mozniot nov pocetok se elementi koi
  267.                {                            // definitivno ne se podlista
  268.                    if(najdenNovP)
  269.                    {
  270.                        while(pocetok!=novPocetok)
  271.                        {
  272.                            novalista.insertLast(pocetok.element);
  273.                            pocetok=pocetok.succ;
  274.                        }        // posle ova pocetok=novpocetok
  275.                        curr1= novPocetok;
  276.                    }else{
  277.                        while(pocetok!=curr1)
  278.                        {
  279.                            novalista.insertLast(pocetok.element);
  280.                            pocetok=pocetok.succ;
  281.                        }
  282.                    }
  283.                      novPocetok=null;
  284.                      najdenNovP=false;
  285.                      pocetok=null;
  286.                      curr2 = podlista.getFirst();
  287.                     // continue;   // za da ne se pomesti na kraj od while-ot ( curr1=curr1.succ)
  288.                    
  289.                }
  290.                else //najdena e nova podlista ili curr1 stignal do null
  291.                {
  292.                     if(curr1!=null) //najdena podlista, ne dodavame elementi (ja briseme t.e..), reset na promenlivite
  293.                     {
  294.                         pocetok = null;
  295.                         novPocetok=null;
  296.                         najdenNovP=false;
  297.                     }
  298.                     else //stignato e do kraj na listata, ako pocetok ne e null znaci na krajot se naogja samo del od podlistata
  299.                     {
  300.                             while(pocetok!=null)
  301.                             {
  302.                                if(curr2!=null)
  303.                                 novalista.insertLast(pocetok.element);
  304.  
  305.                                 pocetok=pocetok.succ;
  306.                              }
  307.                        
  308.                     }
  309.  
  310.                     curr2=podlista.getFirst();
  311.                     //continue;
  312.                }
  313.            } else {
  314.                novalista.insertLast(curr1.element);
  315.            curr1=curr1.succ;
  316.            }
  317.        }
  318.        
  319.        return novalista;
  320.    }
  321.     public static void main(String[] args) throws IOException {
  322.         // TODO code application logic here
  323.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  324.         String s = br.readLine();
  325.         String[] pom= s.split(" ");
  326.        
  327.         SLL<Integer> lista = new SLL<>();
  328.         for(int i=0;i<pom.length;i++)
  329.             lista.insertLast(Integer.parseInt(pom[i]));
  330.        
  331.        
  332.         s = br.readLine();
  333.      pom= s.split(" ");
  334.        
  335.         SLL<Integer> podlista = new SLL<>();
  336.         for(int i=0;i<pom.length;i++)
  337.             podlista.insertLast(Integer.parseInt(pom[i]));
  338.        
  339.        
  340.         lista = brishiPodlista(lista,podlista);
  341.         System.out.println(lista.toString());
  342.        
  343.     }
  344.    
  345. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement