Advertisement
ivana_andreevska

Sporedba na dve listi

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