Advertisement
Guest User

Untitled

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