Data Structures Algorithms 简明教程

Doubly Linked List Data Structure

What is Doubly Linked List?

双向链表是链表的一种变化形式,相较于单向链表,它可以用一种简单的方式实现向前和向后的导航。以下是理解双向链表概念的重要术语。

Doubly Linked List is a variation of Linked list in which navigation is possible in both ways, forward as well as backward easily as compared to Single Linked List. Following are the important terms to understand the concept of doubly linked list.

  1. Link − Each link of a linked list can store a data called an element.

  2. Next − Each link of a linked list contains a link to the next link called Next.

  3. Prev − Each link of a linked list contains a link to the previous link called Prev.

  4. Linked List − A Linked List contains the connection link to the first link called First and to the last link called Last.

Doubly Linked List Representation

doubly linked list representation

根据上述说明,以下是要考虑的重要事项。

As per the above illustration, following are the important points to be considered.

  1. Doubly Linked List contains a link element called first and last.

  2. Each link carries a data field(s) and a link field called next.

  3. Each link is linked with its next link using its next link.

  4. Each link is linked with its previous link using its previous link.

  5. The last link carries a link as null to mark the end of the list.

Basic Operations in Doubly Linked List

以下是列表支持的基本操作。

Following are the basic operations supported by a list.

  1. Insertion − Adds an element at the beginning of the list.

  2. Insert Last − Adds an element at the end of the list.

  3. Insert After − Adds an element after an item of the list.

  4. Deletion − Deletes an element at the beginning of the list.

  5. Delete Last − Deletes an element from the end of the list.

  6. Delete − Deletes an element from the list using the key.

  7. Display forward − Displays the complete list in a forward manner.

  8. Display backward − Displays the complete list in a backward manner.

Doubly Linked List - Insertion at the Beginning

在此操作中,我们创建了一个包含三个区室的新节点,其中一个包含数据,另一个包含列表中其前一个和下一个节点的地址。这个新节点插入列表开头。

In this operation, we create a new node with three compartments, one containing the data, the others containing the address of its previous and next nodes in the list. This new node is inserted at the beginning of the list.

Algorithm

1. START
2. Create a new node with three variables: prev, data, next.
3. Store the new data in the data variable
4. If the list is empty, make the new node as head.
5. Otherwise, link the address of the existing first node to the
next variable of the new node, and assign null to the prev variable.
6. Point the head to the new node.
7. END

Example

以下是该操作在各种编程语言中的实现 −

Following are the implementations of this operation in various programming languages −

Doubly Linked List - Insertion at the End

在此插入操作中,新输入节点将添加到双向链表的末尾;如果列表不为空。如果列表为空,则头指针将指向新节点。

In this insertion operation, the new input node is added at the end of the doubly linked list; if the list is not empty. The head will be pointed to the new node, if the list is empty.

Algorithm

1. START
2. If the list is empty, add the node to the list and point
   the head to it.
3. If the list is not empty, find the last node of the list.
4. Create a link between the last node in the list and the
   new node.
5. The new node will point to NULL as it is the new last node.
6. END

Example

以下是该操作在各种编程语言中的实现 −

Following are the implementations of this operation in various programming languages −

Doubly Linked List - Deletion at the Beginning

此删除操作会删除双向链表中现有的第一个节点。头指针移到下一个节点,并移除链接。

This deletion operation deletes the existing first nodes in the doubly linked list. The head is shifted to the next node and the link is removed.

Algorithm

1. START
2. Check the status of the doubly linked list
3. If the list is empty, deletion is not possible
4. If the list is not empty, the head pointer is
   shifted to the next node.
5. END

Example

以下是该操作在各种编程语言中的实现 −

Following are the implementations of this operation in various programming languages −

Doubly Linked List - Complete Implementation

以下是用各种编程语言对双向链表的完整实现 −

Following are the complete implementations of Doubly Linked List in various programming languages −