Latkoski

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

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