简体   繁体   English

在Java中使用数组实现链接列表?

[英]Implementing a Linked List using array in java?

I have read it in robbert laffore that we can implement Linked list using arrays as well. 我已经在robbert laf中阅读了它,然后我们也可以使用数组来实现链接列表。 But the question is how? 但是问题是如何?

I do have below approach on my mind: 我确实有以下想法:

1) Instead of a Link object use a array of size 2 one of which holds the item and other reference to the next one. 1)代替Link对象,使用大小为2的数组,其中一个保留该项目,而另一个则引用下一个。 But I don't thing it is a good way of doing. 但是我不认为这是一个好方法。

Can some one please suggest a better approach for implementing Linked list using arrays? 有人可以建议一种使用数组实现链接列表的更好方法吗?

It's called an ArrayList 它叫做ArrayList

Contrary to what @trutheality says, they don't require a fixed capacity and the nodes don't store an index to the next item. 与@trutheality所说的相反,它们不需要固定的容量,节点也不存储到下一项的索引。 To get around the size constraints of a typical array, ArrayLists are designed to automatically resize when they reach a pre-defined min/max boundary. 为了解决典型数组的大小限制,ArrayList设计为在达到预定的最小/最大边界时自动调整大小。

Resizing the internal array is expensive. 调整内部阵列的大小非常昂贵。 It includes creating a new array and moving the data from the old array to the new. 它包括创建一个新数组并将数据从旧数组移到新数组。 As such, it's beneficial to limit the number of resize operations needed. 因此,限制所需的调整大小操作的数量是有益的。

One approach is to double the array size when the list reaches the max capacity, and shrink it by half when the list hits 1/4 capacity. 一种方法是在列表达到最大容量时将数组大小加倍,而在列表达到1/4容量时将数组大小缩小一半。

The reason an array isn't shrunk at half capacity is to avoid thrashing. 阵列不收缩一半容量的原因是为了避免抖动。 Thrashing, is when an array increases/decreases in size on the edge of the capacity boundary, causing a lot of resize operations with few changes the internal data. 越界(thrashing)是指阵列在容量边界的边缘上增加/减小大小时,导致执行大量调整大小操作而内部数据很少更改的情况。

Despite the expense of resizing -- since it only happens when the dataset doubles -- the actual performance cost is only O(log n). 尽管调整大小会增加开销(由于仅在数据集加倍时才会发生),但实际的性能成本仅为O(log n)。 So, in insertion cost is linearithmic O(N log N) while retrieval is constant O(N). 因此,插入成本为线性方程O(N log N),而检索成本为常数O(N)。

There is one major weakness of ArrayLists. ArrayLists有一个主要弱点。 If you add/remove arbitrary items from the list the array contents will have to be shifted to accommodate the changes. 如果您从列表中添加/删除任意项,则阵列内容将不得不移动以适应更改。 An operation which costs linear O(N) time. 花费线性O(N)时间的操作。

Even though the cost of changing arbitrary items in a traditional LinkedList is cheap (ie constant O(1) time), the operation requires a lookup to find the position in the chain which costs linear O(N) time. 即使在传统的LinkedList中更改任意项的成本很便宜(即,恒定的O(1)时间),该操作也需要查找以找到链中位置,这会花费线性的O(N)时间。 Unless you're creating a Queue, where both ends of the list are being mutated frequently, using an ArrayList as a list foundation is probably the better choice. 除非您要创建一个Queue(列表的两端都被频繁更改)的队列,否则使用ArrayList作为列表基础可能是更好的选择。

Source: Currently taking an Algorithms course and just finished implementing both an ArrayList and a LinkedList from scratch. 资料来源:目前正在学习算法课程,刚刚从头开始完成ArrayList和LinkedList。

A typical array-backed linked list stores the entire list in a single array, eg: 典型的由阵列支持的链表将整个列表存储在单个阵列中,例如:

class ABLL<T> {
    Node[] theList;
    class Node {
         T item;
         int next; // You store the index of the next node
                   // instead of a reference
    }
    ...
}

You would initialize the array to some capacity, and it wouldn't grow beyond that capacity (unless you add that functionality, which would involve allocating a new array and copying the contents to it). 您将数组初始化为一定容量,并且不会超出该容量(除非您添加该功能,否则将涉及分配新数组并将内容复制到其中)。 You can search for "Array-backed Linked List" to find more discussion about this data structure. 您可以搜索“数组支持的链表”以找到有关此数据结构的更多讨论。

This makes less sense in Java than in C. 在Java中,这比在C语言中没有意义。

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

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