数据结构与算法之链表 双向链表边界 双向数据遍历需求

数据结构与算法阿木 发布于 2025-07-11 6 次阅读


摘要:双向链表是一种常见的线性数据结构,它由一系列节点组成,每个节点包含数据域和两个指针域,分别指向前一个节点和后一个节点。本文将围绕双向链表的数据结构与算法,特别是边界操作,进行深入解析,并通过代码实现来展示双向链表的基本操作。

一、双向链表概述

1. 定义

双向链表是一种链式存储结构,每个节点包含数据域和两个指针域,分别指向前一个节点和后一个节点。双向链表既可以向前遍历,也可以向后遍历,这使得它在某些场景下比单向链表更灵活。

2. 特点

(1)插入和删除操作方便,只需修改前后节点的指针即可。

(2)可以方便地进行双向遍历。

(3)空间复杂度较高,每个节点需要额外的指针域。

二、双向链表的基本操作

1. 创建双向链表

python

class Node:


def __init__(self, data):


self.data = data


self.prev = None


self.next = None

class DoublyLinkedList:


def __init__(self):


self.head = None


self.tail = None

def append(self, data):


new_node = Node(data)


if self.head is None:


self.head = new_node


self.tail = new_node


else:


self.tail.next = new_node


new_node.prev = self.tail


self.tail = new_node

def display(self):


current = self.head


while current:


print(current.data, end=' ')


current = current.next


print()


2. 插入节点

python

def insert(self, data, position):


new_node = Node(data)


if position == 0:


new_node.next = self.head


if self.head:


self.head.prev = new_node


self.head = new_node


if self.tail is None:


self.tail = new_node


elif position == -1:


self.append(data)


else:


current = self.head


for _ in range(position - 1):


if current is None:


raise IndexError("Position out of range")


current = current.next


new_node.next = current.next


new_node.prev = current


if current.next:


current.next.prev = new_node


current.next = new_node


if new_node.next is None:


self.tail = new_node


3. 删除节点

python

def delete(self, position):


if self.head is None:


raise Exception("List is empty")


if position == 0:


self.head = self.head.next


if self.head:


self.head.prev = None


else:


self.tail = None


elif position == -1:


self.tail = self.tail.prev


self.tail.next = None


else:


current = self.head


for _ in range(position):


if current is None:


raise IndexError("Position out of range")


current = current.next


if current.next:


current.next.prev = current.prev


current.prev.next = current.next


if current == self.tail:


self.tail = current.prev


4. 遍历双向链表

python

def forward_traverse(self):


current = self.head


while current:


print(current.data, end=' ')


current = current.next


print()

def backward_traverse(self):


current = self.tail


while current:


print(current.data, end=' ')


current = current.prev


print()


三、双向链表边界操作

1. 边界插入

python

def insert_at_head(self, data):


new_node = Node(data)


new_node.next = self.head


if self.head:


self.head.prev = new_node


self.head = new_node


if self.tail is None:


self.tail = new_node

def insert_at_tail(self, data):


new_node = Node(data)


new_node.prev = self.tail


if self.tail:


self.tail.next = new_node


self.tail = new_node


if self.head is None:


self.head = new_node


2. 边界删除

python

def delete_at_head(self):


if self.head is None:


raise Exception("List is empty")


self.head = self.head.next


if self.head:


self.head.prev = None


else:


self.tail = None

def delete_at_tail(self):


if self.tail is None:


raise Exception("List is empty")


self.tail = self.tail.prev


self.tail.next = None


if self.tail is None:


self.head = None


四、总结

本文详细介绍了双向链表的数据结构与算法,特别是边界操作。通过代码实现,展示了双向链表的基本操作,包括创建、插入、删除和遍历。在实际应用中,双向链表在需要双向遍历的场景下具有很大的优势。希望本文能帮助读者更好地理解双向链表及其边界操作。