Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Zadachiche;
- 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> {
- public E element;
- public 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> {
- 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> getLast(){
- SLLNode<E> temp=first;
- while(temp.succ!=null) {
- temp=temp.succ;
- }
- return temp;
- }
- 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 extends Comparable> 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 remove() {
- //Not implemented
- }
- }
- public SLL<E> joinLists(SLL<E> l2) {
- SLLNode<E> d1 = this.getFirst();//this e referenca na lista1
- SLLNode<E> d2 = l2.getFirst();
- SLL<E> result = new SLL();
- while(d1!=null && d2!=null) {
- if(d1.element.compareTo(d2.element)<0) {
- result.insertLast(d1.element);
- d1 = d1.succ;
- } else if(d1.element.compareTo(d2.element)>0) {
- result.insertLast(d2.element);
- d2 = d2.succ;
- } else {
- result.insertLast(d1.element);
- d1 = d1.succ;
- d2 = d2.succ;
- }
- }
- while(d1!=null) {
- result.insertLast(d1.element);
- d1 = d1.succ;
- }
- while(d2!=null) {
- result.insertLast(d2.element);
- d2 = d2.succ;
- }
- return result;
- }
- }
- public class hehe {
- public static void presmetaj(SLL<Integer> lista,Integer dolzina,Integer M,Integer N) {
- SLLNode<Integer> dvizi1=lista.find(M);
- SLLNode<Integer> dvizi2=lista.find(N);
- SLLNode<Integer> temp=dvizi1;
- while(dvizi1!=dvizi2) {
- temp=dvizi1;
- lista.insertAfter(temp.element, dvizi2);
- lista.delete(temp);
- dvizi1=dvizi1.succ;
- }
- System.out.println(lista);
- }
- public static void main(String[] args) throws IOException {
- SLL<Integer> lista=new SLL<>();
- BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
- int dolzina=Integer.parseInt(br.readLine());
- String line=br.readLine();
- String[] pomniza=line.split(" ");
- for(int i=0;i<pomniza.length;i++) {
- lista.insertLast(Integer.parseInt(pomniza[i]));
- }
- int M=Integer.parseInt(br.readLine());
- int N=Integer.parseInt(br.readLine());
- presmetaj(lista,dolzina,M,N);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment