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 extends Comparable<E>> {
- protected E element;
- protected SLLNode<E> succ;
- public SLLNode(E elem, SLLNode<E> succ) {
- this.element = elem;
- this.succ = succ;
- }
- @Override
- public String toString() {
- return element.toString();
- }
- }
- class SLL<E extends Comparable<E>> {
- private SLLNode<E> first;
- public SLL() {
- // Construct an empty SLL
- this.first = null;
- }
- public void deleteList() {
- first = null;
- }
- public int length() {
- int ret;
- if (first != null) {
- SLLNode<E> tmp = first;
- ret = 1;
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret++;
- }
- return ret;
- } else
- return 0;
- }
- @Override
- public String toString() {
- String ret = new String();
- if (first != null) {
- SLLNode<E> tmp = first;
- ret += tmp + "->";
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret += tmp + "->";
- }
- } else
- ret = "Prazna lista!!!";
- return ret;
- }
- 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> ins = new SLLNode<E>(o, node.succ);
- node.succ = ins;
- } else {
- System.out.println("Dadenot jazol e null");
- }
- }
- public void insertBefore(E o, SLLNode<E> before) {
- if (first != null) {
- SLLNode<E> tmp = first;
- if (first == before) {
- this.insertFirst(o);
- return;
- }
- // ako first!=before
- 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");
- }
- } 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> getFirst() {
- return first;
- }
- public Iterator<E> iterator() {
- // Return an iterator that visits all elements of this list, in
- // left-to-right order.
- return new LRIterator<E>();
- }
- // //////////Inner class ////////////
- private class LRIterator<E extends Comparable<E>> implements Iterator<E> {
- private SLLNode<E> place;
- @SuppressWarnings("unchecked")
- private LRIterator() {
- place = (SLLNode<E>) first;
- }
- public boolean hasNext() {
- return (place != null);
- }
- public E next() {
- if (place == null)
- throw new NoSuchElementException();
- E nextElem = place.element;
- place = place.succ;
- return nextElem;
- }
- @Override
- public void remove() {
- // TODO Auto-generated method stub
- }
- }
- public SLL<E> joinLists(SLL<E> in) {
- // Vashiot kod tuka...
- int dolzinaLista1 = this.length();
- int dolzinaLista2 = in.length();
- if (dolzinaLista1 <= dolzinaLista2)
- return makeTheJoin(this, in, dolzinaLista1);
- else
- return makeTheJoin(this, in, dolzinaLista2);
- }
- private SLL<E> makeTheJoin(SLL<E> pomalataLista, SLL<E> pogolemataLista,
- int pomala) {
- SLL<E> res = new SLL<E>();
- SLLNode<E> tmp1 = pomalataLista.getFirst();
- SLLNode<E> tmp2 = pogolemataLista.getFirst();
- for (int i = 1; i <= pomala; ++i) {
- if (i % 2 != 0) {
- res.insertLast(tmp1.element);
- tmp1 = tmp1.succ;
- if (tmp1 != null) {
- res.insertLast(tmp1.element);
- tmp1 = tmp1.succ;
- }
- } else if (i % 2 == 0) {
- res.insertLast(tmp2.element);
- tmp2 = tmp2.succ;
- if (tmp2 != null) {
- res.insertLast(tmp2.element);
- tmp2 = tmp2.succ;
- }
- }
- }
- while (tmp2 != null) {
- res.insertLast(tmp2.element);
- tmp2 = tmp2.succ;
- }
- while (tmp1 != null) {
- res.insertLast(tmp1.element);
- tmp1 = tmp1.succ;
- }
- return res;
- }
- }
- public class SLLJoinLists {
- public static void main(String[] args) throws IOException {
- SLL<Integer> lista1 = new SLL<Integer>(), lista2 = new SLL<Integer>();
- 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]));
- }
- 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
Advertisement