问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

链表:灵活的数据结构

创作时间:
作者:
@小白创作中心

链表:灵活的数据结构

引用
CSDN
1.
https://m.blog.csdn.net/Whoisbug/article/details/145085222

链表是一种常见的数据结构,它由一系列节点组成,每个节点包含数据部分和指向下一个节点的指针。链表具有动态大小和高效插入、删除操作的特点,使其在许多应用场景中都非常有用。本文将详细介绍链表的基本概念、操作以及应用场景,并通过示例和图表来帮助你更好地理解链表。

链表的基本概念

链表是一种线性数据结构,其中的元素以节点的形式存储,每个节点包含两部分:数据和指向下一个节点的指针。链表的最后一个节点的指针指向null,表示链表的结束。

单链表(Singly Linked List)

单链表是最简单的链表形式,每个节点只有一个指向下一个节点的指针。单链表的节点结构如下:

class ListNode:
    def __init__(self, value=0, next=None):
        self.value = value
        self.next = next  

双链表(Doubly Linked List)

双链表的每个节点有两个指针,一个指向前一个节点,另一个指向后一个节点。双链表的节点结构如下:

class ListNode:
    def __init__(self, value=0, prev=None, next=None):
        self.value = value
        self.prev = prev
        self.next = next  

循环链表(Circular Linked List)

循环链表的最后一个节点的指针指向第一个节点,形成一个闭环。循环链表可以是单向的,也可以是双向的。

链表的操作

链表的基本操作包括插入、删除、查找和遍历。这些操作的时间复杂度通常为O(n),因为可能需要遍历整个链表。

插入操作

在链表中插入节点可以在头部、尾部或中间位置进行。插入操作的时间复杂度为O(1),但找到插入位置的时间复杂度为O(n)。

在头部插入

def insert_at_head(head, value):
    new_node = ListNode(value)
    new_node.next = head
    return new_node  

在尾部插入

def insert_at_tail(head, value):
    new_node = ListNode(value)
    if head is None:
        return new_node
    current = head
    while current.next:
        current = current.next
    current.next = new_node
    return head  

在中间插入

def insert_at_middle(head, value, position):
    if position == 0:
        return insert_at_head(head, value)
    new_node = ListNode(value)
    current = head
    for _ in range(position - 1):
        if current is None:
            return head
        current = current.next
    new_node.next = current.next
    current.next = new_node
    return head  

删除操作

在链表中删除节点可以在头部、尾部或中间位置进行。删除操作的时间复杂度为O(1),但找到删除位置的时间复杂度为O(n)。

删除头部节点

def delete_at_head(head):
    if head is None:
        return None
    return head.next  

删除尾部节点

def delete_at_tail(head):
    if head is None:
        return None
    if head.next is None:
        return None
    current = head
    while current.next.next:
        current = current.next
    current.next = None
    return head  

删除中间节点

def delete_at_middle(head, position):
    if head is None or position == 0:
        return delete_at_head(head)
    current = head
    for _ in range(position - 1):
        if current is None or current.next is None:
            return head
        current = current.next
    if current.next is None:
        return head
    current.next = current.next.next
    return head  

查找操作

在链表中查找节点需要遍历整个链表,时间复杂度为O(n)。

def find_node(head, value):
    current = head
    while current:
        if current.value == value:
            return current
        current = current.next
    return None  

遍历操作

遍历链表可以打印出所有节点的值,时间复杂度为O(n)。

def traverse_list(head):
    current = head
    while current:
        print(current.value, end=" -> ")
        current = current.next
    print("None")  

链表的应用场景

链表在许多应用场景中都非常有用,例如:

  • 实现栈和队列:链表可以高效地实现栈和队列的插入和删除操作。
  • 动态内存分配:链表可以动态地分配内存,适合不确定大小的数据集合。
  • 符号表:在编译器中,链表可以用于实现符号表,存储变量和函数的定义。
  • 多级反馈队列调度:在操作系统中,链表可以用于实现多级反馈队列调度算法。

示例:实现栈

使用链表实现栈,支持push和pop操作。

class Stack:
    def __init__(self):
        self.head = None
    def push(self, value):
        new_node = ListNode(value)
        new_node.next = self.head
        self.head = new_node
    def pop(self):
        if self.head is None:
            return None
        value = self.head.value
        self.head = self.head.next
        return value
    def is_empty(self):
        return self.head is None
    def top(self):
        if self.head is None:
            return None
        return self.head.value  

示例:实现队列

使用链表实现队列,支持enqueue和dequeue操作。

class Queue:
    def __init__(self):
        self.head = None
        self.tail = None
    def enqueue(self, value):
        new_node = ListNode(value)
        if self.tail is None:
            self.head = self.tail = new_node
        else:
            self.tail.next = new_node
            self.tail = new_node
    def dequeue(self):
        if self.head is None:
            return None
        value = self.head.value
        self.head = self.head.next
        if self.head is None:
            self.tail = None
        return value
    def is_empty(self):
        return self.head is None
    def front(self):
        if self.head is None:
            return None
        return self.head.value  

链表的变体

双向链表

双向链表的每个节点有两个指针,一个指向前一个节点,另一个指向后一个节点。这使得在双向链表中可以向前和向后遍历。

双向链表的插入操作

def insert_at_head_doubly(head, value):
    new_node = ListNode(value)
    if head is None:
        return new_node
    new_node.next = head
    head.prev = new_node
    return new_node  

双向链表的删除操作

def delete_at_head_doubly(head):
    if head is None:
        return None
    if head.next is None:
        return None
    head = head.next
    head.prev = None
    return head  

循环链表

循环链表的最后一个节点的指针指向第一个节点,形成一个闭环。这使得在循环链表中可以无缝地遍历整个链表。

循环链表的插入操作

def insert_at_tail_circular(head, value):
    new_node = ListNode(value)
    if head is None:
        new_node.next = new_node
        return new_node
    current = head
    while current.next != head:
        current = current.next
    current.next = new_node
    new_node.next = head
    return head  

循环链表的删除操作

def delete_at_tail_circular(head):
    if head is None:
        return None
    if head.next == head:
        return None
    current = head
    while current.next.next != head:
        current = current.next
    current.next = head
    return head  

总结

链表是一种灵活的数据结构,适用于需要动态大小和高效插入、删除操作的场景。通过理解链表的基本概念和操作,你可以更好地应用链表来解决实际问题。希望本文的示例和图表能帮助你更好地理解和掌握链表。

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号