展开链表2024 年 8 月 28 日 | 阅读 9 分钟 展开链表(Unrolled Linked List)是一种线性数据结构,它是链表的一个变体。展开链表在每个节点中存储一个完整的数组,而不是仅存储一个元素。展开链表结合了数组的优点(低内存开销)和链表的优点(快速插入和删除),从而实现了显著的性能提升。通过在每个节点存储多个元素,展开链表有效地将链表的开销分摊到多个元素上。 因此,如果一个展开链表的每个节点都持有一个包含四个元素的数组,那么链表的开销(指针)就会被分摊到这四个元素上。当考虑到现代CPU的缓存管理时,展开链表的性能更优。与标准的单向链表相比,展开链表每个节点的开销略高。然而,在大多数情况下,与现代计算机带来的优势相比,这个缺点是微不足道的。 展开链表是一种链表,其中每个节点都包含一个值数组,而不仅仅是一个值。每个节点的数组可以包含任何传统数组可以包含的内容,包括基本类型和其他抽象数据类型。每个节点可以容纳最大数量的值,大多数实现会保持每个节点的容量在3/4左右。当特定的数组溢出时,它会通过在节点之间转移值来实现平衡。 展开链表的每个节点开销略高,因为每个节点需要存储其数组可以容纳的最大值数量。然而,与标准链表相比,它的每个值的成本要低得多。随着每个节点中数组的最大尺寸增加,每个值所需的平均存储空间会减少,当值的类型非常小时,空间优势会更加显著。展开链表是数组和链表的混合体。它利用了数组超快的索引速度和存储局部性。这两个优点都源于静态数组的底层特性。此外,它保留了链表在节点插入和删除方面的优势。 在展开链表中插入和删除元素的算法因实现而异。通常将低水位线(low-water mark)设置为50%,这意味着如果一个元素无法插入到某个节点中,就会创建一个新节点,并将当前节点的一半元素移入新节点。如果我们删除一个元素,导致该节点中的值的数量低于50%,我们会从相邻数组中插入元素到该节点,使其恢复到50%以上。如果那个相邻数组也低于50%,则将两个节点合并。 这使得节点的平均利用率大约在70-75%之间。当节点大小适中时,与链表相比,其开销非常低。链表中的每个节点大约需要两到三个单位的开销。而展开链表的开销被分摊到其元素上,使其开销为1/size,这与数组的开销相似。要调整展开链表的性能,可以更改低水位线。通过提高低水位线,可以提高每个节点的平均利用率。然而,这将需要更频繁的分裂和合并操作。 Java 代码现在,让我们编写一个Java代码来实现一个展开链表,它将执行所有基本功能,如在展开链表上添加、删除和显示节点。 输出 上述代码给出以下输出。 Enter array size of each node 8 Please Choose one of the Operations:: 1. To insert Data in the Unrolled Linked List. 2. To Check List is empty or not. 3. To get the size of the Unrolled Linked List. 4. To clear the Unrolled Linked List. 5. To print/display the Data in the Unrolled Linked List. 1 Enter integer element to insert 12 Do you want to continue (Type y or n) y Please Choose one of the Operations:: 1. To insert Data in the Unrolled Linked List. 2. To Check List is empty or not. 3. To get the size of the Unrolled Linked List. 4. To clear the Unrolled Linked List. 5. To print/display the Data in the Unrolled Linked List. 1 Enter integer element to insert 2 Do you want to continue (Type y or n) y Please Choose one of the Operations:: 1. To insert Data in the Unrolled Linked List. 2. To Check List is empty or not. 3. To get the size of the Unrolled Linked List. 4. To clear the Unrolled Linked List. 5. To print/display the Data in the Unrolled Linked List. 1 Enter integer element to insert 3 Do you want to continue (Type y or n) y Please Choose one of the Operations:: 1. To insert Data in the Unrolled Linked List. 2. To Check List is empty or not. 3. To get the size of the Unrolled Linked List. 4. To clear the Unrolled Linked List. 5. To print/display the Data in the Unrolled Linked List. 1 Enter integer element to insert 4 Do you want to continue (Type y or n) y Please Choose one of the Operations:: 1. To insert Data in the Unrolled Linked List. 2. To Check List is empty or not. 3. To get the size of the Unrolled Linked List. 4. To clear the Unrolled Linked List. 5. To print/display the Data in the Unrolled Linked List. 5 Contents of Unrolled Linked List are:: Unrolled Linked List = 1 2 3 4 Wrong Entry Do you want to continue (Type y or n) y Please Choose one of the Operations:: 1. To insert Data in the Unrolled Linked List. 2. To Check List is empty or not. 3. To get the size of the Unrolled Linked List. 4. To clear the Unrolled Linked List. 5. To print/display the Data in the Unrolled Linked List. 2 Empty status = false Do you want to continue (Type y or n) y Please Choose one of the Operations:: 1. To insert Data in the Unrolled Linked List. 2. To Check List is empty or not. 3. To get the size of the Unrolled Linked List. 4. To clear the Unrolled Linked List. 5. To print/display the Data in the Unrolled Linked List. 3 Size = 4 Do you want to continue (Type y or n) y Please Choose one of the Operations:: 1. To insert Data in the Unrolled Linked List. 2. To Check List is empty or not. 3. To get the size of the Unrolled Linked List. 4. To clear the Unrolled Linked List. 5. To print/display the Data in the Unrolled Linked List. 5 Contents of Unrolled Linked List are:: Unrolled Linked List = 1 2 3 4 Do you want to continue (Type y or n) y Please Choose one of the Operations:: 1. To insert Data in the Unrolled Linked List. 2. To Check List is empty or not. 3. To get the size of the Unrolled Linked List. 4. To clear the Unrolled Linked List. 5. To print/display the Data in the Unrolled Linked List. 5 Contents of Unrolled Linked List are:: Unrolled Linked List = 1 2 3 4 Wrong Entry Do you want to continue (Type y or n) y Please Choose one of the Operations:: 1. To insert Data in the Unrolled Linked List. 2. To Check List is empty or not. 3. To get the size of the Unrolled Linked List. 4. To clear the Unrolled Linked List. 5. To print/display the Data in the Unrolled Linked List. 4 List Cleared Do you want to continue (Type y or n) y Please Choose one of the Operations:: 1. To insert Data in the Unrolled Linked List. 2. To Check List is empty or not. 3. To get the size of the Unrolled Linked List. 4. To clear the Unrolled Linked List. 5. To print/display the Data in the Unrolled Linked List. 5 Contents of Unrolled Linked List are:: Unrolled Linked List = empty Wrong Entry Do you want to continue (Type y or n) n 优点以下是展开链表的优点,例如:
下一个主题稀疏矩阵的类型 |
我们请求您订阅我们的新闻通讯以获取最新更新。