Crazy

Спој листи

Oct 21st, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.37 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Iterator;
  5. import java.util.NoSuchElementException;
  6.  
  7. class SLLNode<E>{
  8.     protected E data;
  9.     protected SLLNode<E> succ;
  10.     SLLNode(E data, SLLNode<E> succ){
  11.         this.data = data;
  12.         this.succ = succ;
  13.     }
  14. }
  15.  
  16. class SLL<E>{
  17.     protected SLLNode<E> first;
  18.     SLL(){
  19.         this.first = null;
  20.     }
  21.     public void insertFirst(E o) {
  22.         SLLNode<E> ins = new SLLNode<E>(o, first);
  23.         first = ins;
  24.     }
  25.     public void insertAfter(E o, SLLNode<E> node){
  26.         if(node != null){
  27.         SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  28.         node.succ = ins;}
  29.         else System.exit(1);
  30.     }
  31.     public E deleteFirst(){
  32.         if(first != null){
  33.             SLLNode<E> ins = first;
  34.             first = first.succ;
  35.             return ins.data;
  36.         }
  37.         return null;
  38.     }
  39.     public E delete(SLLNode<E> node){
  40.         if(first != null){
  41.             SLLNode<E> temp = first;
  42.             while(temp.succ != node)
  43.                 temp = temp.succ;
  44.             if(temp.succ == node)
  45.                 temp.succ = temp.succ.succ;
  46.             return node.data;
  47.         }
  48.         else
  49.             System.out.print("Listata e prazna! ");
  50.             return null;
  51.     }
  52.  
  53.     public int lenght(){
  54.         int ret = 0;
  55.         if(first != null){
  56.             SLLNode<E> temp = first;
  57.             while(temp != null)
  58.             {
  59.                 temp = temp.succ;
  60.                 ret++;
  61.             }
  62.         }
  63.         return ret;
  64.     }
  65.     public void insertBefore(E o, SLLNode <E> node){
  66.         if(first != null){
  67.             if(node == first)
  68.                 insertFirst(o);
  69.             else {
  70.                 SLLNode<E> temp = first;
  71.                 while(temp.succ != node)
  72.                     temp = temp.succ;
  73.                 SLLNode<E> temp1 = new SLLNode<E>(o, node);
  74.                 temp.succ = temp1;
  75.             }
  76.         }
  77.         else System.out.print("Listata e prazna");
  78.     }
  79.     public void insertLast(E o){
  80.         if(first != null){
  81.             SLLNode<E> temp = first;
  82.             while(temp.succ != null)
  83.                 temp = temp.succ;
  84.             SLLNode<E> ins = new SLLNode<E>(o, null);
  85.             temp.succ = ins;
  86.         }
  87.         else this.insertFirst(o);
  88.     }
  89.  
  90.     public Iterator<E> iterator () {
  91. // vraka iterator koj gi posetuva site elementi na listata od levo na desno
  92.         return new LRIterator<E>();
  93.     }
  94.     // //////////Inner class ////////////
  95.     private class LRIterator<E> implements Iterator<E> {
  96.         private SLLNode<E> place, prev, curr;
  97.         private LRIterator() {
  98.             place = (SLLNode<E>) first;
  99.             curr = prev = null;
  100.         }
  101.         public boolean hasNext() {
  102.             return (place != null);
  103.         }
  104.         public E next() {
  105.             if (place == null)
  106.                 throw new NoSuchElementException();
  107.             E nextElem = place.data;
  108.             prev = curr;
  109.             curr = place;
  110.             place = place.succ;
  111.             return nextElem;
  112.         }
  113.         public void remove() {
  114. //Not implemented
  115.         }
  116.     }
  117. //    Дадени се две еднострано поврзани листи чии јазли содржат по еден природен број.
  118. //    Листите се сортирани во растечки редослед. Треба да се спојат двете листи во една така
  119. //    што резултантната листа да е сортирана. Сортирањето е подредување со слевање. Јазлите кои
  120. //    се јавуваат како дупликати (од иста листа или од различна) да се отстранат
  121.     public SLL<E> joinLists(SLL<E> lista2){
  122.         SLL<E> spoeni = new SLL<E>();
  123.         SLLNode<E> pok1 = this.first;
  124.         SLLNode<E> pok2 = lista2.first;
  125.         int last = (Integer)pok1.data < (Integer)pok2.data ? (Integer)pok1.data : (Integer)pok2.data;
  126.         spoeni.insertFirst((E)((Object)last));
  127.         if((Integer)pok1.data < (Integer)pok2.data)
  128.             pok1 = pok1.succ;
  129.         else pok2 = pok2.succ;
  130.  
  131.         while(pok1 != null&&pok2 != null){
  132.             if( (Integer)pok1.data < (Integer)pok2.data){
  133.                 if(last != (Integer)pok1.data) {
  134.                     spoeni.insertLast(pok1.data);
  135.                     last = (Integer) pok1.data;
  136.                 }
  137.                 pok1 = pok1.succ;
  138.             }
  139.             else {
  140.                 if(last != (Integer)pok2.data){
  141.                     spoeni.insertLast(pok2.data);
  142.                     last = (Integer)pok2.data;
  143.                 }
  144.                 pok2 = pok2.succ;
  145.             }
  146.         }
  147.         while(pok1 != null){
  148.             if(last != (Integer)pok1.data){
  149.                 spoeni.insertLast(pok1.data);
  150.                 last = (Integer)pok1.data;
  151.             }
  152.             pok1 = pok1.succ;
  153.         }
  154.         while(pok2 != null){
  155.             if(last != (Integer)pok2.data){
  156.                 spoeni.insertLast(pok2.data);
  157.                 last = (Integer)pok2.data;
  158.             }
  159.             pok2 = pok2.succ;
  160.         }
  161.         return spoeni;
  162.     }
  163.  
  164. }
  165.  
  166. public class SLLJoinLists {
  167.     public static void main(String[] args) throws IOException {
  168.  
  169.         BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  170.         String s = stdin.readLine();
  171.         int N = Integer.parseInt(s);
  172.         s = stdin.readLine();
  173.         String[] pomniza = s.split(" ");
  174.         SLL<Integer> lista1 = new SLL<Integer>();
  175.         SLL<Integer> lista2 = new SLL<Integer>();
  176.         SLL<Integer> spoeni = new SLL<Integer>();
  177.         for (int i = 0; i < N; i++) {
  178.             lista1.insertLast(Integer.parseInt(pomniza[i]));
  179.         }
  180.  
  181.         s = stdin.readLine();
  182.         N = Integer.parseInt(s);
  183.         s = stdin.readLine();
  184.         pomniza = s.split(" ");
  185.         for (int i = 0; i < N; i++) {
  186.             lista2.insertLast(Integer.parseInt(pomniza[i]));
  187.         }
  188.  
  189.         spoeni = lista1.joinLists(lista2);
  190.         Iterator<Integer> it = spoeni.iterator();
  191.         while (it.hasNext()) {
  192.             System.out.print(it.next());
  193.             if(it.hasNext())
  194.                 System.out.print(" ");
  195.         }
  196.         System.out.println();
  197.     }
  198. }
Add Comment
Please, Sign In to add comment