数据结构实现的说明

时间:2017-03-29 14:43:38

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

我遇到了这段计算机科学问题的代码,他们让我确定它实现的数据结构类型:

public class Demo
{
        boolean state;
    private Disc east, west;
    private int i;
    static int n
    public Disc() {i = ++n;)
    public Disc(Disc w, Disc e){
    }
        this();
        bind(w,this);
        bind(this,e);
    }
    public static void bind(Disc w, Disc e){
        w.east = e;
        e.east = w;
    }
    public boolean flip(){
        if(state && east != null)
            east.state  = !east.state;
            if(!state && west != null)
                west.state = !west.state();
            return state = !state;
    }
    public Disc get(int x){
        Disc d = this;
        while(d.east != null) d = d.east;
        do{ d = d.west}
        while(d != null && d.i != x);
        return d;
    }
    public String toString(){
        String s= "";
        Disc d = this;
        while(d.east != null) d = d.east;
        do{
            s+= d.i + (d.state? "H ": "T ");
            d = d.west;
        } while(d != null);
        return s;
    }
}

我认为这是双重链接列表,因为指向其他Discs的两个方向(如果有人可以将我链接到讨论双重链接列表的先前帖子或不同的解释,那将非常感激)。

让我感到困惑的是第二个问题,它提出了一个让我找到输出的主要方法。

public static void main(String args[]){
    Disc base = new Disc();
    Disc disc = new Disc();
    Disc.bind(base, disc);
    for (int i = 0; i < 5; i++)
        disc = new Disc(base, disc);
    out.println(base); //<#1>
    Disc dFour = base.get(4);
    Disc.bind(base.get(6), disc.get(3));
    out.println(base); //<#2>
    base.get(3).flip();
    out.println(disc); //<#3>
    out.println(dFour); //<#4>
}

前三行是有道理的,因为basedisc绑定在一起。 for循环将disc的地址重新分配给新对象,该对象是base与原始光盘disc之间的链接。不幸的是,混叠的想法使得这个实现对我来说更加难以理解。

0 个答案:

没有答案