Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package sllizbrishipodlista;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Iterator;
- import java.util.NoSuchElementException;
- import java.util.Objects;
- class SLL<E> {
- private 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> 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> 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 void mirror(){
- if (first != null) {
- //m=nextsucc, p=tmp,q=next
- SLLNode<E> tmp = first;
- SLLNode<E> newsucc = null;
- SLLNode<E> next;
- while(tmp != null){
- next = tmp.succ;
- tmp.succ = newsucc;
- newsucc = tmp;
- tmp = next;
- }
- first = newsucc;
- }
- }
- 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();
- }
- }
- }
- 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();
- }
- }
- public class SLLIzbrishiPodlista {
- public static SLL<Integer> brishiPodlista(SLL<Integer> lista, SLL<Integer> podlista)
- {
- SLL<Integer> novalista = new SLL<>();
- SLLNode<Integer> curr1 = lista.getFirst();
- SLLNode<Integer> curr2 = podlista.getFirst();
- SLLNode<Integer> pocetok, kraj, novPocetok=null;
- boolean najdenNovP = false;
- while(curr1!=null)
- {
- if(curr1.element.equals(curr2.element))
- {
- pocetok = curr1; //najden e element kako prviot na podlistata
- curr1=curr1.succ;
- curr2=curr2.succ;
- while(curr1!=null && curr2!=null && curr1.element.equals(curr2.element)){
- if(curr1.element.equals(pocetok.element) && !najdenNovP){ //ako vo podlistata postoi uste eden element ist kako prviot
- novPocetok = curr1; // i se pronajde takov i vo listata
- najdenNovP=true;
- }
- curr1=curr1.succ;
- curr2=curr2.succ;
- }
- if(curr2!=null) //ne sme ja izminale podlistata dokraj, znaci od pocetok do mozniot nov pocetok se elementi koi
- { // definitivno ne se podlista
- if(najdenNovP)
- {
- while(pocetok!=novPocetok)
- {
- novalista.insertLast(pocetok.element);
- pocetok=pocetok.succ;
- } // posle ova pocetok=novpocetok
- curr1= novPocetok;
- }else{
- while(pocetok!=curr1)
- {
- novalista.insertLast(pocetok.element);
- pocetok=pocetok.succ;
- }
- }
- novPocetok=null;
- najdenNovP=false;
- pocetok=null;
- curr2 = podlista.getFirst();
- // continue; // za da ne se pomesti na kraj od while-ot ( curr1=curr1.succ)
- }
- else //najdena e nova podlista ili curr1 stignal do null
- {
- if(curr1!=null) //najdena podlista, ne dodavame elementi (ja briseme t.e..), reset na promenlivite
- {
- pocetok = null;
- novPocetok=null;
- najdenNovP=false;
- }
- else //stignato e do kraj na listata, ako pocetok ne e null znaci na krajot se naogja samo del od podlistata
- {
- while(pocetok!=null)
- {
- if(curr2!=null)
- novalista.insertLast(pocetok.element);
- pocetok=pocetok.succ;
- }
- }
- curr2=podlista.getFirst();
- //continue;
- }
- } else {
- novalista.insertLast(curr1.element);
- curr1=curr1.succ;
- }
- }
- return novalista;
- }
- public static void main(String[] args) throws IOException {
- // TODO code application logic here
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String s = br.readLine();
- String[] pom= s.split(" ");
- SLL<Integer> lista = new SLL<>();
- for(int i=0;i<pom.length;i++)
- lista.insertLast(Integer.parseInt(pom[i]));
- s = br.readLine();
- pom= s.split(" ");
- SLL<Integer> podlista = new SLL<>();
- for(int i=0;i<pom.length;i++)
- podlista.insertLast(Integer.parseInt(pom[i]));
- lista = brishiPodlista(lista,podlista);
- System.out.println(lista.toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement