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 extends Comparable<E>>{
- private SLLNode<E> first;
- public SLL(){
- this.first=null;
- }
- public SLLNode<E> getFirst(){
- return first;
- }
- public void insertLast(E o)
- {
- if (first==null){
- SLLNode<E> ins = new SLLNode<E>(o, null);
- first=ins;
- }
- else{
- SLLNode<E> tmp = first;
- while (tmp.succ!=null)
- tmp=tmp.succ;
- tmp.succ = new SLLNode<E>(o, null);
- }
- }
- public Iterator<E> iterator() {
- return new LRIterator<E>();
- }
- private class LRIterator<E> implements Iterator<E> {
- private SLLNode<E> e, curr;
- private LRIterator() {
- e = (SLLNode<E>) first;
- curr = null;
- }
- public boolean hasNext() {
- return (e != null);
- }
- public E next() {
- if (e == null)
- throw new NoSuchElementException();
- E nextElem = e.element;
- curr = e;
- e = e.succ;
- return nextElem;
- }
- }
- public SLL<E> joinLists(SLL<E> lista2){
- SLL<E> toReturn = new SLL<E>();
- SLLNode<E> jazol1=first, jazol2=lista2.getFirst();
- E sameValueCheck=null;
- while (jazol1!=null&&jazol2!= null)
- {
- if (jazol1.element.compareTo(jazol2.element)<0)
- {
- if (sameValueCheck == null || jazol1.element!=sameValueCheck)
- toReturn.insertLast(jazol1.element);
- sameValueCheck=jazol1.element;
- jazol1=jazol1.succ;
- }
- else
- {
- if (sameValueCheck == null || jazol2.element!=sameValueCheck)
- toReturn.insertLast(jazol2.element);
- sameValueCheck=jazol2.element;
- jazol2=jazol2.succ;
- }
- }
- while (jazol1!=null)
- {
- if (sameValueCheck == null || jazol1.element!=sameValueCheck)
- toReturn.insertLast(jazol1.element);
- sameValueCheck=jazol1.element;
- jazol1=jazol1.succ;
- }
- while (jazol2!=null)
- {
- if (sameValueCheck == null || jazol2.element!=sameValueCheck)
- toReturn.insertLast(jazol2.element);
- sameValueCheck=jazol2.element;
- jazol2=jazol2.succ;
- }
- return toReturn;
- }
- }
- public class SLLJoinLists {
- 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();
- String[] pomniza = s.split(" ");
- SLL<Integer> lista1=new SLL<Integer>(), lista2=new SLL<Integer>();
- 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]));
- }
- SLL<Integer> spoeni = new SLL<Integer>();
- 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