python中的雙向鏈表實現


引子

雙向鏈表比之單向鏈表,多數操作方法的實現都沒有什么不同,如is_empty, __len__, traverse, search。這些方法都沒有涉及節點的變動,也就可通過繼承單向鏈表來實現即可。

不同之處一是在於節點實現的不同。因為增加了指向前一個節點的前驅區,因此需要為節點添加一個新屬性prev,用以指向前一個節點。

另外一點就是在做增刪的操作時,需要額外考慮節點的前驅區的指向。其中的remove方法更是需要考慮多種特殊情況。

下面給出代碼前,先放一個自己做的圖示。(右鍵選擇在新頁面打開可看完整圖示)

代碼

class Node(object):
    def __init__(self, value):
        self.value = value
        # 前驅區
        self.prev = None
        # 后繼區
        self.next = None


class LinkedListTwoway(object):
    def __init__(self):
        self.__head = None

    def is_empty(self):
        return self.__head is None

    def __len__(self):
        count = 0
        cur = self.__head
        while cur:
            count += 1
            cur = cur.next
        return count

    def traverse(self):
        cur = self.__head
        while cur:
            print(cur.value)
            cur = cur.next

    def add(self, value):
        node = Node(value)
        if self.is_empty():
            self.__head = node
        else:
            # 待插入節點的后繼區指向原頭節點
            node.next = self.__head
            # 原頭節點的前驅區指向待插入節點
            self.__head.prev = node
            self.__head = node

    def append(self, value):
        node = Node(value)
        cur = self.__head
        if self.is_empty():
            self.__head = Node
            return
        while cur.next:
            cur = cur.next
        cur.next = node
        node.prev = cur

    def insert(self, pos, value):
        if pos <= 0:
            self.add(value)
        elif pos > len(self) - 1:
            self.append(value)
        else:
            # 單向鏈表中為了在特定位置插入,要先在鏈表中找到待插入位置和其前一個位置
            # 雙向鏈表中就不需要兩個游標了(當然單向鏈表中一個游標也是可以只找前一個位置)
            node = Node(value)
            count = 0
            cur = self.__head
            while count < pos - 1:
                count += 1
                cur = cur.next
            # 此時的游標指向pos的前一個位置
            # 這里的相互指向需尤為注意,有多種實現,需細細分析
            node.next = cur.next
            cur.next.prev = node
            node.prev = cur
            cur.next = node

    def search(self, value):
        cur = self.__head
        while cur:
            if cur.value == value:
                return True
            else:
                cur = cur.next
        return False

    def remove(self, value):
        if self.is_empty():
            return
        cur = self.__head
        while cur:
            if cur.value == value:
                if cur == self.__head:
                    self.__head = cur.next
                    # 處理鏈表只有一個節點的特殊情況
                    if cur.next:
                        cur.next.prev = None
                else:
                    cur.prev.next = cur.next
                    # 處理待刪除節點是最后一個情況
                    if cur.next:
                        cur.next.prev = cur.prev
                return
            else:
                cur = cur.next

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM