数据结构:链表(Linked Lists)

章节主题

  • 链表简介

  • 链表操作

  • 双重链表

  • 变化:尾部参考

  • 变化:循环链接列表

Chapter Topics

  • Introduction to Linked Lists 

  • Linked List Operations

  • Doubly Linked List

  • Variation: Tail reference

  • Variation: Circular Linked Lists

链表简介(Introduction to Linked Lists)

关于列表的基本事实

  • 列表是按顺序组织的对象集合。

  • 每个列表元素根据其在序列中的位置分配一个整数索引。

  • 序列中的第一个元素具有索引0,并且每个后续元素的索引1都大于其前一个元素。

Elementary Facts About Lists

  • A list is a collection of objects organized in a sequence.

  • Each list element is assigned an integer index based on its position in the sequence.

  • The first element in the sequence has index 0, and each succeeding element has index 1 greater than its predecessor.

连续分配

  • 列表的实现可以基于数组,这称为连续内存分配。

  • 在连续分配中,列表元素占用连续的内存位置。

Contiguous Allocation

  • Implementation of lists can be array-based, this is known as contiguous memory allocation.

  • In contiguous allocation, list elements occupy consecutive memory locations.

随机访问

  • 访问连续分配的列表元素非常快:给定任何索引k,我们可以通过将k项的大小(以字节为单位)添加到第一个元素的地址来计算该位置的列表元素的内存地址。名单。

  • 连续分配据说允许随机访问,因为我们可以直接跳转到任何给定元素而无需通过其前继。

Random Access

  • Access to a contiguously allocated list element is very fast: given any index k, we can compute the memory address of the list element at that position by adding the size (in bytes) of k items to the address of the first element in the list.

  • Contiguous allocation is said to allow random access because we can directly jump to any given element without going through its predecessors.

连续分配

  • 连续分配由基于数组的列表实现(如ArrayList和Vector)使用。

Contiguous Allocation

  • Contiguous allocation is used by array-based list implementations such as ArrayList and Vector.

连续分配的缺点

  • 在列表中间插入或删除元素涉及到之后所有元素的费力重新定位。

  • 例如,如果列表包含一千个元素,则在位置5处插入新元素意味着必须向上移动位置5上的所有元素。

  • 此开销对于执行大量插入和删除的应用程序而言是不利的。

Disadvantages of Contiguous Allocation

  • Insertion or deletion of elements in the middle of the list involves the laborious relocation of all elements that come after.

  • For example, if a list has a thousand elements, then to insert a new element at position 5 means all elements from position 5 on up must be moved up.

  • This overhead is bad for applications that do a lot of insertions and deletions.

链接分配

  • 在链接分配中,列表保留对其第一个元素的引用:这是头引用

  • 列表中的每个元素都保留对其后继者的引用,后者是列表中的后续元素。

  • 列表元素的内存不必是连续的。

Linked Allocation

  • In linked allocation, the list keeps a reference to its first element: this is the head reference

  • Every element in the list keeps a reference to its successor, the element that follows it on the list.

  • Memory for list elements does not have to be consecutive.

节点和链接

  • 包含list元素的对象和与其后继者的引用(链接)称为节点。

  • 对后继者的引用称为后继链接。

Nodes and Links

  • The objects that hold the list element and the reference (link) to its successor are called nodes.

  • The references to successors are called successor links.

链接分配的缺点

  • 给定列表中的节点,我们只能通过从第一个节点开始并跟随后继链接来访问它,直到我们到达所需节点。

  • 这称为顺序访问。

  • 顺序访问比随机访问需要更长的时间才能到达长列表末尾附近的节点。

Disadvantages of Linked Allocation

  • Given a node in the list, we can only access it by starting at the first node and following the successor links till we come to the desired node.

  • This is called sequential access.

  • Sequential access takes a lot longer than random access to get to nodes near the end of a long list.

链接列表的Java实现

  • 需要节点对象的Java实现。

  • 使用对具有第一个元素的节点的引用来表示列表。

  • 空列表用引用表示,其值为null。

  • 每个节点都有对其后继者的引用。

  • 最后一个节点的后继链接设置为null。

Java Implementation of Linked Lists

  • Requires a Java implementation of a node object.

  • A list is represented using a reference to the node with the first element.

  • An empty list is represented with a reference whose value is null.

  • Each node has a reference to its successor.

  • The successor link of the last node is set to null.

链表操作(Linked List Operations)

链接列表操作

  • 创建链接列表

  • 插入链接列表

  • 从链接列表中删除

  • 遍历链接列表

Linked Lists Operations

  • Creation of linked lists

  • Insertion into a linked list 

  • Removal from a linked list 

  • Traversal of a linked list

创建链接列表

创建第一个节点。 head = new Node(“Bob”);

最后添加/插入Carol。

  • 步骤1:创建Carol节点。

  • 步骤2:将Bob的下一个设置为Carol。

Creation of Linked Lists

  • Create the first node. head = new Node(“Bob”);

  • Add/Insert Carol at the end.

  • Step 1: create Carol node.

  • Step 2: set the next of Bob to Carol.

将节点插入链接列表

  • 将Brad插入Bob和Carol之间。

  • 第1步:找到Bob和下一个节点Carol。

  • 第2步:创建Brad,其下一个是Carol。

  • 第3步:更改Bob对Brad的下一个引用。

Inserting Nodes into a Linked List

  • Insert Brad in between Bob and Carol.

  • Step 1: find Bob and the next node Carol.

  • Step 2: create Brad whose next is Carol.

  • Step 3: change Bob’s next reference to Brad.

删除节点

  • 要删除列表的第一个节点,请将头引用设置为其后继节点。

    • 设置对头部的引用。

    • 设定新头脑鲍勃。

    • 将Alan的下一个设置为null以将其返回到系统*。

Removing a Node

  • To remove the first node of a list, set the head reference to its successor.

    • Set a reference to Alan, the head.

    • Set head to Bob, the new head.

    • Set the next of Alan to null to return it to the system*.

删除节点

要删除第一个节点以外的节点:

1.设置对目标节点的前任的引用。

2.在前导者中围绕目标节点路由后继链接。

Removing a Node

To remove a node other than the first:

1. Set a reference to the predecessor of the targeted node.

2. Route the successor link in the predecessor around the targeted node.

删除节点

要删除最后一个节点:

1.设置对倒数第二个节点的引用。

2.将其下一个引用设置为null。

Removing a Node

To remove the last node:

1. Set a reference to the second last node.

2. Set its next reference to null.

遍历链接列表

  • 遍历链表是一种系统的方法,按顺序检查列表中的每个元素。

  • 遍历通常在访问每个元素时执行一些操作。

Traversal of a Linked List

  • A traversal of a linked list is a systematic method of examining, in order, every element of the list.

  • A traversal usually performs some operation as it visits each element.

遍历链接列表

  • 要遍历链接列表,请在列表头部开始引用,并将其从节点移动到后继:

Traversal of a Linked List

  • To traverse a linked list, start a reference at the head of the list, and keep moving it from a node to a successor:

Node temp = head; 

while (temp != null) {

    do something about temp;

    go to the next node of temp; 

}

实施链接清单

  • 基于数组的实现(作为练习)

  • 基于参考的实施需求

            - 节点类Node作为独立或内部类。 - 指向第一个节点的头参考头

            - 计算节点数的计数

            - 常用操作:+ isEmpty():boolean

                + size():int

                + add(int,Object):void + add(Object):void

                + get(int):Object

                + remove(int):void + removeAll():void

注意:应在可以实现为基于数组或基于引用的类的接口中指定这些操作。

异常类

Implementation of A Linked List

  • Array-based implementation(as an exercise)

  • Reference-based implementation needs

            – A node class Node as a stand alone or an inner class. – A head reference head to point to the first node

            – A count counting the number of nodes

            – Common operations: + isEmpty(): boolean

                + size(): int

                + add(int , Object): void + add(Object): void

                + get(int): Object

                + remove(int) : void + removeAll(): void

Note: These operations should be specified in an interface that can be implemented to be an array-based or reference-based class.

Exception classes

双重链表(Doubly Linked Lists)

双重链接列表

  • 单链接列表允许单向遍历:一个可以从节点移动到其后继节点。

  • 限制:人们无法轻易地从节点移动到其前身。

  • 双向链表允许双向轻松转换。

Doubly Linked Lists

  • Singly Linked lists allow one way traversal: one can move from a node to its successor.

  • Limitation: one cannot easily move from a node to its predecessor.

  • Doubly linked lists allow easy transitions in both directions.

双链表的节点

•双向链表的节点必须同时具有后继链接和前驱链接:

Nodes for Doubly Linked Lists

• A node for a doubly linked list must have both a successor link and a predecessor link:

创建双重链接列表

  • 创建第一个节点。

head = new Node(“Bob”,null,null);

  • 在最后添加/插入Carol。

    • 步骤1:创建Carol节点,将其前一个设置为Bob。

    • 第2步:将Bob的下一个设置为Carol。

Creation of Doubly Linked Lists

  • Create the first node.

head = new Node(“Bob”, null, null);

  • Add/Insert Carol at the end.

    • Step 1: create Carol node, set its previous to Bob.

    • Step 2: set the next of Bob to Carol.

创建双重链接列表

  • 最后添加/插入新节点。

1.创建新节点。 将它的前继设置为插入点的前一个元素。

2.将插入点的前一个元素的后继设置为新元素

Creation of Doubly Linked Lists

  • Add/Insert Debby at the end.

1. Create Debby node. Set its previous to Carol.

2. Set the next of Carol to Debby.

创建双重链接列表

在前面添加/插入新节点。

1.使用后继引用现有头节点的头创建新节点。

2.将原头节点的前置以前设为新节点。

3.将头节点设为新节点。

Creation of Doubly Linked Lists

Add/Insert Alan at the front.

1. Create Alan node with the next reference to Bob, the head.

2. Set Bob’s previous to be Alan.

3. Change head to Alan.

将节点插入双向链接列表

•将新节点插入两个节点之间。

1.找到插入点的前后节点

2.创建新,其后继引用设为插入点的原后继结点,其前继引用设为插入点的原前继结点。

3.改变插入点的原前置结点的后继和插入点的原后继结点的前继结点为新结点。

Inserting Nodes into a Doubly Linked List

• Insert Brad in between Bob and Carol.

1. Find Bob and the next node Carol

2. Create Brad whose next reference is Carol and

previous reference is Bob.

3. Change Bob’s next and Carol’s previous to Brad.

删除节点

•要删除列表的第一个节点,请将头引用设置为其后继节点。

1.设置对头部结点的引用。

2.设置新的头部结点为原第二结点。

3.将原头部结点的后继设置为null以将原头部结点返回到系统*。

4.将原第二结点的前一个设置为null以将原头部结点返回到系统*。

Removing a Node

• To remove the first node of a list, set the head reference to its successor.

1. Set a reference to Alan, the head.

2. Set head to Bob, the new head.

3. Set the next of Alan to null to return Alan to the system*.

4. Set the previous of Bob to null to return Alan to the system*.

删除节点

要删除第一个节点以外的节点:

1.设置对目标节点的前任的引用。

2.在前导者中围绕目标节点路由后继链接。

Removing a Node

To remove a node other than the first:

1. Set a reference to the predecessor of the targeted node.

2. Route the successor link in the predecessor around the targeted node.

删除节点

要删除中间结点:

1.设置对卡罗尔的引用。 在步骤4之后删除此引用。

2.将要删除中间结点的前继节点的后继设置为要删除中间结点的后继节点。

3.将要删除中间结点的后继节点的前继设置为要删除中间结点的前继节点。

4.将要删除中间结点的next和previous设置为null以返回要删除中间结点

系统。

Removing a Node

To remove Carol:

1. Set a reference to Carol. Remove this reference after step 4.

2. Set the next of the previous node of Carol to the next node of Carol.

3. Set the previous of the next node of Carol to the previous node of Carol.

4. Set both next and previous of Carol to null to return Carol to

the system.

删除节点

要删除最后一个节点:

1.设置对倒数第二个节点的引用。 去掉

这个参考在第3步之后。

2.将其下一个节点的上一个节点设置为null

3.将其下一个引用设置为null。

Removing a Node

To remove the last node:

1. Set a reference to the second last node. Remove

this reference after step 3.

2. Set the previous of its next node to null

3. Set its next reference to null.

发布了32 篇原创文章 · 获赞 40 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_27467365/article/details/84261340