用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

相关推荐
qq_40698103几秒前
c++的decltype是什么
开发语言·c++
坤坤子吖6 分钟前
算法学习——单调队列
开发语言·c++·笔记·学习·算法
我就是全世界15 分钟前
OpenCode Go 套餐划算吗:先看你用哪个模型
开发语言·后端·golang
螺丝钉tina28 分钟前
[Playwright] [02]常用命令与调试:Debug UI、Mode和 Trace Viewer
开发语言·javascript·测试工具·ui·自动化
杜 硕38 分钟前
从零开始的C++生活——第六讲——01
开发语言·c++·生活
taller_20001 小时前
【012】数据类型入门: Excel 数据到 Python 值的转换
python·excel·pie·py
淡漠的蓝精灵1 小时前
web.py:一个简单到极致的 Python Web 框架
开发语言·前端·python·其他
乌暮1 小时前
请求进门先安检:Spring 拦截器与统一功能实战指南
java·开发语言·后端·学习·spring
起个名字费劲死了2 小时前
Halcon学习一:骰子识别
前端·python·学习
এ慕ོ冬℘゜2 小时前
JS 事件循环、宏任务微任务:单线程底层原理 + 执行顺序
开发语言·javascript·ecmascript