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.NoSuchElementException;
- import java.util.Iterator;
- class SLLNode<E> {
- protected E element;
- protected SLLNode<E> successor;
- public SLLNode(E element, SLLNode<E> successor) {
- this.element = element;
- this.successor = successor;
- }
- }
- class SLL<E> {
- private SLLNode<E> first;
- public SLL() {
- first = null;
- }
- public SLLNode<E> getFirst() {
- return first;
- }
- public void insertFirst(E o) {
- SLLNode<E> insert = new SLLNode<E>(o, first);
- first = insert;
- }
- public void insertLast(E o) {
- if(first != null) {
- SLLNode<E> tmp = first;
- while(tmp.successor != null)
- tmp = tmp.successor;
- SLLNode<E> insert = new SLLNode<E>(o, null);
- tmp.successor = insert;
- } else insertFirst(o);
- }
- public Iterator<E> iterator() {
- return new LRIterator();
- }
- private class LRIterator<E> implements Iterator<E> {
- private SLLNode<E> place, current;
- private LRIterator() {
- place = (SLLNode<E>)first;
- current = null;
- }
- public boolean hasNext() {
- return place != null;
- }
- public E next() {
- if(place == null)
- throw new NoSuchElementException();
- E nextElement = place.element;
- current = place;
- place = place.successor;
- return nextElement;
- }
- }
- }
- public class SpecialSLLJoin {
- public static SLL<Integer> specialJoin(SLL<Integer> listOne, SLL<Integer> listTwo) {
- SLLNode<Integer> listOneNode = listOne.getFirst(), listTwoNode = listTwo.getFirst();
- SLL<Integer> specialOne = new SLL<Integer>();
- while(listOneNode != null&&listTwoNode != null) {
- specialOne.insertLast(listOneNode.element);
- if(listOneNode.successor != null) {
- listOneNode = listOneNode.successor;
- specialOne.insertLast(listOneNode.element);
- }
- listOneNode = listOneNode.successor;
- specialOne.insertLast(listTwoNode.element);
- if(listTwoNode.successor != null) {
- listTwoNode = listTwoNode.successor;
- specialOne.insertLast(listTwoNode.element);
- }
- listTwoNode = listTwoNode.successor;
- }
- if(listOneNode != null) {
- while(listOneNode != null) {
- specialOne.insertLast(listOneNode.element);
- listOneNode = listOneNode.successor;
- }
- }
- if(listTwoNode != null) {
- while(listTwoNode != null) {
- specialOne.insertLast(listTwoNode.element);
- listTwoNode = listTwoNode.successor;
- }
- }
- return specialOne;
- }
- public static void main(String[] args) throws IOException{
- SLL<Integer> lista1 = new SLL<Integer>();
- 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 = specialJoin(lista1,lista2);
- Iterator<Integer> it = spoeni.iterator();
- while(it.hasNext()) {
- System.out.print(it.next());
- if(it.hasNext())
- System.out.print(" ");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment