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 extends Comparable<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 void deleteSLL() {
- first = null;
- }
- public int length() {
- int len = 0;
- if (first != null) {
- SLLNode<E> temp = first;
- len = 1;
- while (temp.succ != null) {
- temp = temp.succ;
- len++;
- }
- }
- return len;
- }
- @Override
- public String toString() {
- if (first != null) {
- SLLNode<E> temp = first;
- StringBuffer sb = new StringBuffer();
- sb.append(temp.toString() + " ");
- while (temp.succ != null) {
- temp = temp.succ;
- sb.append(temp + " ");
- }
- return sb.toString();
- } else {
- return "The linked list is empty.";
- }
- }
- public void insertFirst(E elem) {
- SLLNode<E> insert = new SLLNode<E>(elem, first);
- /*
- * insert is a node pointing to the head, so the link has already been
- * established by the constructor
- */
- first = insert;
- }
- public void insertLast(E elem) {
- if (first != null) {
- SLLNode<E> temp = first;
- while (temp.succ != null) {
- temp = temp.succ;
- }
- SLLNode<E> insert = new SLLNode<E>(elem, null);
- temp.succ = insert;
- } else {
- insertFirst(elem);
- }
- }
- public void insertBefore(E elem, SLLNode<E> before) {
- if (first != null) {
- if (first == before) {
- this.insertFirst(elem);
- return;
- }
- /* Search till temp is the before node minus one! */
- SLLNode<E> temp = first;
- while (temp.succ != before) {
- temp = temp.succ;
- }
- if (temp.succ == before) {
- SLLNode<E> insert = new SLLNode<E>(elem, before);
- temp.succ = insert;
- } else {
- System.out.println("The element cannot be found in the list.");
- return;
- }
- } else {
- System.out.println("The linked list is empty.");
- }
- }
- public void insertAfter(E elem, SLLNode<E> after) {
- if (after != null) {
- SLLNode<E> insert = new SLLNode<E>(elem, after.succ);
- after.succ = insert;
- } else {
- System.out.println("The given node as a parameter is null.");
- }
- }
- public SLLNode<E> getFirst() {
- return first;
- }
- 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 SLL<E> joinLists(SLL<E> one, SLL<E> two) {
- SLL<E> result = new SLL<E>();
- SLLNode<E> firstOne = one.getFirst();
- SLLNode<E> firstTwo = two.getFirst();
- boolean flag = true;
- while (firstOne != null && firstTwo != null && firstOne.succ != null && firstTwo.succ != null) {
- result.insertLast(firstOne.element);
- firstOne = firstOne.succ;
- result.insertLast(firstOne.element);
- firstOne = firstOne.succ;
- result.insertLast(firstTwo.element);
- firstTwo = firstTwo.succ;
- result.insertLast(firstTwo.element);
- firstTwo = firstTwo.succ;
- }
- if (firstOne != null) {
- while (firstOne != null) {
- result.insertLast(firstOne.element);
- firstOne = firstOne.succ;
- }
- }
- if (firstTwo != null) {
- while (firstTwo != null) {
- result.insertLast(firstTwo.element);
- firstTwo = firstTwo.succ;
- }
- }
- return result;
- }
- 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;
- }
- // else throw Exception;
- }
- return null;
- // else throw Exception;
- }
- public SLL<E> deleteDuplicates(SLL<E> one) {
- SLL<E> result = new SLL<E>();
- SLLNode<E> firstOne = one.getFirst();
- while (firstOne != null
- && firstOne.succ != null) {
- if (firstOne.element.compareTo(firstOne.succ.element) == 0) {
- firstOne = firstOne.succ;
- } else {
- result.insertLast(firstOne.element);
- firstOne = firstOne.succ;
- }
- if (firstOne.succ == null) {
- result.insertLast(firstOne.element);
- }
- }
- return result;
- }
- public Iterator<E> iterator() {
- // Return an iterator that visits all elements of this list, in
- // left-to-right order.
- return new LRIterator<E>();
- }
- private class LRIterator<E extends Comparable<E>> implements Iterator<E> {
- private SLLNode<E> place, prev, curr;
- @SuppressWarnings("unchecked")
- 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.element;
- prev = curr;
- curr = place;
- place = place.succ;
- return nextElem;
- }
- public void remove() {
- // Not implemented
- }
- }
- }
- public class SpecialSLLJoin<E extends Comparable<E>> {
- 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> listOne = new SLL<Integer>();
- SLL<Integer> listTwo = new SLL<Integer>();
- for (int i = 0; i < N; i++) {
- listOne.insertLast(Integer.parseInt(pomniza[i]));
- }
- //System.out.println(listOne.toString());
- s = stdin.readLine();
- N = Integer.parseInt(s);
- s = stdin.readLine();
- pomniza = s.split(" ");
- for (int i = 0; i < N; i++) {
- listTwo.insertLast(Integer.parseInt(pomniza[i]));
- }
- //System.out.println(listTwo.toString());
- SLL<Integer> listJoined = listOne.joinLists(listOne, listTwo);
- System.out.println(listJoined.toString());
- /*
- * SLL<Integer> listJoined = listOne.joinLists(listTwo); Iterator<Integer> it =
- * listJoined.iterator(); while (it.hasNext()) { System.out.print(it.next());
- * if(it.hasNext()) System.out.print(" "); } System.out.println();
- */
- }
- }
Add Comment
Please, Sign In to add comment