Python 如何在不交换数据的情况下对双链接列表进行气泡排序(仅传输链接)

Python 如何在不交换数据的情况下对双链接列表进行气泡排序(仅传输链接),python,sorting,doubly-linked-list,Python,Sorting,Doubly Linked List,我的代码在排序方法上有问题。我需要在不更改任何数据的情况下对双链接列表中的节点进行排序,可能仅在节点的下一个和上一个部分传输(更改节点) 我尝试通过以下示例进行排序: def sort(self): # Check if head is None if self.head is None: return if self.head.next is None: return # Sor

我的代码在排序方法上有问题。我需要在不更改任何数据的情况下对双链接列表中的节点进行排序,可能仅在节点的下一个和上一个部分传输(更改节点)

我尝试通过以下示例进行排序:

def sort(self):
        # Check if head is None
        if self.head is None:
            return

        if self.head.next is None:
            return

        # Sort
        change = 1
        while change:
            # Variables
            change = 0
            curr = self.head
            # Sort one iteration
            while curr.next is not None:
                if curr.data < curr.next.data:
                    change = 1

                    temp_curr = curr.prev
                    print(temp_curr)
                    curr.prev = curr.next
                    print(curr.prev)
                    curr.next = curr.next.next
                    print(curr.next)
                    temp_next = curr.next.prev
                    curr.next.prev = temp_curr
                    curr.next.next = temp_next
                curr = curr.next

class ListNode(object):
    def __init__(self, data):
        # store data
        self.data = data
        # store reference (next item)
        self.next = None
        # store reference (previous item)
        self.previous = None


class List(object):
    def __init__(self):
        self.head = None

    def add_list_item(self, item):
        if isinstance(item, ListNode):
            if self.head is None:
                self.head = item
                item.previous = None
                item.next = None
            else:
                curr = self.head
                while curr.next is not None:
                    # Iterate
                    curr = curr.next
                # Add item into tail
                curr.next = item
                item.previous = curr
                item.next = None

    def print_ls(self):
        # Start from head
        current_node = self.head
        # Iterate via the whole list
        while current_node is not None:
            # Print one at one
            print(current_node.data)
            # jump to the linked node
            current_node = current_node.next

    def sort(self):
        # Check if head is None
        if self.head is None:
            return

        if self.head.next is None:
            return

        # Sort
        change = 1
        while change:
            # Variables
            change = 0
            curr = self.head
            # Sort one iteration
            while curr.next is not None:
                if curr.data < curr.next.data:
                    change = 1

                    temp_curr = curr.prev
                    print(temp_curr)
                    curr.prev = curr.next
                    print(curr.prev)
                    curr.next = curr.next.next
                    print(curr.next)
                    temp_next = curr.next.prev
                    curr.next.prev = temp_curr
                    curr.next.next = temp_next
                curr = curr.next

if __name__ == '__main__':
    link_list = List()

    link_list.add_list_item(ListNode(2))
    link_list.add_list_item(ListNode(1))
    link_list.add_list_item(ListNode(3))

    link_list.sort()
    link_list.print_ls()
def排序(自):
#检查头部是否为无
如果self.head为无:
返回
如果self.head.next为无:
返回
#分类
变化=1
改变时:
#变数
变化=0
curr=self.head
#排序一次迭代
而curr.next不是None:
如果当前数据<当前下一个数据:
变化=1
当前温度=当前温度
打印(当前温度)
当前上一个=当前下一个
打印(当前版本)
curr.next=curr.next.next
打印(当前下一页)
临时下一个=当前下一个上一个
curr.next.prev=当前温度
当前下一步=临时下一步
curr=curr.next
类ListNode(对象):
定义初始化(自身,数据):
#存储数据
self.data=数据
#门店参考(下一项)
self.next=无
#门店参考(上一项)
self.previous=无
类列表(对象):
定义初始化(自):
self.head=无
def添加列表项目(自身,项目):
如果isinstance(项,列表节点):
如果self.head为无:
self.head=项目
item.previous=无
item.next=无
其他:
curr=self.head
而curr.next不是None:
#迭代
curr=curr.next
#将项目添加到尾部
curr.next=项目
item.previous=当前
item.next=无
def打印工具(自身):
#从头开始
当前_节点=self.head
#遍历整个列表
当前_节点不是无时:
#一张一张地打印
打印(当前节点数据)
#跳转到链接节点
当前节点=当前节点。下一步
def分拣(自我):
#检查头部是否为无
如果self.head为无:
返回
如果self.head.next为无:
返回
#分类
变化=1
改变时:
#变数
变化=0
curr=self.head
#排序一次迭代
而curr.next不是None:
如果当前数据<当前下一个数据:
变化=1
当前温度=当前温度
打印(当前温度)
当前上一个=当前下一个
打印(当前版本)
curr.next=curr.next.next
打印(当前下一页)
临时下一个=当前下一个上一个
curr.next.prev=当前温度
当前下一步=临时下一步
curr=curr.next
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
link_list=list()
链接列表。添加列表项(列表节点(2))
链接列表。添加列表项(列表节点(1))
链接列表。添加列表项(列表节点(3))
link_list.sort()
链接列表。打印列表()

在我通过读取python错误消息来修复一些bug之后,我注意到如果上一个节点根本不是节点,而是self.head,那么“previous”节点的临时变量将不起作用。在我通过读取python错误消息来修复一些bug之后,我注意到,如果上一个节点根本不是节点,而是self.head,那么“previous”节点的临时变量将不起作用