Latkoski

Спој листи наизменично

Jan 22nd, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.11 KB | None | 0 0
  1. package sl;
  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 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
  154.         // left-to-right order.
  155.         return new LRIterator<E>();
  156.     }
  157.  
  158.     // //////////Inner class ////////////
  159.  
  160.     private class LRIterator<E> implements Iterator<E> {
  161.  
  162.         private SLLNode<E> place, curr;
  163.  
  164.         private LRIterator() {
  165.             place = (SLLNode<E>) first;
  166.             curr = null;
  167.         }
  168.  
  169.         public boolean hasNext() {
  170.             return (place != null);
  171.         }
  172.  
  173.         public E next() {
  174.             if (place == null)
  175.                 throw new NoSuchElementException();
  176.             E nextElem = place.element;
  177.             curr = place;
  178.             place = place.succ;
  179.             return nextElem;
  180.         }
  181.  
  182.         public void remove() {
  183.             // Not implemented
  184.         }
  185.     }
  186.  
  187.     public void mirror() {
  188.         if (first != null) {
  189.             // m=nextsucc, p=tmp,q=next
  190.             SLLNode<E> tmp = first;
  191.             SLLNode<E> newsucc = null;
  192.             SLLNode<E> next;
  193.  
  194.             while (tmp != null) {
  195.                 next = tmp.succ;
  196.                 tmp.succ = newsucc;
  197.                 newsucc = tmp;
  198.                 tmp = next;
  199.             }
  200.             first = newsucc;
  201.         }
  202.  
  203.     }
  204.  
  205.     public void merge(SLL<E> in) {
  206.         if (first != null) {
  207.             SLLNode<E> tmp = first;
  208.             while (tmp.succ != null)
  209.                 tmp = tmp.succ;
  210.             tmp.succ = in.getFirst();
  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 SpojListi {
  233.     public static void JoinLists(SLL<Integer> l1, SLL<Integer> l2) {
  234.         SLL<Integer> lfinal = new SLL<Integer>();
  235.         SLLNode<Integer> n1 = l1.getFirst();
  236.         SLLNode<Integer> n2 = l2.getFirst();
  237.         while (n1 != null && n2 != null) {
  238.             if (n1.element <= n2.element) {
  239.                 lfinal.insertLast(n1.element);
  240.                 n1 = n1.succ;
  241.             } else {
  242.                 lfinal.insertLast(n2.element);
  243.                 n2 = n2.succ;
  244.             }
  245.         }
  246.         while (n1 != null) {
  247.             lfinal.insertLast(n1.element);
  248.             n1 = n1.succ;
  249.         }
  250.         while (n2 != null) {
  251.             lfinal.insertLast(n2.element);
  252.             n2 = n2.succ;
  253.         }
  254.         n1 = lfinal.getFirst();
  255.  
  256.         while (n1.succ != null) {
  257.             if (n1.element.equals(n1.succ.element)) {
  258.                 n1 = n1.succ;
  259.                 System.out.print(n1.element + " ");
  260.                 n1 = n1.succ;
  261.             }
  262.  
  263.             else {
  264.                 System.out.print(n1.element + " ");
  265.                 n1 = n1.succ;
  266.             }
  267.         }
  268.         System.out.print(n1.element);
  269.     }
  270.  
  271.     public static void main(String[] args) throws NumberFormatException, IOException {
  272.  
  273.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  274.         SLL<Integer> l1 = new SLL<Integer>();
  275.         SLL<Integer> l2 = new SLL<Integer>();
  276.         SLL<Integer> lfinal = new SLL<Integer>();
  277.         SLLNode<Integer> n1 = l1.getFirst();
  278.         SLLNode<Integer> n2 = l2.getFirst();
  279.         int first_list_length = Integer.parseInt(br.readLine());
  280.         String[] jazli_string1 = br.readLine().split(" ");
  281.         for (int i = 0; i < first_list_length; i++)
  282.             l1.insertLast(Integer.parseInt(jazli_string1[i]));
  283.         int second_list_length = Integer.parseInt(br.readLine());
  284.         String[] jazli_string2 = br.readLine().split(" ");
  285.         for (int i = 0; i < second_list_length; i++)
  286.             l2.insertLast(Integer.parseInt(jazli_string2[i]));
  287.         JoinLists(l1, l2);
  288.  
  289.     }
  290.  
  291. }
Advertisement
Add Comment
Please, Sign In to add comment