Question

I would like to create a third list by connecting 2 others. I tried to do it, but as I expected my idea was wrong and everything doesn't work fine. Below you can see List file and test program. The other (empty) constructors of List are meant to connect 2 lists by adding elements of one List before specific index of the second list, and adding elements of one List before specific element of the second one using equals() method. It will be also hard for me for sure, but I ask only for simply connecting elements to obtain something like that: (L11, L12, L13)+(L21, L22, L23) = L11,L12,L13,L21,L22,L23

public class Lista implements List {
private Element head = new Element(null); //wartownik
private int size; 
public Lista(){
    clear();
}

public Lista(Lista lista1, Lista lista2) {
        head.previous = lista2.head.previous;
        head.next = lista1.head.next;
}

public Lista(Lista lista1, Lista lista2, int index) {

}

public Lista(Lista lista1, Lista2. Object value) {

}

public void clear(){
    head.setPrevious(head);
    head.setNext(head);
    size=0;
}

public void insert(int index, Object value) throws IndexOutOfBoundsException {
   if (index<0 || index>size) throw new IndexOutOfBoundsException();
   Element element = new Element(value);
   element.wstawPrzed(getElement(index));
   ++size;
}

public Element getElement(int index) {
   Element szukany = head.getNext();
   for (int i=index; i>0; --i) 
       szukany = szukany.getNext();   
   return szukany;
}


 public Object get(int index) throws IndexOutOfBoundsException{
    if(index<0 || index>size) throw new IndexOutOfBoundsException();
    Element particular = head.getNext();
    for(int i=0; i <= index; i++)
        particular = particular.getNext();
    return particular.getValue();
}  

public boolean delete(Object o){
    if(head.getNext() == null) return false;
    if(head.getNext().getValue().equals(o)){
        head.setNext(head.getNext().getNext());
        size--;
        return true;
    }

    Element delete = head.getNext();
    while(delete != null && delete.getNext() != null){
        if(delete.getNext().getValue().equals(o)){
            delete.setNext(delete.getNext().getNext());
                            size--;
            return true;
        }
        delete = delete.getNext();
    }
    return false;
}

public int size(){
    return size;
}

public boolean isEmpty(){
    return size == 0;
}

public void infoOStanie() {
    if (isEmpty()) {
      System.out.println("Lista pusta.");
  }
  else
  {
      System.out.println("Lista zawiera " + size() + " elementow.");
  }

}

public IteratorListowy iterator() {
    return new IteratorListowy();
}

public void wyswietlListe() {
    System.out.println();
    IteratorListowy iterator = iterator();
    for (iterator.first(); !iterator.isDone(); iterator.next())
    {
        System.out.println(iterator.current());
    }
    System.out.println();
}

private static final class Element{
    private Object value; 
    private Element next; //Referencja do kolejnego obiektu
    private Element previous; //Referencja do elementu poprzedniego

    public Element(Object value){ 
        setValue(value); 
      }

    public void setValue(Object value) {
        this.value = value;
    }

    public Object getValue() {
        return value;
    }

    //ustawia referencję this.next na obiekt next podany w atgumencie
    public void setNext(Element next) {
        if (next != null)
        this.next = next;
    }

    public Element getNext(){
        return next;
    }

    public void setPrevious(Element previous) {
        if (previous != null)
        this.previous = previous;
    }

    public Element getPrevious() {
        return previous;
    }

    public void wstawPrzed(Element next) {
        Element previous = next.getPrevious();
        setNext(next);
        setPrevious(previous);
        next.setPrevious(this);
        previous.setNext(this);
    } 

    public void delete() {
        previous.setNext(next);
        next.setPrevious(previous);
    }

}

private class IteratorListowy implements Iterator{
private Element current;

public IteratorListowy() {
   current = head;
} 

public void next() {
   current = current.next;
}   

public void previous() {
   current = current.previous;
}

 public boolean isDone() {
   return current == head;
} 
public Object current() {
    return current.value;
}

public void first() {
   current = head.getNext();
}

public void last() {
   current = head.getPrevious();
}
}
}

test

public class Program {

public static void main(String[] args) {
  Lista lista1 = new Lista();
  Lista lista2 = new Lista();
  Lista lista3 = new Lista(lista1, lista2);
  Student s1 = new Student("Kowalski", 3523);
  Student s2 = new Student("Polański", 45612);
  Student s3 = new Student("Karzeł", 8795);
  Student s4 = new Student("Pałka", 3218);
  Student s5 = new Student("Konowałek", 8432);
  Student s6 = new Student("Kłopotek", 6743);
  Student s7 = new Student("Całka", 14124);
  Student s8 = new Student("Pabin", 1258);
  Student s9 = new Student("Dryjas", 7896);
  Student s10 = new Student("Zając", 5642);
  lista1.insert(0, s1);
  lista1.insert(0, s2);
  lista1.insert(0, s3);
  lista1.insert(0, s4);
  lista1.insert(0, s5);
  lista1.wyswietlListe();
  lista1.infoOStanie();

  lista2.insert(0, s6);
  lista2.insert(0, s7);
  lista2.insert(0, s8);
  lista2.insert(0, s9);
  lista2.insert(0, s10);
  lista2.wyswietlListe();
  lista2.infoOStanie();

  lista3.wyswietlListe();

}
}
Was it helpful?

Solution

As I understood you are trying to concatenate two doubly linked lists list1 and list2, so you would need this simple logic :

// tail of list1 should point to head of list2
lista1.tail.next = lista2.head;

// as we are doubly linked, head of list2 should point back to tail of list1
lista2.head.previous = lista1.tail

this is it, the head of the list1 will now be the head of result list.

Now, doing it in your constructor :

public Lista(Lista lista1, Lista lista2) {
    // get hold of lisa1.tail
    Element lista1Tail = lista1.getElement(lista1.size() - 1);
    listaTail.next = lista2.head;

    //now pointer back to tail of lista1
    lista2.head.previous = listaTail;
}

This will work, given you fix your getElement() method (which now return not correct index) and add null pointer checks for boundary cases.

As a simpler alternative, I recommend keeping tail member of your list, it is cleaner and more performant.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top