从分类数组包中实现双向链表(数据结构)

时间:2017-12-02 18:41:23

标签: java arrays data-structures doubly-linked-list

这是我第一次在StackOverFlow上发帖,因为我现在真的很绝望。我无法在任何地方找到问题的答案,长话短说,我的数据结构课程有一些项目。该项目有两个部分。第一部分是针对某些问题实施Sorted Array Bag / Sorted Collection。我们正在使用 java

第二部分是我确实遇到很多问题的地方。所以主要的想法是从排序数组包/排序集合中实现一个双向链接列表,并且我将在我的主要部分中切换带有双向链表的排序数组包,并且所有内容都应该按照以前的方式工作

当你在main中声明SortedArrayBag时,SortedArrayBag的主要内容就是我理解使用Comparator,它看起来像这样:

SortedBag<Grupe> al = new SortedArrayBag<>(new ComparatorVot());
al.add(new Grupe("gr1", 5));
al.add(new Grupe("gr2", 7));

已排序的集合/排序数组包由我的老师实现,因为Java中没有这样的数据结构,这是她的实现:

public class SortedArrayBag<T> implements SortedBag<T> {

    private ArrayList<T> elemente;
    private Comparator<T> relatie;

    public SortedArrayBag(Comparator<T> rel) {
        this.elemente = new ArrayList<>();
        this.relatie = rel;
    }

    public void add(T elem) {
        int index = 0;
        boolean added = false;
        while (index < this.elemente.size() && added == false) {
            T currentElem = this.elemente.get(index);
            if (relatie.compare(currentElem, elem) < 0) {
                index++;
            } else {
                this.elemente.add(index, elem);
                added = true;
            }
        }
        if (!added) {
            this.elemente.add(elem);
        }
    }

    public void remove(T elem) {
        boolean removed = this.elemente.remove(elem);
    }

    public int size() {
        return this.elemente.size();
    }

    public boolean search(T elem) {
        return this.elemente.contains(elem);
    }

    public Iterator<T> iterator() {
        return this.elemente.iterator();
    }

}

SortedBag界面看起来像这样

public interface SortedBag<T> {
    public void add(T elem);
    public void remove(T elem);
    public int size();
    public boolean search(T elem);
    public Iterator<T> iterator();
}

如果有帮助,比较器如下所示:

public class ComparatorVot implements Comparator<Grupe> {


    public int compare(Grupe o1, Grupe o2) {
        Grupe gr1 = (Grupe) o1;
        Grupe gr2 = (Grupe) o2;


        if (gr1.getNrPersoane() / 2 + 1 == gr2.getNrPersoane() / 2 + 1) {
            return 0;
        } else if (gr1.getNrPersoane() / 2 + 1 > gr2.getNrPersoane() / 2 + 1) {
            return 1;
        } else {
            return -1;
        }
    }
}

所以,我尝试使用SortedArrayBag最好地实现doublyLinkedList,这就是我所做的,如果它有助于使我的代码更清晰,prim = first,ultim = last,urmator = next,anterior = previous

import java.util.Iterator;

public class LDI {
    private Nod prim;
    private Nod ultim;

//private int lungime;
public LDI() {
    this.prim = null;
    this.ultim = null;
    //this.lungime = 0;
}

public class Nod {
    private int elem;
    private int frecventa;
    private Nod urmator;
    private Nod anterior;

    public Nod(int e, int f) {
        this.elem = e;
        this.frecventa = f;
        this.urmator = null;
        this.anterior = null;
    }
}

public void add(int elem, int frecventa) {
    Nod nodNou = new Nod(elem, frecventa);
    nodNou.elem = elem;
    nodNou.frecventa = frecventa;
    if (prim == null) {
        this.prim = nodNou;
        this.ultim = nodNou;
    } else if (frecventa <= prim.frecventa) {
        nodNou.urmator = prim;
        this.prim.anterior = nodNou;
        this.prim = nodNou;
    } else if (frecventa >= prim.frecventa) {
        nodNou.anterior = prim;
        for (; nodNou.anterior.urmator != null; nodNou.anterior = nodNou.anterior.urmator) {
            if (nodNou.anterior.urmator.frecventa > frecventa)
                break;
        }
        nodNou.urmator = nodNou.anterior.urmator;
        if (nodNou.anterior.urmator != null) {
            nodNou.anterior.urmator.anterior = nodNou;
        }
        nodNou.anterior.urmator = nodNou;
        nodNou.anterior = nodNou.anterior;
    }
}


public void remove() {
    if (this.prim != null) {
        if (this.prim == this.ultim) {
            this.prim = null;
            this.ultim = null;
        } else
            this.prim = this.prim.urmator;
        this.prim.anterior = null;
    }
}

public int size() {
    int count = 0;
    for (Nod nodNou = prim; nodNou != null; nodNou = nodNou.urmator)
        count++;
    return count;
}

public class MyIterator {
    private Nod curent;

    public MyIterator() {
        this.curent = prim;
    }

    public void urmator() {
        this.curent = this.curent.urmator;
    }

    public int getElem() {
        return this.curent.elem;
    }

    public boolean valid() {
        if (this.curent != null) {
            return true;
        } else {
            return false;
        }
    }
}

public Iterator iterator() {
    return new MyIterator();
}
}

问题是,它不起作用,我不知道如何使我的数据结构能够接收我使用的比较器,并且Iterator也不起作用。如果你有任何想法如何使这项工作,请帮助我。

0 个答案:

没有答案