添加和显示不正确,链表java

时间:2014-03-26 16:09:33

标签: java

下面你可以找到我的三个课程,一个公共课的一部分,主要方法是:

public static void main(String[] args) {
        List A = new List(); 
        Cont c = A.New("T1", "W1");
        A.InsertFirst("T1", "W3");
        A.InsertLast("T1", "W4");
        A.Display("T1");
        Cont d = A.New("T2", "X1");
        A.InsertFirst("T2", "X2");
        A.InsertFirst("T2", "X3");
        A.Display("T1");
        A.Display("T2");
        A.ListAList();
}

我无法理解为什么该计划的结果是

T1: W2 W1 
T2: X3 X2 X1 
T2: X3 X2 X1 
List: T2 T1

而不是

T1: W2 W1 
T1: W2 W1 
T2: X3 X2 X1 
List: T2 T1

第二次调用Display方法工作不正常(它看不到T1),但ListAList()显示正确的整个列表。任何帮助表示赞赏。

public static class Elem {

    public String name;
    public Elem next;
    public Elem prev;

    public Elem() {
    }

    public Elem(String a) {
        this.name = a;
        this.next = null;
        this.prev = null;
    }
}

public static class Cont {

    public String name;
    public Cont next;
    public Elem first;
    public Elem last;

    public Cont() {
        this.name = "";
        this.first = new Elem();
        this.last = this.first;
    }

    public Cont(String pname, String wname) {
        this.name = pname;
        this.next = null;
        this.first = new Elem();
        this.last = new Elem(wname);
        this.first.next = this.last;
        this.first.prev = this.last;
        this.last.prev = this.first;
        this.last.next = this.first;
    }
}

public static class List {

    public Cont first;

    public List() {
        this.first = null;
    }

    public Cont New(String T, String W) {
        if (this.first == null) {
            this.first = new Cont(T, W);
        } else {
            Cont tmp = new Cont(T, W);
            tmp.next = this.first;
            this.first = tmp;
        }
        return this.first;
    }

    public Cont find(String name, Cont tmp) {
        while (!tmp.name.equals(name)) {
            tmp = tmp.next;
        }
        return tmp;
    }

    public void Display(String T) {
        Cont tmp = this.first;
        find(T, tmp);
        String prints = tmp.name + ": ";
        Elem elem = tmp.first.next;
        while (elem != tmp.first) {
            prints += elem.name + " ";
            elem = elem.next;
        }
        System.out.println(prints);
    }

    public void InsertFirst(String T, String W) {
        Cont tmp = this.first;
        Elem elem = new Elem(W);
        find(T, tmp);
        elem.next = tmp.first.next;
        elem.prev = tmp.first;
        tmp.first.next.prev = elem;
        tmp.first.next = elem;
    }

    public void InsertLast(String T, String W) {
        Cont tmp = this.first;
        Elem elem = new Elem(W);
        find(T, tmp);
        elem.next = tmp.first;
        elem.prev = tmp.last;
        tmp.last.next = elem;
        tmp.last = elem;
    }

    public void ListAList() {
        Cont tmp = this.first;
        String prints = "List: ";
        while (tmp != null) {
            prints += tmp.name + " ";
            tmp = tmp.next;
        }
        System.out.println(prints);
    }
}

0 个答案:

没有答案