用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

相关推荐
Dxy123931021627 分钟前
DrissionPage调试工具:网页自动化与数据采集的革新利器
爬虫·python·drissionpage
阿蒙Amon1 小时前
C#获取磁盘容量:代码实现与应用场景解析
开发语言·c#
界面开发小八哥1 小时前
VS代码生成工具ReSharper v2025.1——支持.NET 10和C# 14预览功能
开发语言·ide·c#·.net·visual studio·resharper
不争先.1 小时前
URL 结构说明+路由(接口)的认识
python·pycharm·flask·apifox
(・Д・)ノ1 小时前
python打卡day44
人工智能·python·机器学习
胡西风_foxww2 小时前
Python 入门到进阶全指南:从语言特性到实战项目
开发语言·python·快速入门
bubiyoushang8882 小时前
matlab实现高斯烟羽模型算法
开发语言·算法·matlab
CN.LG2 小时前
C# 从 ConcurrentDictionary 中取出并移除第一个元素
java·开发语言·c#
菜是一种态度2 小时前
Python-多线程(一)
python·多线程·threading·_thread/thread
liuweidong08022 小时前
【Pandas】pandas DataFrame sample
python·数据挖掘·pandas