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 element;
- protected SLLNode<E> succ;
- public SLLNode(E element, SLLNode<E> succ) {
- this.element = element;
- this.succ = succ;
- }
- }
- class SLL<E> {
- private SLLNode<E> first;
- public SLL(SLLNode<E> first) {
- this.first = null;
- }
- public SLL()
- {
- this.first = null;
- }
- public void insertFirst(E o)
- {
- SLLNode<E> ins = new SLLNode<E>(o,first);
- first = ins;
- }
- public void insertAfter(E o, SLLNode<E> node)
- {
- if(node!=null)
- {
- SLLNode<E> tmp = new SLLNode<E>(o,node.succ);
- node.succ = tmp;
- }
- }
- public void deleteFirst()
- {
- if(first != null)
- {
- SLLNode<E> tmp = first;
- first = first.succ;
- }
- }
- public void delete(SLLNode<E> node)
- {
- if(first != null)
- {
- SLLNode<E> tmp = first;
- while(tmp.succ != node && tmp.succ.succ != null)
- {
- tmp = tmp.succ;
- }
- if(tmp.succ == node)
- {
- tmp.succ = tmp.succ.succ;
- }
- }
- }
- public int length()
- {
- int br;
- if(first != null) {
- SLLNode<E> tmp = first;
- br = 1;
- while (tmp.succ != null) {
- br++;
- tmp = tmp.succ;
- }
- return br;
- }
- else
- return 0;
- }
- public void insertBefore(E o, SLLNode<E> before)
- {
- if(first != null)
- {
- SLLNode<E> tmp = first;
- if(first == before)
- {
- this.insertFirst(o);
- return;
- }
- else
- {
- while(tmp.succ != before)
- {
- tmp = tmp.succ;
- }
- if(tmp.succ == before)
- {
- SLLNode<E> ins = new SLLNode<E>(o,before);
- tmp.succ = ins;
- }
- else
- {
- System.out.println("Elementot ne postoi vo listata\n");
- }
- }
- }
- else
- System.out.println("Listata e prazna");
- }
- public void insertLast(E o)
- {
- if(first!=null)
- {
- SLLNode<E> tmp = first;
- while(tmp.succ != null)
- {
- tmp = tmp.succ;
- }
- SLLNode<E> ins = new SLLNode<E>(o,null);
- tmp.succ = ins;
- }
- else
- insertFirst(o);
- }
- public SLLNode<E> find(E o)
- {
- if(first!=null)
- {
- SLLNode<E> tmp = first;
- while(tmp.succ != null && tmp.element != o)
- {
- tmp = tmp.succ;
- }
- if(tmp.element == o )
- return tmp;
- else
- System.out.println("Elementot ne postoi vo listata\n");
- }
- else if(first == o)
- return first;
- else
- System.out.println("Listata e prazna\n");
- return null;
- }
- public SLLNode<E> getFirst()
- {
- return first;
- }
- public void merge(SLL<E> in)
- {
- if(first != null)
- {
- SLLNode<E> tmp = first;
- while(tmp.succ != null)
- {
- tmp = tmp.succ;
- }
- tmp = in.getFirst();
- }
- else
- first = in.getFirst();
- }
- public Iterator<E> iterator() {
- return new LRIterator<E>();
- }
- }
- class LRIterator<E> implements Iterator<E>
- {
- private SLLNode<E> place, prev, curr;
- LRIterator() {
- place = (SLLNode<E>) curr;
- curr = prev = null;
- }
- public boolean hasNext() {
- return (place != null);
- }
- public E next() {
- if (place == null)
- throw new NoSuchElementException();
- E nextElem = place.element;
- prev = curr;
- curr = place;
- place = place.succ;
- return nextElem;
- }
- }
- public class Main {
- public static void main(String[] args) throws IOException {
- BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
- String s = stdin.readLine();
- int N = Integer.parseInt(s);
- s = stdin.readLine();
- SLL<Integer> lista1 = new SLL<>();
- String[] pomniza = s.split(" ");
- for (int i = 0; i < N; i++) {
- lista1.insertLast(Integer.parseInt(pomniza[i]));
- }
- // s = stdin.readLine();
- String p = stdin.readLine();
- int M = Integer.parseInt(p);
- p = stdin.readLine();
- SLL<Integer> lista2 = new SLL<>();
- pomniza = p.split(" ");
- for (int i = 0; i < M; i++) {
- lista2.insertLast(Integer.parseInt(pomniza[i]));
- }
- SLL<Integer> spoeni = new SLL<>();
- SLLNode<Integer> first = lista1.getFirst();
- SLLNode<Integer> first2 = lista2.getFirst();
- while(first != null && first2 != null)
- {
- if(first.element.compareTo(first2.element) < 0)
- {
- spoeni.insertLast(first.element);
- first = first.succ;
- }
- else if(first.element.compareTo(first2.element) > 0)
- {
- spoeni.insertLast(first2.element);
- first2 = first2.succ;
- }
- else if(first.element.compareTo(first2.element) == 0)
- {
- first = first.succ;
- }
- }
- if(first != null)
- {
- while(first != null)
- {
- spoeni.insertLast(first.element);
- first = first.succ;
- }
- }
- if(first2 != null)
- {
- while(first2 != null)
- {
- spoeni.insertLast(first2.element);
- first2 = first2.succ;
- }
- }
- //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