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<E> {
- protected E element;
- protected SLLNode<E> succ;
- public SLLNode(E element,SLLNode<E> succ) {
- this.element=element;
- this.succ=succ;
- }
- @Override
- public String toString() {
- return element.toString();
- }
- }
- class SLL<E extends Comparable<E>> {
- private SLLNode<E> first;
- public SLL() {
- this.first=null;
- }
- public SLLNode<E> getFirst() {
- return first;
- }
- public void insertLast(E ob) {
- if(first!=null) {
- SLLNode<E> temp = first;
- while(temp.succ != null) {
- temp = temp.succ;
- }
- SLLNode<E> ins = new SLLNode<E>(ob,null);
- temp.succ = ins;
- } else {
- SLLNode<E> temp = new SLLNode<E>(ob,first);
- first=temp;
- }
- }
- public SLL<E> specialJoin(SLL<E> list2) {
- SLL<E> rezultat = new SLL<E>();
- SLLNode<E> jazol1 = this.getFirst();
- SLLNode<E> jazol2 = list2.getFirst();
- int counter=0;
- while(jazol1 != null&&jazol2 != null) {
- if(counter<2) {
- rezultat.insertLast(jazol1.element);
- jazol1=jazol1.succ;
- counter++;
- } else if(counter>=2) {
- rezultat.insertLast(jazol2.element);
- jazol2=jazol2.succ;
- counter++;
- if(counter>=4) {
- counter = 0;
- }
- }
- }
- if(jazol1 != null){
- while(jazol1 !=null){
- rezultat.insertLast(jazol1.element);
- jazol1=jazol1.succ;
- }
- }
- if(jazol2 != null){
- while(jazol2 !=null){
- rezultat.insertLast(jazol2.element);
- jazol2=jazol2.succ;
- }
- }
- return rezultat;
- }
- @Override
- public String toString() {
- String print = new String();
- if(first != null) {
- SLLNode<E> temp = first;
- print+=first;
- while(temp.succ != null) {
- temp=temp.succ;
- print+=" " + temp;
- }
- }
- return print;
- }
- }
- public class SpecialSLLJoin {
- 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]));
- }
- //spoeni = specialJoin(lista1,lista2);
- SLL<Integer> spoeni = lista1.specialJoin(lista2);
- System.out.println(spoeni);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement