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 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 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> ins = new SLLNode<E>(ob,first);
- first = ins;
- }
- }
- public void deleteDuplicate(SLL<E> list) {
- SLLNode<E> tmp=list.getFirst();
- while( tmp!=null && tmp.succ!=null) {
- if(tmp.element==tmp.succ.element) {
- tmp.succ=tmp.succ.succ;
- } else {
- tmp=tmp.succ;
- }
- }
- }
- public SLL<E> joinLists(SLL<E> list2) {
- SLL<E> rezultat = new SLL<E>();
- SLLNode<E> jazol1=this.getFirst();
- SLLNode<E> jazol2 = list2.getFirst();
- while(jazol1 !=null&&jazol2 != null) {
- if(jazol1.element.compareTo(jazol2.element)<0) {
- rezultat.insertLast(jazol1.element);
- jazol1=jazol1.succ;
- } else {
- rezultat.insertLast(jazol2.element);
- jazol2=jazol2.succ;
- }
- }
- 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> tmp = first;
- print += tmp + " ";
- while (tmp.succ != null) {
- if(tmp.succ == null){
- tmp = tmp.succ;
- print += tmp;
- }else{
- tmp = tmp.succ;
- print += tmp + " ";
- }
- }
- } else
- print = "Prazna lista!!!";
- return print;
- }
- }
- public class SLLJoinLists {
- 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]));
- }
- SLL<Integer> spoeni = lista1.joinLists(lista2);
- spoeni.deleteDuplicate(spoeni);
- System.out.println(spoeni);
- // 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