简体   繁体   English

单链表和双链表之间是否有性能差异?

[英]Are there any performance differences between singly- and doubly-linked lists?

Our homework assignment asks us to prove that the Java LinkedList implementation is doubly-linked and not singly-linked. 我们的作业要求我们证明Java LinkedList实现是双链接而不是单链接。 However, list operations such as adding elements, removing elements, and looking elements up seem to have the same complexity for both implementations, so there doesn't seem to be a way to use a performance argument to demonstrate the doubly-linked nature of Java's LinkedList . 但是,列表操作(例如添加元素,删除元素和查找元素)对于这两种实现似乎都具有相同的复杂性,因此似乎没有一种方法可以使用性能参数来证明Java的双链接性质。 LinkedList Anyone know of a better way to illustrate the difference between the two? 有谁知道更好的方法来说明两者之间的区别?

看一下在向前或向后方向上进行迭代,删除“ before-last”元素,等等。

这很容易证明-您查看源代码,看每个节点都有一个.previous指针:) http://www.docjar.com/html/api/java/util/LinkedList.java.html

Consider the following nodes, single and double. 考虑以下节点,单节点和双节点。

class SingleLinkedNode { E data; class SingleLinkedNode {E数据; SingleLinkedNode next; 接下来是SingleLinkedNode; } }

class DoubleLinkedNode { E data; class DoubleLinkedNode {E数据; DoubleLinkedNode prev; DoubleLinkedNode上一个; DoubleLinkedNode next; 接下来是DoubleLinkedNode; } }

If we want to remove from a DoubleLinkedList (assuming we have already FOUND the node, which is very different) what do we need to do? 如果我们想从DoubleLinkedList中删除(假设我们已经找到该节点,这是非常不同的),我们该怎么做?

  1. Make the node before the deleted one point to the one after. 使删除前的节点指向之后的节点。
  2. Make the node after the deleted one point to the one before. 使删除后的节点指向之前的节点。

If we want to remove from a SingleLinkedList (assuming we have already FOUND the node, which is very different) what do we need to do? 如果我们想从SingleLinkedList中删除(假设我们已经找到该节点,这是非常不同的),我们需要做什么?

  1. Make the node before the deleted one point to the one after. 使删除前的节点指向之后的节点。

You'd think that means it's even faster in a single linked list than a double. 您可能认为这意味着单个链接列表中的链接甚至比双链接列表中的链接列表更快。

But, how are we doing to delete the node if we don't have a reference to the one before it? 但是,如果我们之前没有对该节点的引用,该如何删除该节点? We only have a reference to the next. 我们只有下一个参考。 Wouldn't we have to do a whole other search on the list just to find prev? 为了找到上一个,我们不必在列表上进行其他搜索吗? :-O :-O

The Java List interface doesn't have a method which allows you to remove an item without searching through a linked list. Java List接口没有允许您在不搜索链接列表的情况下删除项目的方法。 It has remove(int index), which would have to scan the list to find the indexed entry, and it also has remove(Object o), which has to scan the list as well. 它具有remove(int index),它将必须扫描列表以找到已索引的条目,它还具有remove(Object o),它也必须扫描列表。 Since a linked list implementation can save the necessary previous-item entry context while scanning, remove has equivalent complexity for singly- and doubly-linked lists. 由于链表实现可以在扫描时保存必要的前项输入上下文,因此删除对单链表和双链表具有相同的复杂性。 This state can be saved in an iterator, as well, so Iterator.remove() doesn't change this. 此状态也可以保存在迭代器中,因此Iterator.remove()不会更改此状态。 So I don't think you can tell from remove performance. 因此,我认为您无法从删除性能中看出什么。

My guess is that the "right" answer to this is to create several lists of different sizes and time the performance of .indexOf() and .lastIndexOf() when searching for the first or last object. 我的猜测是对此的“正确”答案是创建多个不同大小的列表,并在搜索第一个或最后一个对象时计时.indexOf()和.lastIndexOf()的性能。 Presuming that the implementation is doubly-linked and searches from the beginning of the list for .indexOf() and searches from the end for .lastIndexOf(), the performance will be length-dependent or length-independent. 假设实现是双向链接的,并且从列表的开头搜索.indexOf(),然后从列表的末尾搜索.lastIndexOf(),则性能将取决于长度或与长度无关。

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

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