循环链表以及从节点内部访问上一个/下一个

发布于 2024-12-25 04:17:22 字数 512 浏览 1 评论 0原文

我需要对象的循环列表。每个人都应该知道哪一个是上一个或下一个。我这样做了:

class Bus {

    private Bus previous;
    private Bus next;

    public Bus() {
      //anything
    }

    public void setPrevious(Bus bus) {
      this.previous = bus;
    }

    public void setNext(Bus bus) {
      this.next = bus;
    }

    private void someMethod() {
     // if (previous.xxx() && next.xxx()) {
     //   do something
     // } 
    }

}

我创建了一个 Bus 数组。将所有总线添加到其中后,我设置每个元素的下一个和上一个。我觉得它很丑:D。你能建议我更好的方法吗?

I need circular list of objects. And each one should know which is previous or next. I did this:

class Bus {

    private Bus previous;
    private Bus next;

    public Bus() {
      //anything
    }

    public void setPrevious(Bus bus) {
      this.previous = bus;
    }

    public void setNext(Bus bus) {
      this.next = bus;
    }

    private void someMethod() {
     // if (previous.xxx() && next.xxx()) {
     //   do something
     // } 
    }

}

And I created an array of Bus. After I add all buses into it, I set next and previous of each element. And I feel it's ugly:D. May you suggest me better way?

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

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

发布评论

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

评论(4

星光不落少年眉 2025-01-01 04:17:22

如果您调整 setNextsetPrevious 方法,不仅更新其实例,还更新设置为 nextprevious 的实例 你想要的必须依赖于外部机制。

假设您最初创建了 Bus AB。当您调用 A.setNext( B ) 时,它还应该更新 B 的前一个节点,而不必调用 B.setPrevious( A )。与在 Java 中向 LinkedList 添加内容类似,您不必手动设置最后一个对象与刚刚添加的对象之间的链接。当然

public void setPrevious(Bus bus) {
  this.previous = bus;
  if ( bus.next != this ){
    bus.next = this;
  }
}

,您仍然必须考虑总线已经包含在另一个 List 中的情况,您也必须更新该 List

因此,按照其他响应之一的建议将节点与实际总线实例分开的建议是一个更好的主意。这允许您将总线添加到多个列表,并且可以更轻松地编写循环列表(或者仅使用列表的可用实现)。这也是更好的面向对象设计,因为您可以重用您编写的列表。

If you adjust your setNext and setPrevious methods to not only update their instance but also the instance that is set as next and as previous you want have to depend on an external mechanism.

So let's say you have initially created Bus A and B. When you call A.setNext( B ), it should also update the previous node of B, without having to call B.setPrevious( A ). Similar as when you add something to a LinkedList in Java, you do not have to manually set the link between the last object and the object you just added. Something like

public void setPrevious(Bus bus) {
  this.previous = bus;
  if ( bus.next != this ){
    bus.next = this;
  }
}

Of course then you still have to consider the scenario where the bus is already contained in another List that you have to update that List as well.

Therefore, the suggestion to separate the nodes from the actual bus instances as suggested in one of the other responses is a better idea. This allows you to add busses to multiple lists, and makes it probably easier to write your circular list (or just use an available implementation for the list). This is also better OO design as you can reuse the list you wrote.

刘备忘录 2025-01-01 04:17:22

只是一个想法,但也许有一个用于节点的类和一个用于列表的类。通过这种方式,您可以为节点类提供一个构造函数,用于处理设置其下一个和最后一个引用。这使得列表实际上只需要担心像 Add() 这样的标准列表函数。

看一下列表界面。

Just a thought, but maybe have one class for the nodes and one for the list. In this way, you can have a constructor for the node class that handles setting its next and last references. This allows the list to really only need to worry about standard list functions like Add().

Take a look at the List interface.

林空鹿饮溪 2025-01-01 04:17:22

链表不需要数组。要创建列表,您需要使用 next 和 previous 方法来连接对象并将最后一个与第一个连接(以进行循环)。和使用示例(使用您的实现):

Bus one = new Bus();
Bus two = new Bus();
Bus three = new Bus();

one.setPrevious(three);
one.setNext(two);

two.setPrevious(one);
two.setNext(three);

three.setPrevious(two);
three.setNext(one);

或多或少是链表循环的想法。我建议查看另一个问题来理解链接列表。做圆形很容易。

A linked list don't need an array. To create the list you need to use next and previous methods to connect your objects and connect the last with the first (to do circular). And example of use (using your implementation):

Bus one = new Bus();
Bus two = new Bus();
Bus three = new Bus();

one.setPrevious(three);
one.setNext(two);

two.setPrevious(one);
two.setNext(three);

three.setPrevious(two);
three.setNext(one);

That more or less the idea of a Linked List circular. I recommend to see that another question to understand a Linked List. To do it circular is easy.

老子叫无熙 2025-01-01 04:17:22

为什么需要数组?您已经拥有每个对象中的引用。

Why do you need an array? You already have the references in each object.

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