Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dlll;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Iterator;
- import java.util.LinkedList;
- import java.util.NoSuchElementException;
- class DLLNode<E> {
- protected E element;
- protected DLLNode<E> pred, succ;
- public DLLNode(E elem, DLLNode<E> pred, DLLNode<E> succ) {
- this.element = elem;
- this.pred = pred;
- this.succ = succ;
- }
- @Override
- public String toString() {
- return "<-" + element.toString() + "->";
- }
- }
- class DLL<E> {
- private DLLNode<E> first, last;
- public DLL() {
- // Construct an empty SLL
- this.first = null;
- this.last = null;
- }
- public void deleteList() {
- first = null;
- last = null;
- }
- public int length() {
- int ret;
- if (first != null) {
- DLLNode<E> tmp = first;
- ret = 1;
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret++;
- }
- return ret;
- } else
- return 0;
- }
- public void insertFirst(E o) {
- DLLNode<E> ins = new DLLNode<E>(o, null, first);
- if (first == null)
- last = ins;
- else
- first.pred = ins;
- first = ins;
- }
- public void insertLast(E o) {
- if (first == null)
- insertFirst(o);
- else {
- DLLNode<E> ins = new DLLNode<E>(o, last, null);
- last.succ = ins;
- last = ins;
- }
- }
- public void insertAfter(E o, DLLNode<E> after) {
- if (after == last) {
- insertLast(o);
- return;
- }
- DLLNode<E> ins = new DLLNode<E>(o, after, after.succ);
- after.succ.pred = ins;
- after.succ = ins;
- }
- public void insertBefore(E o, DLLNode<E> before) {
- if (before == first) {
- insertFirst(o);
- return;
- }
- DLLNode<E> ins = new DLLNode<E>(o, before.pred, before);
- before.pred.succ = ins;
- before.pred = ins;
- }
- public E deleteFirst() {
- if (first != null) {
- DLLNode<E> tmp = first;
- first = first.succ;
- if (first != null) first.pred = null;
- if (first == null)
- last = null;
- return tmp.element;
- } else
- return null;
- }
- public E deleteLast() {
- if (first != null) {
- if (first.succ == null)
- return deleteFirst();
- else {
- DLLNode<E> tmp = last;
- last = last.pred;
- last.succ = null;
- return tmp.element;
- }
- }
- // else throw Exception
- return null;
- }
- @Override
- public String toString() {
- String ret = new String();
- if (first != null) {
- DLLNode<E> tmp = first;
- ret += tmp + "<->";
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret += tmp + "<->";
- }
- } else
- ret = "Prazna lista!!!";
- return ret;
- }
- public DLLNode<E> getFirst() {
- return first;
- }
- public DLLNode<E> getLast() {
- return last;
- }
- public void delete(DLLNode<E> node) {
- // System.out.println(node.element);
- // if( node.element == t deleteFirst();
- if(node.pred == null){ first = first.succ; first.pred= null;}
- else if(node== this.last) deleteLast();
- else { DLLNode<E> tmp = node;
- node.pred.succ = tmp.succ;
- tmp.succ.pred=node.pred;
- //deleteFirst();
- }}
- public DLLNode<E> get(int index){
- if (index<=this.length()-1){
- DLLNode<E> tmp = first;
- int k=0;
- while(tmp!=null){
- if (k==index) break;
- k++;
- tmp = tmp.succ;
- }
- return tmp;
- }
- else return null;
- }
- public void WHY(DLL<E> lista) {
- DLLNode<E> tmp1= this.first; int count=0;
- DLLNode<E> tmp2 = lista.first;
- int s= lista.length();
- while(tmp1 != null) {
- while(tmp2 != null && tmp1 !=null && tmp1.element.equals(tmp2.element))
- {
- count++; tmp1= tmp1.succ; tmp2= tmp2.succ;
- }
- if ( count== s) {
- while(count !=0)
- { System.out.println("Dali sum "+ tmp1.pred);
- if(tmp1.pred == null) System.out.println("eror"); //deleteFirst();
- this.delete(tmp1.pred);
- // System.out.println(tmp1.pred);
- // tmp1=tmp1.succ;
- count-=1;
- }}
- tmp2=lista.first;
- tmp1=tmp1.succ;
- } if(this.length()>0)
- { DLLNode<E>tmp3= this.first;
- for(int i=0;i<this.length();i++)
- {System.out.print( tmp3.element.toString() + " ");
- tmp3=tmp3.succ;
- } }
- else System.out.println("PRAZNA LISTA!");
- }}
- public class ZastoNE {
- 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);
- DLL<Integer> lista1 = new DLL<Integer>();
- 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);
- DLL<Integer> lista2 = new DLL<Integer>();
- s = stdin.readLine();
- pomniza = s.split(" ");
- for (int i = 0; i < N; i++) {
- lista2.insertLast(Integer.parseInt(pomniza[i]));
- }
- //lista1.WHY(lista2);
- System.out.println("na ivan :");
- lista1.deleteSublist(lista2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement