11

我想创建一个循环/循环链表,其中列表的尾部将指向列表的头部。那么我可以在创建列表后使用java.util.LinkedList和修改尾节点以使其循环/循环吗?如果是这样,你能告诉我一些关于如何发生的代码吗?

如果我不能使用java.util.LinkedList,我应该如何创建自己的循环/循环链表实现?你能告诉我这个实现的外观吗?

如果您需要更多详细信息,请告诉我,我会解决任何困惑。

4

4 回答 4

19
class ListNode {
    public ListNode next;
    public Object data;

    public ListNode(Object data, ListNode next) {
        this.next = next;
        this.data = data;
    }
}

class CircularLinkedList {
    private ListNode head = null;
    private int numberOfElements = 0;
    private ListNode actualElement = null;
    private int index = 0;

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

    public int getNumberOfElements() {
        return numberOfElements;
    }

    public void insertFirst(Object data) {
        if (!(isEmpty())) {
            index++;
        }
        ListNode listNode = new ListNode(data, head);
        head = listNode;
        numberOfElements++;
    }

    public void insertAfterActual(Object data) {
        ListNode listNode = new ListNode(data, actualElement.next);
        actualElement.next = listNode;
        numberOfElements++;
    }

    public boolean deleteFirst() {
        if (isEmpty())
            return false;
        if (index > 0)
            index--;
        head = head.next;
        numberOfElements--;
        return true;
    }

    public boolean deleteActualElement() {
        if (index > 0) {
            numberOfElements--;
            index--;
            ListNode listNode = head;
            while (listNode.next.equals(actualElement) == false)
                listNode = listNode.next;
            listNode.next = actualElement.next;
            actualElement = listNode;
            return true;
        }
        else {
            actualElement = head.next;
            index = 0;
            return deleteFirst();
        }
    }

    public boolean goToNextElement() {
        if (isEmpty())
            return false;
        index = (index + 1) % numberOfElements;
        if (index == 0)
            actualElement = head;
        else
            actualElement = actualElement.next;
        return true;
    }

    public Object getActualElementData() {
        return actualElement.data;
    }

    public void setActualElementData(Object data) {
        actualElement.data = data;
    }
}
于 2010-09-18T18:27:39.650 回答
12

对于实际应用(例如,不仅是玩耍或学习),我个人更喜欢 Guava 的Iterables.cycle方法 - 请参阅Iterables.cycle

于 2011-08-31T14:37:49.507 回答
3

java.util.LinkedList 是 Collections 数据类型之一。Collections 的目的是提供实用结构,而不是让程序员担心它们的内部实现。如果您必须拥有以某种方式工作的内部组件,而 java.util 不能保证它们是如何工作的,那么它们不适合您。

要实现循环链​​表,首先创建一个 ListNode 类:

class ListNode {
    ListNode next;
    ListNode prev;
    Object data;
}

然后存储 a ListNode head,并确保指向列表的“end”,并prev确保“end”指向。不过,老实说,保持尾指针的双向链表和循环链表之间几乎没有区别。headnexthead

于 2010-09-18T15:10:00.780 回答
0

您可以使用 Deque 使用简单的解决方案。从双端队列中汇集最后一个值并将其作为第一个插入。这将导致值的循环旋转

Deque<String> circularStructure = new ArrayDeque();
circularStructure.addAll(List.of("Val1","Val2","Val3"));

for(i=0;i<100;i++){
   String value = typeCodeStack.pollLast(); //take and remove last
   typeCodeStack.push(code); //add as first
   //section where to use value 
   someOperationOnValue(value)
});
于 2022-02-21T12:18:59.297 回答