简体   繁体   English

循环链表中索引处的元素

[英]element at index in a circular linked list

I was reading about circular linked lists. 我在阅读有关循环链表的信息。 Here is a code which I do not understand how it works. 这是我不明白它如何工作的代码。

public int elementAt(int index){
        if(index>size){
            return -1;
        }
        Node n = head;
        while(index-1!=0){ // this line is unclear for me
            n=n.next;
            index--;
        }
        return n.data;
    }

I would write the same code but in this way : 我会写相同的代码,但是这样:

public int elementAt(int index){
        if(index>size){
            return -1;
        }
        Node n = head;
        while(n.size != index){ // here is my change in the code
            n=n.next;
        }
        return n.data;
    }

Here is the whole code : http://algorithms.tutorialhorizon.com/circular-linked-list-complete-implementation/ 这是完整的代码: http : //algorithms.tutorialhorizo​​n.com/circular-linked-list-complete-implementation/

Am I doing right in the second code ? 我在第二个代码中正确吗?

Thanks 谢谢

The example code uses a 1-based index: 1, 2, 3, ..., size. 示例代码使用基于1的索引:1、2、3,...,大小。 Which is strange in computer science, where one would expect: 0, .. , size-1. 在计算机科学中,这是很奇怪的,人们会期望:0,..,size-1。

Unfortunately size is a property of the entire list, not a single Node in the list. 不幸的是, size是整个列表的属性,而不是列表中的单个节点。 So their solution is fine. 所以他们的解决方案很好。

Though when index <= 0 then nice things happen. 虽然当索引<= 0时,好事发生了。


For a real circular list the Node has a previous field. 对于真实的循环列表,节点具有previous字段。 And the last node is linked both ways to the first node. 最后一个节点双向链接到第一个节点。

In that case you can walk in both directions, following next or previous . 在这种情况下,您可以沿下nextprevious next方向双向行走。 Then when index < size / 2 one would by next forwards to the index, or else go back by previous for about (size - index) steps. 然后,当索引<size / 2时, next将前进到索引,否则按previous一步返回约(大小-索引)个步骤。 In order to take the least number of steps. 为了采取最少的步骤。

it is just doing a count. 它只是做一个计数。 You can do it in various way. 您可以通过多种方式进行操作。 I assume that the size is the size of your LinkedList. 我认为大小是您的LinkedList的大小。 In that case your code is wrong. 在这种情况下,您的代码是错误的。 you can do like following 你可以喜欢

public int elementAt(int index){
        if(index>size){
            return -1;
        }
        Node n = head;
        int i = 0; // zero-indexing
        while(i++ != index){ // you can increment i at the end too
            n=n.next;
        }
        return n.data;
    }

FIrst code is also counting, but instead of using another variable it used the existing one 第一个代码也在计数,但是没有使用另一个变量,而是使用了现有变量

The line which you do not understand is just counting "index" positions fordward, and it is starting at head. 您不理解的那条线只是计算“索引”位置的前移,它从头开始。 So the method is giving you back the element "index -1" positions from the head element Other assumption is that the head element is at 1 因此,该方法从head元素中退还元素“ index -1”的位置。其他假设是head元素为1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM