用Python实现的双向链表类,包含了头插、尾插、归并排序等功能


下面是一个用Python实现的双向链表类,包含了头插、尾插、归并排序等功能。这个类还包含了基本的链表操作,如插入、删除、遍历等。

python 复制代码
  
下面是一个用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 is_empty(self):
        return self.head is None

    def insert_at_head(self, data):
        new_node = Node(data)
        if self.is_empty():
            self.head = self.tail = new_node
        else:
            new_node.next = self.head
            self.head.prev = new_node
            self.head = new_node

    def insert_at_tail(self, data):
        new_node = Node(data)
        if self.is_empty():
            self.head = self.tail = new_node
        else:
            new_node.prev = self.tail
            self.tail.next = new_node
            self.tail = new_node

    def delete_at_head(self):
        if self.is_empty():
            return None
        data = self.head.data
        if self.head == self.tail:
            self.head = self.tail = None
        else:
            self.head = self.head.next
            self.head.prev = None
        return data

    def delete_at_tail(self):
        if self.is_empty():
            return None
        data = self.tail.data
        if self.head == self.tail:
            self.head = self.tail = None
        else:
            self.tail = self.tail.prev
            self.tail.next = None
        return data

    def print_list(self):
        current = self.head
        while current:
            print(current.data, end=" <-> ")
            current = current.next
        print("None")

    def get_middle(self, head):
        if head is None:
            return head
        slow = head
        fast = head
        while fast.next and fast.next.next:
            slow = slow.next
            fast = fast.next.next
        return slow

    def merge(self, left, right):
        if left is None:
            return right
        if right is None:
            return left

        if left.data <= right.data:
            result = left
            result.next = self.merge(left.next, right)
            result.next.prev = result
        else:
            result = right
            result.next = self.merge(left, right.next)
            result.next.prev = result
        return result

    def merge_sort(self, head):
        if head is None or head.next is None:
            return head

        middle = self.get_middle(head)
        next_to_middle = middle.next

        middle.next = None
        next_to_middle.prev = None

        left = self.merge_sort(head)
        right = self.merge_sort(next_to_middle)

        sorted_list = self.merge(left, right)
        return sorted_list

    def sort(self):
        self.head = self.merge_sort(self.head)
        # Update the tail after sorting
        current = self.head
        while current and current.next:
            current = current.next
        self.tail = current

# Example usage:
dll = DoublyLinkedList()
dll.insert_at_tail(3)
dll.insert_at_tail(1)
dll.insert_at_tail(4)
dll.insert_at_tail(2)

print("Original list:")
dll.print_list()

dll.sort()
print("Sorted list:")
dll.print_list()

dll.insert_at_head(5)
print("After inserting 5 at head:")
dll.print_list()

dll.insert_at_tail(0)
print("After inserting 0 at tail:")
dll.print_list()

代码说明:

Node类:表示双向链表中的一个节点,包含数据、前驱指针和后继指针。

DoublyLinkedList类:表示双向链表,包含头节点和尾节点。

insert_at_head(data):在链表头部插入一个节点。

insert_at_tail(data):在链表尾部插入一个节点。

delete_at_head():删除链表头部的节点。

delete_at_tail():删除链表尾部的节点。

print_list():打印链表中的所有节点。

get_middle(head):找到链表的中间节点,用于归并排序。

merge(left, right):合并两个有序链表。

merge_sort(head):递归地对链表进行归并排序。

sort():对链表进行排序,并更新尾节点。

示例:

创建一个双向链表并插入一些数据。

对链表进行排序。

在链表头部和尾部插入新节点,并打印链表。

你可以根据需要扩展或修改这个类。


Original list:

3 <-> 1 <-> 4 <-> 2 <-> None

Sorted list:

1 <-> 2 <-> 3 <-> 4 <-> None

After inserting 5 at head:

5 <-> 1 <-> 2 <-> 3 <-> 4 <-> None

After inserting 0 at tail:

5 <-> 1 <-> 2 <-> 3 <-> 4 <-> 0 <-> None

相关推荐
大学生亨亨29 分钟前
go语言八股文(五)
开发语言·笔记·golang
raoxiaoya30 分钟前
同时安装多个版本的golang
开发语言·后端·golang
miracletiger2 小时前
uv 新的包管理工具总结
linux·人工智能·python
cloues break.2 小时前
C++进阶----多态
开发语言·c++
我不会编程5552 小时前
Python Cookbook-6.10 保留对被绑定方法的引用且支持垃圾回收
开发语言·python
ʚɞ 短腿欧尼2 小时前
关系数据的可视化
python·pycharm·可视化·数据可视化·图表
道剑剑非道2 小时前
QT开发技术【qcustomplot 曲线与鼠标十字功能】
开发语言·qt·计算机外设
刘婉晴3 小时前
【环境配置】Mac电脑安装运行R语言教程 2025年
开发语言·macos·r语言
Despacito0o3 小时前
C++核心编程:类与对象全面解析
开发语言·c++
Tiger Z3 小时前
R 语言科研绘图第 43 期 --- 桑基图-冲击
开发语言·r语言·贴图