【问题标题】:How to make a circular list in Java如何在Java中制作循环列表
【发布时间】:2019-10-22 07:57:09
【问题描述】:

我想用 Java 创建一个circular LinkedList。我有一个包含点列表的多边形。 list 需要是圆形的。

我想创建循环列表:CircularList extends LinkedList,但我不知道如何继续。

感谢您的帮助。

【问题讨论】:

  • 请展示你的作品
  • 为什么要循环?
  • 你应该先看看谷歌。你在这里cs.williams.edu/~bailey/JavaStructures/doc/structure5/…
  • 您有什么理由需要这份清单通告吗?您的问题是否需要这样做,或者是否可以轻松地双向导航多边形的点?
  • @为什么它必须是循环的问题:我真的猜这是出于教育原因。此外,如果我认为在大多数情况下问题是针对特定数据结构的,那么问为什么要使用这种特定数据结构是没有意义的,因为答案大多是:这就是所要求的!

标签: java linked-list circular-list


【解决方案1】:

LinkedList 有 2 个对列表中第一个和最后一个节点的引用。

来自LinkedListsource code

/**
 * Pointer to first node.
 * Invariant: (first == null && last == null) ||
 *            (first.prev == null && first.item != null)
 */
transient Node<E> first;

/**
 * Pointer to last node.
 * Invariant: (first == null && last == null) ||
 *            (last.next == null && last.item != null)
 */
transient Node<E> last;

如您所见,第一个节点的“prev”成员设置为 null,最后一个节点的“next”成员设置为 null。 设置 first.prev= last 和 last.next=prev 将创建一个循环列表,但是(这里要小心)您必须覆盖linkedlist中的大多数插入、链接和删除操作以适应这种变化。您可能还需要为迭代器编写一些代码。 这不是微不足道的,因为您需要在迭代列表时跟踪停止的位置,否则您可能会陷入无限循环。 希望我说清楚了,答案会有所帮助。 问候

【讨论】:

【解决方案2】:

这是为您提供的快速解决方案。

检查以下代码以在 Java 中创建 circular LinkedList

public static void main(String arg[]) {
        CircularLinkedList cl = new CircularLinkedList();
        cl.add(1);
        cl.add(2);
        cl.add(3);
        cl.add(4);
        cl.display();
    }

    public class Node{
        int data;
        Node next;
        public Node(int data) {
            this.data = data;
        }
    }
    public Node head = null;
    public Node tail = null;
    public void add(int data){
        Node newNode = new Node(data);
        if(head == null) {
            head = newNode;
            tail = newNode;
            newNode.next = head;
        }
        else {
            tail.next = newNode;
            tail = newNode;
            tail.next = head;
        }
    }
    public void display() {
        Node current = head;
        if(head == null) {
            System.out.println("List is empty");
        }
        else {
            System.out.println("Nodes of the circular linked list: ");
             do{
                System.out.print(" "+ current.data);
                current = current.next;
            }while(current != head);
            System.out.println();
        }
    }

希望这个例子能帮助你理解circular LinkedList的概念。

【讨论】:

    【解决方案3】:

    我有一个由点列表组成的多边形。我有类 Point、Polygon 和 ListCircular。 这是我做的:

    import java.util.LinkedList;
    
    public class ListCircPoly <Point> extends LinkedList {
        private Node node;
        private Node last;
        private int size;
    
        public ListCircPoly()
        {
            super();
            node=null;//Création d'une chaine vide
            last=null;
            size=0;
        }
        /**
         * @pre /
         * @post ajoute une valeur en fin de list
         * @param s valeur à stocker
         * @return 
         */
    
        @Override
        public boolean add(Object s)
        {
            if (size==0)//Si la chaine est vide
            {
                this.node= new Node(s);
                this.node.next=this.node;
                last=node;
                size=1;
            }
            else
            {
                Node addNode= new Node(s);
                addNode.next=last.next;
                last.next=addNode;
                last=addNode;
                size++;
            }
            return true;
        }
    
        public void print()
        {
            if (size>0)
            {
                System.out.println(this.node.data);
                Node currentNode=this.node.next;
                while (currentNode!=this.node)
                {
                    System.out.println(currentNode.data);
                    currentNode=currentNode.next;
                }
            }
            else System.out.println("Lise vide");
    
        }
        private class Node
        {
            private Object data;
            private Node next;
    
            public Node(Object data)
            {
                this.data=data;
                this.next=null;
            }
        }
     }
    

    我想像使用linkedList一样使用它,但我的多边形类有问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-19
      • 1970-01-01
      • 1970-01-01
      • 2022-06-13
      • 2023-03-19
      相关资源
      最近更新 更多