用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

相关推荐
databook13 小时前
Manim实现闪光轨迹特效
后端·python·动效
Juchecar14 小时前
解惑:NumPy 中 ndarray.ndim 到底是什么?
python
用户83562907805114 小时前
Python 删除 Excel 工作表中的空白行列
后端·python
Json_14 小时前
使用python-fastApi框架开发一个学校宿舍管理系统-前后端分离项目
后端·python·fastapi
数据智能老司机21 小时前
精通 Python 设计模式——分布式系统模式
python·设计模式·架构
数据智能老司机1 天前
精通 Python 设计模式——并发与异步模式
python·设计模式·编程语言
数据智能老司机1 天前
精通 Python 设计模式——测试模式
python·设计模式·架构
数据智能老司机1 天前
精通 Python 设计模式——性能模式
python·设计模式·架构
c8i1 天前
drf初步梳理
python·django
每日AI新事件1 天前
python的异步函数
python