我可以使用 java.util.LinkedList 构造循环链表吗?

发布于 2024-09-19 13:23:24 字数 225 浏览 5 评论 0原文

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

如果我不能使用 java.util.LinkedList,我应该如何创建自己的循环/循环链表实现?您能给我展示一下这个实现的框架吗?

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

I would like to create a circular/cyclic linked list where the tail of the list would point back to the head of the list. So can I use java.util.LinkedList and modify the tail node after creation of the list to make it circular/cyclic? If so, can you show me some code on how that would happen?

If I can't use java.util.LinkedList, how should I create my own circular/cyclic linked list implementation? Can you show me the skeletons of how this implementation would look?

Let me know if you need more details and I'll clear up any confusion.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

尹雨沫 2024-09-26 13:23:25

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

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

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

然后存储一个ListNode头,并确保headprev指向“列表的“end”,“end”的“next”指向回“head”。但老实说,保留尾指针的双向链表和循环链表之间没有什么区别。

java.util.LinkedList is one of the Collections datatypes. The purpose of Collections is to provide utility structures, not bothering the programmer to worry about their internal implementation. If you must have internals that work in a certain way, and the java.util ones do not guarantee that is how they work, then they are not for you.

To implement a circular linked list, first create a ListNode class:

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

Then store a ListNode head, and make sure prev of head points to the "end" of the list, and next of the "end" points back to head. Honestly, though, there's little difference between a bidirectionally linked list keeping a tail pointer and a circular linked list.

心如狂蝶 2024-09-26 13:23:25

您可以使用 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)
});

You can use simple solution using Deque. Pool last value from deque and insert it as first. This will cause cyclic roatation of values

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)
});
巨坚强 2024-09-26 13:23:24
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;
    }
}
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;
    }
}
没有心的人 2024-09-26 13:23:24

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

For practical application (e.g. not only playing around or learning) I would personally prefer Guava's Iterables.cycle method - see Iterables.cycle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文