Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package probno;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- /**
- *
- * @author Sandra
- */
- public class SLLJoinLists1<E extends Comparable<E>> {
- public static class SLLNode<E> {
- protected E element;
- protected SLLNode<E> succ;
- public SLLNode(E value, SLLNode<E> succ) {
- this.element = value;
- this.succ = succ;
- }
- public E getElement() {
- return element;
- }
- }
- public static class SLL<E> {
- private SLLNode<E> first;
- public SLL() {
- this.first = null;
- }
- public SLLNode<E> getFirst() {
- return first;
- }
- public void insertFirst(E o) {
- SLLNode<E> ins = new SLLNode<E>(o, first);
- first = ins;
- }
- public void insertLast(E o) {
- if (first != null) {
- SLLNode<E> pom1 = first;
- while (pom1.succ != null) {
- pom1 = pom1.succ;
- }
- SLLNode<E> ins = new SLLNode<E>(o, null);
- pom1.succ = ins;
- } else {
- insertFirst(o);
- }
- }
- public E delete(SLLNode<E> node) {
- if (first != null) {
- SLLNode<E> tmp = first;
- while (tmp.succ != node
- && tmp.succ.succ != null) {
- tmp = tmp.succ;
- }
- if (tmp.succ == node) {
- tmp.succ = tmp.succ.succ;
- return node.element;
- }
- }
- return null;
- }
- }
- public static SLL<Integer> joinLists(SLL<Integer> lista1, SLL<Integer> lista2) {
- SLL<Integer> lista3 = new SLL<Integer>();
- SLLNode<Integer> jazol1 = lista1.getFirst(), jazol2 = lista2.getFirst();
- while (jazol1 != null && jazol2 != null) {
- if (jazol1.element.compareTo(jazol2.element) < 0) {
- lista3.insertLast(jazol1.element);
- jazol1 = jazol1.succ;
- } else {
- lista3.insertLast(jazol2.element);
- jazol2 = jazol2.succ;
- }
- }
- if (jazol1 != null) {
- while (jazol1 != null) {
- lista3.insertLast(jazol1.element);
- jazol1 = jazol1.succ;
- }
- }
- if (jazol2 != null) {
- while (jazol2 != null) {
- lista3.insertLast(jazol2.element);
- jazol2 = jazol2.succ;
- }
- }
- return lista3;
- }
- public static SLL<Integer> brisiDup(SLL<Integer> lista){
- SLLNode<Integer> pom1 = (SLLNode<Integer>) lista.first;
- SLLNode<Integer> next = (SLLNode<Integer>) lista.first.succ;
- while (pom1 != null) {
- next = pom1.succ;
- while (next != null) {
- if (pom1.getElement() == next.getElement()) {
- lista.delete(next);
- next = next.succ;
- } else {
- next = next.succ;
- }
- }
- pom1 = pom1.succ;
- }
- return lista;
- }
- public static void pecati(SLL<Integer> lista) {
- SLLNode<Integer> temp = lista.getFirst();
- while (temp != null) {
- System.out.print(temp.element);
- System.out.print(" ");
- temp = temp.succ;
- }
- }
- 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]));
- }
- String s1 = stdin.readLine();
- int N1 = Integer.parseInt(s1);
- s1 = stdin.readLine();
- String[] pomniza1 = s1.split(" ");
- SLL<Integer> lista2 = new SLL<Integer>();
- for (int i = 0; i < N1; i++) {
- lista2.insertLast(Integer.parseInt(pomniza1[i]));
- }
- SLL<Integer> lista3 = new SLL<Integer>();
- lista3 = joinLists(lista1, lista2);
- brisiDup(lista3);
- pecati(lista3);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement