Advertisement
metalni

APS Labs 2 Spoj listi

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