Advertisement
AnaGocevska

Untitled

Oct 23rd, 2015
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.92 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Scanner;
  5.  
  6. import java.util.Iterator;
  7. import java.util.NoSuchElementException;
  8.  
  9. class SLL<E> {
  10.     private SLLNode<E> first;
  11.  
  12.     public SLL() {
  13.         // Construct an empty SLL
  14.         this.first = null;
  15.     }
  16.  
  17.     public void deleteList() {
  18.         first = null;
  19.     }
  20.  
  21.     public int length() {
  22.         int ret;
  23.         if (first != null) {
  24.             SLLNode<E> tmp = first;
  25.             ret = 1;
  26.             while (tmp.succ != null) {
  27.                 tmp = tmp.succ;
  28.                 ret++;
  29.             }
  30.             return ret;
  31.         } else
  32.             return 0;
  33.  
  34.     }
  35.  
  36.     @Override
  37.     public String toString() {
  38.         String ret = new String();
  39.         if (first != null) {
  40.             SLLNode<E> tmp = first;
  41.             ret += tmp + "->";
  42.             while (tmp.succ != null) {
  43.                 tmp = tmp.succ;
  44.                 ret += tmp + "->";
  45.             }
  46.         } else
  47.             ret = "Prazna lista!!!";
  48.         return ret;
  49.     }
  50.  
  51.     public void insertFirst(E o) {
  52.         SLLNode<E> ins = new SLLNode<E>(o, first);
  53.         first = ins;
  54.     }
  55.  
  56.     public void insertAfter(E o, SLLNode<E> node) {
  57.         if (node != null) {
  58.             SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  59.             node.succ = ins;
  60.         } else {
  61.             System.out.println("Dadenot jazol e null");
  62.         }
  63.     }
  64.  
  65.     public void insertBefore(E o, SLLNode<E> before) {
  66.        
  67.         if (first != null) {
  68.             SLLNode<E> tmp = first;
  69.             if(first==before){
  70.                 this.insertFirst(o);
  71.                 return;
  72.             }
  73.             //ako first!=before
  74.             while (tmp.succ != before)
  75.                 tmp = tmp.succ;
  76.             if (tmp.succ == before) {
  77.                 SLLNode<E> ins = new SLLNode<E>(o, before);
  78.                 tmp.succ = ins;
  79.             } else {
  80.                 System.out.println("Elementot ne postoi vo listata");
  81.             }
  82.         } else {
  83.             System.out.println("Listata e prazna");
  84.         }
  85.     }
  86.  
  87.     public void insertLast(E o) {
  88.         if (first != null) {
  89.             SLLNode<E> tmp = first;
  90.             while (tmp.succ != null)
  91.                 tmp = tmp.succ;
  92.             SLLNode<E> ins = new SLLNode<E>(o, null);
  93.             tmp.succ = ins;
  94.         } else {
  95.             insertFirst(o);
  96.         }
  97.     }
  98.  
  99.     public E deleteFirst() {
  100.         if (first != null) {
  101.             SLLNode<E> tmp = first;
  102.             first = first.succ;
  103.             return tmp.element;
  104.         } else {
  105.             System.out.println("Listata e prazna");
  106.             return null;
  107.         }
  108.     }
  109.  
  110.     public E delete(SLLNode<E> node) {
  111.         if (first != null) {
  112.             SLLNode<E> tmp = first;
  113.             if(first ==node){
  114.                 return this.deleteFirst();
  115.             }
  116.             while (tmp.succ != node&&tmp.succ.succ != null)
  117.                 tmp = tmp.succ;
  118.             if (tmp.succ == node) {
  119.                 tmp.succ = tmp.succ.succ;
  120.                 return node.element;
  121.             } else {
  122.                 System.out.println("Elementot ne postoi vo listata");
  123.                 return null;
  124.             }
  125.         } else {
  126.             System.out.println("Listata e prazna");
  127.             return null;
  128.         }
  129.  
  130.     }
  131.  
  132.     public SLLNode<E> getFirst() {
  133.         return first;
  134.     }
  135.    
  136.     public SLLNode<E> find(E o) {
  137.         if (first != null) {
  138.             SLLNode<E> tmp = first;
  139.             while (tmp.element != o&&tmp.succ != null)
  140.                 tmp = tmp.succ;
  141.             if (tmp.element == o) {
  142.                 return tmp;
  143.             } else {
  144.                 System.out.println("Elementot ne postoi vo listata");
  145.             }
  146.         } else {
  147.             System.out.println("Listata e prazna");
  148.         }
  149.         return first;
  150.     }
  151.    
  152.     public Iterator<E> iterator () {
  153.     // Return an iterator that visits all elements of this list, in left-to-right order.
  154.         return new LRIterator<E>();
  155.     }
  156.  
  157.     // //////////Inner class ////////////
  158.  
  159.     private class LRIterator<E> implements Iterator<E> {
  160.  
  161.         private SLLNode<E> place, curr;
  162.  
  163.         private LRIterator() {
  164.             place = (SLLNode<E>) first;
  165.             curr = null;
  166.         }
  167.  
  168.         public boolean hasNext() {
  169.             return (place != null);
  170.         }
  171.  
  172.         public E next() {
  173.             if (place == null)
  174.                 throw new NoSuchElementException();
  175.             E nextElem = place.element;
  176.             curr = place;
  177.             place = place.succ;
  178.             return nextElem;
  179.         }
  180.  
  181.         public void remove() {
  182.             //Not implemented
  183.         }
  184.     }
  185.    
  186.     public void mirror(){
  187.         if (first != null) {
  188.             //m=nextsucc, p=tmp,q=next
  189.             SLLNode<E> tmp = first;
  190.             SLLNode<E> newsucc = null;
  191.             SLLNode<E> next;
  192.            
  193.             while(tmp != null){
  194.                 next = tmp.succ;
  195.                 tmp.succ = newsucc;
  196.                 newsucc = tmp;
  197.                 tmp = next;
  198.             }
  199.             first = newsucc;
  200.         }
  201.        
  202.     }
  203.    
  204.     public void merge (SLL<E> in){
  205.         if (first != null) {
  206.             SLLNode<E> tmp = first;
  207.             while(tmp.succ != null)
  208.                 tmp = tmp.succ;
  209.             tmp.succ = in.getFirst();
  210.         }
  211.         else{
  212.             first = in.getFirst();
  213.         }
  214.     }
  215. }
  216.  
  217. class SLLNode<E> {
  218.     protected E element;
  219.     protected SLLNode<E> succ;
  220.  
  221.     public SLLNode(E elem, SLLNode<E> succ) {
  222.         this.element = elem;
  223.         this.succ = succ;
  224.     }
  225.  
  226.     @Override
  227.     public String toString() {
  228.         return element.toString();
  229.     }
  230. }
  231.  
  232. public class SpecialSLLJoin {
  233.    
  234.     public static void pecati(SLL<Integer> lista)
  235.     {
  236.         SLLNode<Integer> tmp = lista.getFirst();
  237.        
  238.         while(tmp != null)
  239.         {
  240.             System.out.print(tmp.element + " ");
  241.             tmp = tmp.succ;
  242.         }
  243.     }
  244.  
  245.     public static SLL<Integer> joinList (SLL<Integer> lista1, SLL<Integer> lista2)
  246.     {
  247.         SLL<Integer> finalList = new SLL<Integer>();
  248.         SLLNode<Integer> tmp1 = lista1.getFirst();
  249.         SLLNode<Integer> tmp2 = lista2.getFirst();
  250.        
  251.         while(tmp1 != null&&tmp1.succ !=null && tmp2 != null && tmp2.succ != null)
  252.         {
  253.             finalList.insertLast(tmp1.element);
  254.             finalList.insertLast(tmp1.succ.element);
  255.             finalList.insertLast(tmp2.element);
  256.             finalList.insertLast(tmp2.succ.element);
  257.            
  258.             if(tmp1.succ.succ != null)
  259.             {
  260.                 tmp1 = tmp1.succ.succ;
  261.             }
  262.             else
  263.             {
  264.                 tmp1 = null;
  265.             }
  266.             if(tmp2.succ.succ != null)
  267.             {
  268.                 tmp2 = tmp2.succ.succ;
  269.             }
  270.             else
  271.             {
  272.                 tmp2 = null;
  273.             }
  274.            
  275.         }
  276.         if(tmp1 != null)
  277.         {
  278.             while(tmp1 != null)
  279.             {
  280.                 finalList.insertLast(tmp1.element);
  281.                 tmp1 = tmp1.succ;
  282.             }
  283.         }
  284.         if(tmp2 != null)
  285.         {
  286.             while(tmp2 != null)
  287.             {
  288.                 finalList.insertLast(tmp2.element);
  289.                 tmp2 = tmp2.succ;
  290.             }
  291.         }
  292.         return finalList;
  293.     }
  294.    
  295.    
  296.     public static void main(String[] args) throws IOException{
  297.    
  298.         BufferedReader stdin = new BufferedReader(new InputStreamReader(
  299.                 System.in));
  300.         String s = stdin.readLine();
  301.         SLL<Integer> lista1 = new SLL<Integer>();
  302.         SLL<Integer> lista2 = new SLL<Integer>();
  303.         int N = Integer.parseInt(s);
  304.         s = stdin.readLine();
  305.         String[] pomniza = s.split(" ");
  306.         for (int i = 0; i < N; i++) {
  307.             lista1.insertLast(Integer.parseInt(pomniza[i]));
  308.         }
  309.  
  310.         s = stdin.readLine();
  311.         N = Integer.parseInt(s);
  312.         s = stdin.readLine();
  313.         pomniza = s.split(" ");
  314.         for (int i = 0; i < N; i++) {
  315.             lista2.insertLast(Integer.parseInt(pomniza[i]));
  316.         }
  317.        
  318.         //spoeni = specialJoin(lista1,lista2);
  319.        
  320.         Scanner scan = new Scanner(System.in);
  321.        
  322.         SLL<Integer> list = joinList(lista1, lista2);
  323.        
  324.         pecati(list);
  325.        
  326.        
  327.     }
  328. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement