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> {
- // atributi
- protected E element;// kreirame jazel so seuste nepoznat value E
- protected SLLNode<E> succ;// link do sledniot jazel
- // konstruktor
- public SLLNode(E element, SLLNode<E> succ) {
- this.element = element;
- this.succ = succ;
- }
- }
- class SLL<E> {
- // atributi
- private SLLNode<E> first;// prv jazel
- // konstruktor - NE PRIMA ARGUMENTI
- public SLL() {
- // kreiranje prazna lista
- this.first = null;// go postavuvame prviot jazel da pokazuva na null
- }
- // vrakja SLLNode
- public SLLNode<E> getFirst() {
- return first;
- }
- public void insertFirst(E o) {
- // napravi nov jazel koj ima value o i pokazuva na first
- SLLNode<E> ins = new SLLNode<E>(o, first);
- first = ins;// prviot jazel e noviot jazel
- }
- public void insertLast(E o) {
- if (first != null) {// ako ne e posleden
- SLLNode<E> tmp = first;// kreiram temporary jazel
- while (tmp.succ != null)// dodeka temporary ne e posleden
- tmp = tmp.succ;// izminuvaj ja listata, noviot temporary e
- // sledniot
- // ako stasas do posledniot
- // napravi nov jazel koj ima value o i pokazuva na null
- SLLNode<E> ins = new SLLNode<E>(o, null);
- tmp.succ = ins;// sledniot jazel na posledniot neka bide noviot
- // jazel
- } else {// ako prviot e i posleden i e ednakov na null
- insertFirst(o);// vnesi kako prv i edinstven element
- }
- }
- // LOGIKA
- public SLL<E> specialJoin(SLL<E> in2) {
- SLL<E> rezultat = new SLL<E>();
- SLLNode<E> nodeLista1 = this.getFirst();
- SLLNode<E> nodeLista2 = in2.getFirst();
- boolean flag = true;
- boolean flag1 = true;
- while (nodeLista1 != null && nodeLista2 != null) {
- if (nodeLista1.succ == null && nodeLista2.succ == null) {
- rezultat.insertLast(nodeLista1.element);
- rezultat.insertLast(nodeLista2.element);
- return rezultat;
- }
- // ako sledbenikot na first ne e null
- if (nodeLista1.succ != null) {
- rezultat.insertLast(nodeLista1.element);
- nodeLista1 = nodeLista1.succ;// odi na sleden
- rezultat.insertLast(nodeLista1.element);
- nodeLista1 = nodeLista1.succ;
- } else if (flag) {// ako sledbenikot e null, vmetni go first i stavi
- // lock
- rezultat.insertLast(nodeLista1.element);
- flag = false;
- }
- // ako sledbenikot na first ne e null
- if (nodeLista2.succ != null) {
- rezultat.insertLast(nodeLista2.element);
- nodeLista2 = nodeLista2.succ;// odi na sleden
- rezultat.insertLast(nodeLista2.element);
- nodeLista2 = nodeLista2.succ;
- } else if (flag1) {// ako e null, vmetni go first i stavi lock
- rezultat.insertLast(nodeLista2.element);
- flag1 = false;
- }
- }
- if (flag)
- if (nodeLista1 != null) {// lista2 dosla do kraj
- while (nodeLista1 != null) {
- rezultat.insertLast(nodeLista1.element);
- nodeLista1 = nodeLista1.succ;
- }
- }
- if (flag1)
- if (nodeLista2 != null) {// lista1 dosla do kraj
- while (nodeLista2 != null) {
- rezultat.insertLast(nodeLista2.element);
- nodeLista2 = nodeLista2.succ;
- }
- }
- return rezultat;
- }
- public Iterator<E> iterator() {
- // vraka iterator koj gi posetuva site elementi na listata od levo na
- // desno
- return new LRIterator<E>();
- }
- // //////////Inner class ////////////
- 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.element;
- prev = curr;
- curr = place;
- place = place.succ;
- return nextElem;
- }
- public void remove() {
- // Not implemented
- }
- }
- }
- public class SpecialSLLJoin {
- 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>();
- 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(" ");
- SLL<Integer> lista2 = new SLL<Integer>();
- for (int i = 0; i < N; i++) {
- lista2.insertLast(Integer.parseInt(pomniza[i]));
- }
- SLL<Integer> spoeni = lista1.specialJoin(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