鏈表:單向循環鏈表

函數說明:

  • 初始化;
  • 判空;
  • 求長度;
  • 插入節點:頭插、尾插、隨機插入;
  • 遍歷節點;
  • 刪除節點;
  • 查找節點(搜索節點);

代碼:

class Node(object):
    def __init__(self, elem):
        self.elem = elem
        self.next = None
        
class SingleLinkList(object):
    """單向循環鏈表"""
    def __init__(self, node = None):
        self.__head = node
        if node:
            node.next = node
        
    def is_empty(self):
        """鏈表是否爲空"""
        return self.__head == None
    
    def length(self):
        if self.is_empty():
            return 0
        # 遊標用來遍歷節點
        cur = self.__head
        # 節點計數器,初始值影響while循環的判斷條件
        counter = 1
        while cur.next != self.__head :
            counter += 1
            cur = cur.next
        return counter
            
    def travel(self):
        if self.is_empty():
            return 
        cur = self.__head
        while cur.next != self.__head:
            print(cur.elem)
            cur = cur.next
#         退出循環,cur指向尾節點,但尾節點的元素未打印
        print(cur.elem)
        
    def add(self, item):
        """鏈表頭部添加元素"""
#         print("=====頭插法=====")
        node = Node(item)
        if self.is_empty():
            self.__head = node
            node.next = node
        else:
    #         遍歷尋找尾節點
            cur = self.__head
            while cur.next != self.__head:
                cur = cur.next
    #           退出循環,cur指向的就是尾節點
            node.next = self.__head
            self.__head = node
            cur.next = node
        
    
    def append(self, item):
        """鏈表尾部添加元素"""
        node = Node(item)
#         print("=====尾插法=====")
        if self.is_empty():
            self.__head = node
            node.next = node
        else:
            cur = self.__head
            while cur.next != self.__head:
                cur = cur.next
#             node.next = cur.next
            node.next = self.__head
            cur.next = node
    
    def insert(self, pos, item):
        """
        指定位置添加元素
        @pos: 從0開始 例:insert(2,100)        
        因爲不涉及尾部,所以不需要修改代碼。
        """
        if pos <= 0 :
            self.add(item)
        elif pos > (self.length() - 1):
            self.append(item)
        else:
            node = Node(item)
            pre = self.__head
            count = 0
            while count < (pos - 1):
                pre = pre.next
                count += 1
    #             當循環退出後,pre指向pos-1位置
            node.next = pre.next
            pre.next = node
        
    def search(self, item):
        """查找節點是否存在"""
        if self.is_empty(): # 判斷特殊情況
            return False
        cur = self.__head
        while cur.next != self.__head: # 修改條件,但丟了尾節點
            if cur.elem == item:
                return True
            else:
                cur = cur.next
#       退出循環,cur指向的就是尾節點
        if cur.elem == elem:
            return True
        return False
    
#     難度係數高
    def remove(self, item):
        if self.is_empty():
            return
        cur = self.__head
        pre = None
        while cur.next != self.__head: # 修改條件
            if cur.elem == item:
                # 先判斷此節點是否爲頭節點
                if cur == self.__head:
                    # 頭節點
                    # 找尾節點
                    rear = self.__head
                    while rear.next != self.__head:
                        rear = rear.next
                    self.__head = cur.next
                    rear.next = self.__head
                else:
                    # 中間節點
                    pre.next = cur.next
                return # break
            else: # 遊標移動
                pre = cur
                cur = cur.next
            # 退出循環,cur指向尾節點
        if cur.elem == item:
            if cur == self.__head:
                # 鏈表只有一個節點
                self.__head = None
            else:
#               pre.next = cur.next
                pre.next = self.__head
           
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章