python中的雙向鏈表實現

來源:互聯網
上載者:User

標籤:wow   ini   next   owa   技術   代碼   操作方法   none   頁面   

引子

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

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

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

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

代碼
class Node(object):    def __init__(self, value):        self.value = value        # 前驅區        self.prev = None        # 後繼區        self.next = Noneclass 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

 

python中的雙向鏈表實現

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.