Advertisement
Guest User

KONECNOOO

a guest
Oct 25th, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.56 KB | None | 0 0
  1. package probno;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. /**
  8.  *
  9.  * @author Sandra
  10.  */
  11. public class SLLJoinLists1<E extends Comparable<E>> {
  12.  
  13.     public static class SLLNode<E> {
  14.  
  15.         protected E element;
  16.         protected SLLNode<E> succ;
  17.  
  18.         public SLLNode(E value, SLLNode<E> succ) {
  19.             this.element = value;
  20.             this.succ = succ;
  21.         }
  22.  
  23.         public E getElement() {
  24.             return element;
  25.         }
  26.     }
  27.  
  28.     public static class SLL<E> {
  29.  
  30.         private SLLNode<E> first;
  31.  
  32.         public SLL() {
  33.             this.first = null;
  34.         }
  35.  
  36.         public SLLNode<E> getFirst() {
  37.             return first;
  38.         }
  39.  
  40.         public void insertFirst(E o) {
  41.             SLLNode<E> ins = new SLLNode<E>(o, first);
  42.             first = ins;
  43.         }
  44.  
  45.         public void insertLast(E o) {
  46.             if (first != null) {
  47.                 SLLNode<E> pom1 = first;
  48.                 while (pom1.succ != null) {
  49.                     pom1 = pom1.succ;
  50.                 }
  51.                 SLLNode<E> ins = new SLLNode<E>(o, null);
  52.                 pom1.succ = ins;
  53.             } else {
  54.                 insertFirst(o);
  55.             }
  56.         }
  57.  
  58.         public E delete(SLLNode<E> node) {
  59.             if (first != null) {
  60.                 SLLNode<E> tmp = first;
  61.                 while (tmp.succ != node
  62.                         && tmp.succ.succ != null) {
  63.                     tmp = tmp.succ;
  64.                 }
  65.                 if (tmp.succ == node) {
  66.                     tmp.succ = tmp.succ.succ;
  67.                     return node.element;
  68.                 }
  69.             }
  70.             return null;
  71.         }
  72.     }
  73.  
  74.     public static SLL<Integer> joinLists(SLL<Integer> lista1, SLL<Integer> lista2) {
  75.         SLL<Integer> lista3 = new SLL<Integer>();
  76.         SLLNode<Integer> jazol1 = lista1.getFirst(), jazol2 = lista2.getFirst();
  77.  
  78.         while (jazol1 != null && jazol2 != null) {
  79.             if (jazol1.element.compareTo(jazol2.element) < 0) {
  80.                 lista3.insertLast(jazol1.element);
  81.                 jazol1 = jazol1.succ;
  82.             } else {
  83.                 lista3.insertLast(jazol2.element);
  84.                 jazol2 = jazol2.succ;
  85.             }
  86.         }
  87.         if (jazol1 != null) {
  88.             while (jazol1 != null) {
  89.                 lista3.insertLast(jazol1.element);
  90.                 jazol1 = jazol1.succ;
  91.             }
  92.         }
  93.         if (jazol2 != null) {
  94.             while (jazol2 != null) {
  95.                 lista3.insertLast(jazol2.element);
  96.                 jazol2 = jazol2.succ;
  97.             }
  98.         }
  99.         return lista3;
  100.     }
  101.  
  102.     public static SLL<Integer> brisiDup(SLL<Integer> lista){
  103.         SLLNode<Integer> pom1 = (SLLNode<Integer>) lista.first;
  104.         SLLNode<Integer> next = (SLLNode<Integer>) lista.first.succ;
  105.            
  106.             while (pom1 != null) {
  107.                 next = pom1.succ;
  108.                 while (next != null) {
  109.                     if (pom1.getElement() == next.getElement()) {
  110.                         lista.delete(next);
  111.                         next = next.succ;
  112.                     } else {
  113.                         next = next.succ;
  114.                     }
  115.                 }
  116.                 pom1 = pom1.succ;
  117.             }
  118.        
  119.         return lista;
  120.     }
  121.  
  122.     public static void pecati(SLL<Integer> lista) {
  123.         SLLNode<Integer> temp = lista.getFirst();
  124.         while (temp != null) {
  125.             System.out.print(temp.element);
  126.             System.out.print(" ");
  127.             temp = temp.succ;
  128.         }
  129.     }
  130.  
  131.     public static void main(String[] args) throws IOException {
  132.  
  133.         BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  134.  
  135.         String s = stdin.readLine();
  136.         int N = Integer.parseInt(s);
  137.         s = stdin.readLine();
  138.         String[] pomniza = s.split(" ");
  139.  
  140.         SLL<Integer> lista1 = new SLL<Integer>();
  141.  
  142.         for (int i = 0; i < N; i++) {
  143.             lista1.insertLast(Integer.parseInt(pomniza[i]));
  144.         }
  145.  
  146.         String s1 = stdin.readLine();
  147.         int N1 = Integer.parseInt(s1);
  148.         s1 = stdin.readLine();
  149.         String[] pomniza1 = s1.split(" ");
  150.  
  151.         SLL<Integer> lista2 = new SLL<Integer>();
  152.  
  153.         for (int i = 0; i < N1; i++) {
  154.             lista2.insertLast(Integer.parseInt(pomniza1[i]));
  155.         }
  156.  
  157.         SLL<Integer> lista3 = new SLL<Integer>();
  158.         lista3 = joinLists(lista1, lista2);
  159.         brisiDup(lista3);
  160.         pecati(lista3);
  161.  
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement