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;
- class SLLNode<E>
- {
- E element;
- SLLNode<E> succ;
- SLLNode(){}
- SLLNode(E element, SLLNode<E> succ)
- {
- this.element=element;
- this.succ=succ;
- }
- }
- class SLL<E>
- {
- SLLNode<E> first;
- SLL()
- {
- first=null;
- }
- public void insertFirst(E element)
- {
- SLLNode<E> nov = new SLLNode<>(element,first);
- first = nov;
- }
- public void insertLast(E element)
- {
- if(first==null) insertFirst(element);
- else
- {
- SLLNode<E> dvizi = first;
- while(dvizi.succ != null)
- {
- dvizi = dvizi.succ;
- }
- SLLNode<E> last = new SLLNode<>(element,null);
- dvizi.succ=last;
- }
- }
- public void insertAfter(E element, SLLNode<E>node)
- {
- if(node!=null){
- SLLNode<E> nov = new SLLNode<>(element,node.succ);
- node.succ = nov;
- }
- }
- public int getLength()
- {
- SLLNode<E> dvizi = first;
- int length = 0;
- while(dvizi!=null)
- {
- length++;
- dvizi = dvizi.succ;
- }
- return length;
- }
- public String toString()
- {
- String s = new String();
- SLLNode<E> dvizi = first;
- while(dvizi!=null)
- {
- s = s+dvizi.element+" ";
- dvizi=dvizi.succ;
- }
- return s;
- }
- public SLLNode<E> getFirst()
- {
- return first;
- }
- }
- public class SpecialSLLJoin {
- private static SLL<Integer> specialJoin(SLL<Integer> lista1,
- SLL<Integer> lista2) {
- SLL<Integer> spoenaLista = new SLL<>();
- SLLNode<Integer> firstL1 = lista1.getFirst();
- SLLNode<Integer> firstL2 = lista2.getFirst();
- int br1,br2;
- while((firstL1 != null)||(firstL2!=null))
- {
- br1=0;br2=0;
- while((firstL1!=null)&&(br1!=2))
- {
- spoenaLista.insertLast(firstL1.element);
- firstL1 = firstL1.succ;
- br1++;
- }
- while((firstL2!=null)&&(br2!=2))
- {
- spoenaLista.insertLast(firstL2.element);
- firstL2 = firstL2.succ;
- br2++;
- }
- }
- return spoenaLista;
- }
- public static void main(String[] args) throws IOException{
- SLL<Integer> lista1 = new SLL<>();
- SLL<Integer> lista2 = new SLL<>();
- 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);
- System.out.println(spoeni);
- }
- }
Add Comment
Please, Sign In to add comment