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;
- class SLLNode {
- protected int n;
- protected SLLNode succ;
- public SLLNode(int n, SLLNode succ) {
- this.n = n;
- this.succ = succ;
- }
- public int getN() {
- return n;
- }
- public void setN(int n) {
- this.n = n;
- }
- public SLLNode getSucc() {
- return succ;
- }
- public void setSucc(SLLNode succ) {
- this.succ = succ;
- }
- }
- class SLL {
- protected SLLNode first;
- public SLL() {
- first = null;
- }
- public void insertFirst(int o) {
- SLLNode ins = new SLLNode(o, first);
- first = ins;
- }
- public void delete(SLLNode node) {
- if (first != null) {
- SLLNode tmp = first;
- while (tmp.succ != node&&tmp.succ.succ != null)
- tmp = tmp.succ;
- if (tmp.succ == node) {
- tmp.succ = tmp.succ.succ;
- }
- // else throw Exception;
- }
- // else throw Exception;
- }
- public SLLNode first() {
- return first;
- }
- public void insertLast(int o) {
- if (first != null) {
- SLLNode tmp = first;
- while (tmp.succ != null)
- tmp = tmp.succ;
- SLLNode ins = new SLLNode(o, null);
- tmp.succ = ins;
- } else {
- insertFirst(o);
- }
- }
- public SLL spoeni(SLL A) {
- SLLNode tmp1 =first;
- SLLNode tmp2 = A.first;
- SLL spoena = new SLL();
- while (tmp1 != null) {
- int i=0;
- if(tmp2==null){
- spoena.insertLast(tmp1.n);
- tmp1=tmp1.succ;
- }
- else{
- if(tmp1.succ==null&&tmp2==null)
- {
- spoena.insertLast(tmp1.n);
- tmp1=tmp1.succ;
- }
- if(tmp1.succ!=null)
- {
- spoena.insertLast(tmp1.n);
- spoena.insertLast(tmp1.succ.n);
- tmp1=tmp1.succ.succ;
- }
- else{
- spoena.insertLast(tmp1.n);
- tmp1=tmp1.succ;
- }
- while(tmp2.succ!=null)
- {
- spoena.insertLast(tmp2.n); i++;
- tmp2=tmp2.succ;
- if(i==2&&tmp1 !=null) break;
- }}
- if(i==1 && tmp2!=null){
- spoena.insertLast(tmp2.n);
- tmp2=tmp2.succ;}
- }
- if(tmp1==null && tmp2 != null)spoena.insertLast(tmp2.n);
- if(tmp2==null && tmp1 != null)spoena.insertLast(tmp1.n);
- return spoena;
- }
- public void pecati() {
- SLLNode tmp=first;
- while(tmp!=null) {
- System.out.printf("%d ",tmp.n);
- tmp=tmp.succ;
- }
- }
- }
- 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();
- SLL lista1=new SLL();
- SLL lista2=new SLL();
- 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 spoena=new SLL();
- spoena=lista1.spoeni(lista2);
- spoena.pecati();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement