python中的單向循環鏈表實現


引子

所謂單向循環鏈表,不過是在單向鏈表的基礎上,如響尾蛇般將其首尾相連,也因此有諸多類似之處與務必留心之點。尤其是可能涉及到頭尾節點的操作,不可疏忽。

對於諸多操所必須的遍歷,這時的條件是什么?又應該在哪里停止?

在做刪除操作時,如若待刪除節點是頭或尾節點時,該如何處理?如果鏈表只有一個節點,又該如何處理?

代碼實現

class Node(object):
    def __init__(self, value):
        # 元素域
        self.value = value
        # 鏈接域
        self.next = None


class CircularLinkedListOneway(object):
    def __init__(self, node=None):
        # 構造非空鏈時,讓其地址域指向自己
        if node:
            node.next = node
        self.__head = node

    def is_empty(self):
        # 頭節點不為None則不為空
        return self.__head == None

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

    def traversal(self):
        if self.is_empty():
            return
        cur = self.__head
        while cur.next != self.__head:
            print(cur.value)
            cur = cur.next
        # 退出循環時,cur正是尾節點
        print(cur.value)

    def add(self, value):
        """頭插法"""
        node = Node(value)
        if self.is_empty():
            self.__head = node
            self.__head.next = self.__head
            return
        cur = self.__head
        while cur.next != self.__head:
            cur = cur.next
        # 新節點的next指針指向原頭節點
        node.next = self.__head
        # 將新節點指向頭節點
        self.__head = node
        # 尾節點next指針指向新頭節點
        cur.next = self.__head

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

    def insert(self, pos, value):
        if pos <= 0:
            self.add(value)
        elif pos > len(self) - 1:
            self.append(value)
        else:
            node = Node(value)
            cur = self.__head
            count = 0
            while count < pos - 1:
                count += 1
                cur = cur.next
            node.next = cur.next
            cur.next = node

    def search(self, value):
        if self.is_empty():
            return False
        cur = self.__head
        while cur.next != self.__head:
            if cur.value == value:
                return True
            else:
                cur = cur.next
        # 別忘了while循環外的尾節點
        if cur.value == value:
            return True
        return False

    def remove(self, value):
        cur = self.__head
        prior = None
        if self.is_empty():
            return
        while cur.next != self.__head:
            # 待刪除節點如果找到
            if cur.value == value:
                # 待刪除節點在頭部
                if cur == self.__head:
                    rear = self.__head
                    while rear.next != self.__head:
                        rear = rear.next
                    self.__head = cur.next
                    rear.next = self.__head
                # 待刪除節點在中間
                else:
                    prior.next = cur.next
          # 這里不是跳出循環的break,而是退出函數的return哦,因為已經處理完畢了
return # 如果還沒找到 else: prior = cur cur = cur.next # 待刪除節點在尾部 if cur.value == value: # 如果鏈表中只有一個元素,則此時prior為None,Next屬性就會報錯 # 此時直接使其頭部元素為None即可 if cur == self.__head: self.__head = None return prior.next = cur.next

 


免責聲明!

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



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