循环链表无限循环

时间:2018-02-21 20:09:30

标签: java

我正在使用我的java书来查看数据结构,我需要重新创建一个循环链表。我有这个链表无限循环的问题,无法找出原因。我能够将值插入列表中,但是打印和删除值似乎无限循环最初插入的值。如何更改List类以避免无限循环?

输出:

30, 5, 15, 20, 10, 30, 5, 15, 20, 10, 30, 5, 15, 20, 10, ...

主:

public static void main(String[] args) {

    CircularList theList = new CircularList();
    theList.insert(10);
    theList.insert(20);
    theList.insert(15);
    theList.insert(5);
    theList.insert(30);

    theList.displayList();
    System.out.println(theList.delete());
    theList.delete(15);
    theList.displayList();       

    while (!theList.isEmpty()) {
        Link aLink = theList.delete();
        System.out.println("Deleted: " + aLink);           
    }
    if (!theList.isEmpty()) 
        System.out.println("Program error");
    else
        System.out.println("Program success");

}

CircularList.class

class CircularList {

private Link current;
private Link prev;

public CircularList() {
    // implement: set both current and prev to null
    current = prev = null;
}

public boolean isEmpty() {
    // implement
    return (current == null); // true if current is empty
}

public void insert(int id) {
    // implement: insert the new node behind the current node
    Link newLink = new Link(id);

    if (isEmpty()) {
        prev = current = newLink;
    } else {
        prev.next = newLink;
        newLink.next = current;
        current = newLink;
    }
}

public Link delete() {
    // implement: delete the node referred by current
    if (!isEmpty()) {
        Link temp = current;
        current = current.next;
        return temp;
    } else 
        return null;
}

public Link delete(int id) {
    // implement: delete the node with value id
    // if no node with the id exists, return null
    if (isEmpty()) {
        return null;
    }

    // Link current; // start probe at beginning
    Link prev = current; // start previous at current 

    while (current != null && current.equals(current)) {
        prev = current; //save previous link
        current = current.next; // move to next Link
    }

    if (current == current.next)
        current = current.next;
    else if (current != null)
        prev.next = current.next;
    return current;

}

public void displayList() {
    // implement: print all the list element values once, each value seperated by comma
    while (current != null) {
        System.out.printf(current + ", ");
        current = current.next;
    }
}
}

Link.class

class Link {

private int id;
Link next;

public Link(int id) {
    // implement
    this.id = id;
    next = null;
}

public String toString() {
    return String.valueOf(id);
}
}

2 个答案:

答案 0 :(得分:0)

它是循环列表,current永远不会为delete(int id)displayList(),除非列表为空。

如果您回到起点Link,您必须记住您开始查看/打印和中断的位置。

答案 1 :(得分:0)

因为一个圆圈永远存在,你应该设置一个开始/停止索引:

public void displayList() {
    // implement: print all the list element values once, each value seperated by comma
     Link start = current;
     if (start == null)
        return;
     do {
        System.out.printf(current + ", ");
        current = current.next;
     } while (current != start)
}