Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Iterator;
- import java.util.NoSuchElementException;
- class SLLNode<E>
- {
- protected E data;
- protected SLLNode<E> next;
- public SLLNode(E data, SLLNode<E> next)
- {
- this.data = data;
- this.next=next;
- }
- }
- class SLL<E extends Comparable<E>>
- {
- private SLLNode<E> first;
- public SLL()
- {
- first = null;
- }
- public SLLNode<E> getFirst()
- {
- return first;
- }
- public void insertFirst(E obj)
- {
- SLLNode<E> ins = new SLLNode<>(obj,first);
- first = ins;
- }
- public void insertLast(E obj)
- {
- if(first!=null)
- {
- SLLNode<E> temp = first;
- while(temp.next!=null) temp=temp.next;
- SLLNode<E> ins = new SLLNode<E>(obj,null);
- temp.next=ins;
- }
- else
- {
- insertFirst(obj);
- }
- }
- public SLL<E> joinLists(SLL<E> lista2)
- {
- SLL<E> result = new SLL<>();
- SLLNode<E> jazol1 = getFirst();
- SLLNode<E> jazol2 = lista2.getFirst();
- while(jazol1!=null&&jazol2!=null)
- {
- if(jazol1.next!=null&&jazol1.data.compareTo(jazol1.next.data)==0)
- {
- jazol1 = jazol1.next;
- }
- else if(jazol2.next!=null&&jazol2.data.compareTo(jazol2.next.data)==0)
- {
- jazol2 = jazol2.next;
- }
- else if(jazol1.data.compareTo(jazol2.data)==0)
- {
- jazol2 = jazol2.next;
- }
- else if(jazol1.data.compareTo(jazol2.data)<0)
- {
- result.insertLast(jazol1.data);
- jazol1 = jazol1.next;
- }
- else
- {
- result.insertLast(jazol2.data);
- jazol2 = jazol2.next;
- }
- }
- if(jazol1!=null)
- {
- while(jazol1!=null)
- {
- if(jazol1.next!=null&&jazol1.data.compareTo(jazol1.next.data)==0)
- {
- jazol1 =jazol1.next;
- }
- else
- {
- result.insertLast(jazol1.data);
- jazol1=jazol1.next;
- }
- }
- }
- if(jazol2!=null)
- {
- while(jazol2!=null)
- {
- if(jazol2.next!=null&&jazol2.data.compareTo(jazol2.next.data)==0)
- {
- jazol2 =jazol2.next;
- }
- else
- {
- result.insertLast(jazol2.data);
- jazol2=jazol2.next;
- }
- }
- }
- return result;
- }
- public Iterator<E> iterator () {
- return new LRIterator<E>();
- }
- private class LRIterator<E> implements Iterator<E> {
- private SLLNode<E> place, prev, curr;
- private LRIterator() {
- place = (SLLNode<E>) first;
- curr = prev = null;
- }
- public boolean hasNext() {
- return (place != null);
- }
- public E next() {
- if (place == null)
- throw new NoSuchElementException();
- E nextElem = place.data;
- prev = curr;
- curr = place;
- place = place.next;
- return nextElem;
- }
- }
- }
- public class SLLJoinLists {
- public static void main(String[] args) throws IOException {
- SLL<Integer> lista1 = new SLL<>();
- SLL<Integer> lista2 = new SLL<>();
- SLL<Integer> spoeni = new SLL<>();
- BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
- String s = stdin.readLine();
- int N = Integer.parseInt(s);
- s = stdin.readLine();
- String[] pomniza = s.split(" ");
- for (int i = 0; i < N; i++) {
- lista1.insertLast(Integer.parseInt(pomniza[i]));
- }
- s = stdin.readLine();
- N = Integer.parseInt(s);
- s = stdin.readLine();
- pomniza = s.split(" ");
- for (int i = 0; i < N; i++) {
- lista2.insertLast(Integer.parseInt(pomniza[i]));
- }
- spoeni = lista1.joinLists(lista2);
- Iterator<Integer> it = spoeni.iterator();
- while (it.hasNext()) {
- System.out.print(it.next());
- if(it.hasNext())
- System.out.print(" ");
- }
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement