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;
- import java.util.Iterator;
- import java.util.NoSuchElementException;
- class SLLNode<E> {
- protected E data;
- protected SLLNode<E> next;
- public SLLNode(E data, SLLNode<E> next) {
- this.data= data;
- this.next= next;
- }
- public E getData(){
- return data;
- }
- public SLLNode<E> getNext(){
- return next;
- }
- public void setNext(SLLNode<E> newNext){
- next = newNext;
- }
- }
- class SLL<E>{
- private SLLNode<E> first;
- public SLL(){
- // kreiranjepraznalista
- this.first= null;
- }
- public Iterator<E> iterator() {
- // vrakaiteratorkojgiposetuvasite elementinalistataodlevonadesno
- return new LRIterator<E>();
- }
- // //////////Inner class ////////////
- private class LRIterator<E> implements Iterator<E> {
- private SLLNode<E> place, prev, curr;
- private LRIterator() {
- place = (SLLNode<E>) first;
- curr= prev= null;
- }
- public boolean hasNext() {
- return (place != null);
- }
- public E next() {
- if (place == null)
- throw new NoSuchElementException();
- E nextElem= place.getData();
- prev= curr;
- curr= place;
- place = place.getNext();
- return nextElem;
- }
- //public void remove() {//Not implemented}
- }
- public void insertLast(E o){
- SLLNode<E> newNode = new SLLNode<E>(o, null);
- if(first == null)
- first = newNode;
- else{
- SLLNode<E> last = first;
- while(last.getNext() != null){
- last = last.getNext();
- }
- last.setNext(newNode);
- }
- }
- public SLLNode<E> getFirst(){ return first; }
- public SLL<E> joinLists(SLL<E> vtora){
- SLL<E> spoena = new SLL<E>();
- SLLNode<E> ptr1 = this.getFirst();
- SLLNode<E> ptr2 = vtora.getFirst();
- if((Integer) ptr1.getData() < (Integer) ptr2.getData()){
- spoena.insertLast(ptr1.getData());
- ptr1 = ptr1.getNext();
- }
- else if(ptr1.getData() == ptr2.getData()){
- spoena.insertLast(ptr2.getData());
- ptr1 = ptr1.getNext();
- ptr2 = ptr2.getNext();
- }
- else {
- spoena.insertLast(ptr2.getData());
- ptr2 = ptr2.getNext();
- }
- SLLNode<E> ptrS = spoena.getFirst();
- while (ptr1 != null&&ptr2 != null){
- if(ptr1.getData() == ptr2.getData()){
- if(ptr1.getData() != ptrS.getData()){
- ptrS.setNext(new SLLNode<E>(ptr1.getData(), null));
- ptrS = ptrS.getNext();
- }
- ptr1 = ptr1.getNext();
- ptr2 = ptr2.getNext();
- }
- else if((Integer) ptr1.getData() < (Integer) ptr2.getData()){
- if(ptr1.getData() != ptrS.getData()){
- ptrS.setNext(new SLLNode<E>(ptr1.getData(), null));
- ptrS = ptrS.getNext();
- }
- ptr1 = ptr1.getNext();
- }
- else {
- if(ptr2.getData() != ptrS.getData()){
- ptrS.setNext(new SLLNode<E>(ptr2.getData(), null));
- ptrS = ptrS.getNext();
- }
- ptr2 = ptr2.getNext();
- }
- }
- while(ptr1 != null){
- if(ptr1.getData() != ptrS.getData()){
- ptrS.setNext(new SLLNode<E>(ptr1.getData(), null));
- ptrS = ptrS.getNext();
- }
- ptr1 = ptr1.getNext();
- }
- while(ptr2 != null){
- if(ptr2.getData() != ptrS.getData()){
- ptrS.setNext(new SLLNode<E>(ptr2.getData(), null));
- ptrS = ptrS.getNext();
- }
- ptr2 = ptr2.getNext();
- }
- return spoena;
- }
- //public void insertBefore(E o, SLLNode<E> before)
- //public E deleteFirst()
- //public E delete(SLLNode<E> node)
- //public SLLNode<E> getFirst()
- }
- public class SLLJoinLists {
- 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();
- String[] pomniza = s.split(" ");
- SLL<Integer> lista1 = new SLL<Integer>();
- 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(" ");
- SLL<Integer> lista2 = new SLL<Integer>();
- for (int i = 0; i < N; i++) {
- lista2.insertLast(Integer.parseInt(pomniza[i]));
- }
- SLL<Integer> spoeni = lista1.joinLists(lista2);
- // spoeni = lista1.joinLists(lista2);
- Iterator<Integer> it = spoeni.iterator();
- while (it.hasNext()) {
- System.out.print(it.next());
- if(it.hasNext())
- System.out.print(" ");
- }
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement