Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Iterator;
- import java.util.NoSuchElementException;
- public class SLLJoinLists {
- public class Node<E> {
- private E element;
- private Node<E> succ;
- public Node(E element, Node<E> succ) {
- this.element = element;
- this.succ = succ;
- }
- }
- public class Iter<E> implements Iterator<E> {
- Node<E> atm, sledno;
- Iter(Node<E> sledno) {
- this.sledno = sledno;
- atm = sledno;
- }
- public boolean hasNext() {
- if (sledno == null)
- return false;
- else
- return true;
- }
- public E next() {
- if (atm == null)
- System.out.println("Atm e null");
- E nov = atm.element;
- if (hasNext() == true) {
- nov = sledno.element;
- atm = sledno;
- sledno = sledno.succ;
- }
- return nov;
- }
- @Override
- public void remove() {
- }
- }
- public class Lista<E> {
- Node<E> prv, posleden;
- Lista() {
- prv = null;
- posleden = null;
- }
- Iter<E> iterator() {
- return new Iter<E>(prv);
- }
- public void insertLast(E element) {
- if (prv == null) {
- Node<E> novo = new Node<E>(element, prv);
- prv = novo;
- posleden = novo;
- } else {
- Node<E> novo = new Node<E>(element, null);
- posleden.succ = novo;
- posleden = novo;
- }
- }
- }
- public <E extends Comparable<E>> Lista<E> merge(Lista<E> prva,
- Lista<E> vtora) {
- Lista<E> spoena = new Lista<E>();
- Node<E> jazol1 = prva.prv, jazol2 = vtora.prv;
- while (jazol1 != null && jazol2 != null) {
- if ((Integer) jazol1.element < (Integer) jazol2.element) {
- if (spoena.posleden != null
- && spoena.posleden.element == jazol1.element) {
- jazol1 = jazol1.succ;
- continue;
- }
- spoena.insertLast(jazol1.element);
- jazol1 = jazol1.succ;
- } else if ((Integer) jazol1.element > (Integer) jazol2.element) {
- if (spoena.posleden != null
- && spoena.posleden.element == jazol2.element) {
- jazol2 = jazol2.succ;
- continue;
- }
- spoena.insertLast(jazol2.element);
- jazol2 = jazol2.succ;
- } else {
- if (spoena.posleden != null
- && spoena.posleden.element == jazol2.element) {
- jazol1 = jazol1.succ;
- jazol2 = jazol2.succ;
- continue;
- }
- spoena.insertLast(jazol1.element);
- jazol1 = jazol1.succ;
- jazol2 = jazol2.succ;
- }
- }
- while (jazol1 != null) {
- if (spoena.posleden != null
- && spoena.posleden.element != jazol1.element)
- spoena.insertLast(jazol1.element);
- jazol1 = jazol1.succ;
- }
- while (jazol2 != null) {
- if (spoena.posleden != null
- && spoena.posleden.element != jazol2.element)
- spoena.insertLast(jazol2.element);
- jazol2 = jazol2.succ;
- }
- return spoena;
- }
- 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(" ");
- SLLJoinLists join = new SLLJoinLists();
- Lista<Integer> lista1 = join.new Lista<Integer>();
- Lista<Integer> lista2 = join.new Lista<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]));
- }
- Lista<Integer> spoeni = join.merge(lista1, 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