Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.NoSuchElementException;
- import java.util.Iterator;
- 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 elem, SLLNode<E> succ) {
- this.element = elem;
- this.succ = succ;
- }
- @Override
- public String toString() {
- return element.toString();
- }
- }
- class SLL<E> {
- private SLLNode<E> first;
- public SLL() {// Construct an empty SLL
- this.first = null;
- }
- public void deleteList() {
- first = null;
- }
- public int length() {
- int ret;
- if (first != null) {
- SLLNode<E> tmp = first;
- ret = 1;
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret++;
- }
- return ret;
- } else
- return 0;
- }
- @Override
- public String toString() {
- String ret = new String();
- if (first != null) {
- SLLNode<E> tmp = first;
- ret += tmp + "->";
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret += tmp + "->";
- }
- } else
- ret = "Prazna lista!!!";
- return ret;
- }
- public void insertFirst(E o) {
- SLLNode<E> ins = new SLLNode<E>(o, first);
- first = ins;
- }
- public void insertAfter(E o, SLLNode<E> node) {
- if (node != null) {
- SLLNode<E> ins = new SLLNode<E>(o, node.succ);
- node.succ = ins;
- } else {
- System.out.println("Dadenot jazol e null");
- }
- }
- public void insertBefore(E o, SLLNode<E> before) {
- if (first != null) {
- SLLNode<E> tmp = first;
- if (first == before) {
- this.insertFirst(o);
- return;
- }//ako first!=before
- while (tmp.succ != before)
- tmp = tmp.succ;
- if (tmp.succ == before) {
- SLLNode<E> ins = new SLLNode<E>(o, before);
- tmp.succ = ins;
- } else {
- System.out.println("Elementot ne postoi vo listata");
- }
- } else {
- System.out.println("Listata e prazna");
- }
- }
- public void insertLast(E o) {
- if (first != null) {
- SLLNode<E> tmp = first;
- while (tmp.succ != null)
- tmp = tmp.succ;
- SLLNode<E> ins = new SLLNode<E>(o, null);
- tmp.succ = ins;
- } else {
- insertFirst(o);
- }
- }
- public E deleteFirst() {
- if (first != null) {
- SLLNode<E> tmp = first;
- first = first.succ;
- return tmp.element;
- } else {
- System.out.println("Listata e prazna");
- return null;
- }
- }
- public E delete(SLLNode<E> node) {
- if (first != null) {
- SLLNode<E> tmp = first;
- if (first == node) {
- return this.deleteFirst();
- }
- while (tmp.succ != node && tmp.succ.succ != null)
- tmp = tmp.succ;
- if (tmp.succ == node) {
- tmp.succ = tmp.succ.succ;
- return node.element;
- } else {
- System.out.println("Elementot ne postoi vo listata");
- return null;
- }
- } else {
- System.out.println("Listata e prazna");
- return null;
- }
- }
- public SLLNode<E> getFirst() {
- return first;
- }
- public SLLNode<E> find(E o) {
- if (first != null) {
- SLLNode<E> tmp = first;
- while (tmp.element != o && tmp.succ != null)
- tmp = tmp.succ;
- if (tmp.element == o) {
- return tmp;
- } else {
- System.out.println("Elementot ne postoi vo listata");
- }
- } else {
- System.out.println("Listata e prazna");
- }
- return first;
- }
- public Iterator<E> iterator() {
- // Return an iterator that visits all elements of this list, in left-to-right order.
- return new LRIterator<E>();
- } // //////////Inner class ////////////
- private class LRIterator<E> implements Iterator<E> {
- private SLLNode<E> place, curr;
- private LRIterator() {
- place = (SLLNode<E>) first;
- curr = null;
- }
- public boolean hasNext() {
- return (place != null);
- }
- public E next() {
- if (place == null)
- throw new NoSuchElementException();
- E nextElem = place.element;
- curr = place;
- place = place.succ;
- return nextElem;
- }
- }
- public void mirror() {
- if (first != null) {
- //m=nextsucc, p=tmp,q=next
- SLLNode<E> tmp = first;
- SLLNode<E> newsucc = null;
- SLLNode<E> next;
- while (tmp != null) {
- next = tmp.succ;
- tmp.succ = newsucc;
- newsucc = tmp;
- tmp = next;
- }
- first = newsucc;
- }
- }
- public void merge(SLL<E> in) {
- if (first != null) {
- SLLNode<E> tmp = first;
- while (tmp.succ != null)
- tmp = tmp.succ;
- tmp.succ = in.getFirst();
- } else {
- first = in.getFirst();
- }
- }
- public void reverse(SLL list) {
- SLLNode<E> counter = list.getFirst();
- SLL<E> novo = new SLL<>();
- while (counter != null) {
- novo.insertLast((E) counter);
- counter = counter.succ;
- }
- counter = novo.getFirst();
- while (counter != null) {
- System.out.print(counter + " -> ");
- counter = counter.succ;
- }
- }
- public SLL sortiraj() {
- SLLNode<E> temp = getFirst();// node temp = head;
- while (temp != null) { // Traverse the List
- SLLNode<E> min = temp; // node min = temp;
- SLLNode<E> r = temp.succ;// node r = temp.next;
- // Traverse the unsorted sublist
- while (r != null) {
- if ((int) min.element > (int) r.element) // if (min.data > r.data)
- min = r;
- r = r.succ; //r = r.next;
- }
- // Swap Data
- E x = temp.element; //int x = temp.data;
- temp.element = min.element; //temp.data = min.data;
- min.element = x; //min.data = x;
- temp = temp.succ; //temp = temp.next;
- }
- return this;
- }
- public void removeDuplicates() {
- SLLNode<E> amanvise = getFirst();
- while(amanvise.succ!=null) {
- if(amanvise.element == amanvise.succ.element)
- delete(amanvise);
- amanvise = amanvise.succ;
- }
- }
- public SLL specialJoin(SLL list1, SLL list2) {
- SLLNode<E> counter1 = list1.getFirst();
- SLLNode<E> counter2 = list2.getFirst();
- SLL<E> nova = new SLL<>();
- int help = 0;
- while(counter1!=null && counter1.succ!=null && counter2!=null && counter2.succ!=null) {
- nova.insertLast((E) counter1); //first two from first list
- nova.insertLast((E) counter1.succ); //first two from first list
- nova.insertLast((E) counter2); //then following those, two from the second list
- nova.insertLast((E) counter2.succ);
- counter1 = counter1.succ.succ;
- counter2 = counter2.succ.succ; // counters go brr
- }
- if(counter1!=null) {
- while(counter1!=null) {
- nova.insertLast((E) counter1);
- counter1 = counter1.succ;
- }
- }
- if(counter2!=null)
- while(counter2!=null) {
- nova.insertLast((E) counter2);
- counter2 = counter2.succ;
- }
- return nova;
- }
- }
- 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 spoeni = new SLL();
- spoeni = spoeni.specialJoin(lista1,lista2);
- SLLNode print = spoeni.getFirst();
- while(print!=null) {
- System.out.print(print + " -> ");
- print = print.succ;
- }
- }
- }
- /*
- Дадени се две еднострано поврзани листи чии што јазли содржат по еден природен број.
- Треба да се спојат двете листи во една резултантна на тој начин што наизменично прво
- ќе се додаваат првите два јазли од првата листа во резултантната, па првите два од
- втората листа, па следните два од првата, па следните два од втората итн. Јазлите
- што ќе останат треба да се додадат на крај во резултантната листа, прво оние што
- останале од првата листа, потоа оние што останале од втората листа.
- Во првиот ред од влезот се дадени броевите од кои се составени јазлите по редослед
- во првата листа, а во вториот ред броевите од кои се составени јазлите по редослед
- во втората листа. На излез треба да се испечатат јазлите по редослед во резултантната споена листа.
- Забелешка: Да се креира податочна структура еднострано поврзана листа и истата да се искористи во задачата.
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement