用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

相关推荐
caimouse17 小时前
reactos编码规范
c语言·开发语言
xieliyu.21 小时前
Java算法精讲:双指针(三)
java·开发语言·算法
love530love21 小时前
LiveTalking 数字人项目 Windows 部署完全指南(EPGF 架构)
人工智能·windows·python·架构·livetalking·epgf
遇事不決洛必達1 天前
【Python基础】GIL 锁是什么及其对爬虫的影响
爬虫·python·线程·进程·gil锁
CryptoPP1 天前
快速对接东京证券交易所API数据:实战指南与代码示例
开发语言·人工智能·windows·python·信息可视化·区块链
ZC跨境爬虫1 天前
跟着 MDN 学JavaScript day_7:数学运算与逻辑判断实战测试
开发语言·前端·javascript·学习·ecmascript
探物 AI1 天前
把 MambaOut 塞进 YOLOv11:会有什么样的反应
python·yolo·计算机视觉
如竟没有火炬1 天前
最大矩阵——单调栈
数据结构·python·线性代数·算法·leetcode·矩阵
阳区欠1 天前
【LangChain】LLM基础介绍
开发语言·python·langchain
Cosolar1 天前
保姆级 CrewAI 教程:从零构建多智能体协作系统
人工智能·python·架构