Advertisement
cimona

SLLJoinLists

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