數據結構筆記:單向鏈表

原文地址

分類目錄——數據結構筆記

  • 離散存儲,手拉手,每一塊有指向下一塊的指針(形象描述,python中沒有指針),就好像形成了一條鏈

  • 一個元素包括兩部分:value 和 next

  • 鏈表與順序表都是線性表

  • 知識點補充

    b = 20
    a = 'achar'
    a = b
    # 在python中,所有的變量保存的都是值的地址(就相當於c語言中的指針)
    # 等號右邊表示執行,=b中的b就是執行,根據值的地址取到b的值,就成了a=20,而這時的操作,是把20的地址返給a;也因此,在python中,之前給a的字符串變量可以在之後換成int的變量,這次其他語言中是不能實現的。
    # 等號實質上是改變了數據指向
    

    在鏈表實現時,爲next賦值即是這種機制

  • 節點類

    class Node(object):
        def __init__(self, value):
            self.value = value
            self.next = None
    
  • 鏈表類及常用方法實現

    構造的時候要考慮到特殊情況,常用參考項有鏈表爲空時,鏈表只有一個元素時,鏈表尾部時。

    class SinglCycleLinkList(object):
        def __init__(self, node=None):
            self.head = None
            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  # 指針
            count = 1  # 計數器
            while cur.next != self.head:
                count += 1
                cur = cur.next
            return count
    
        def travel(self):
            '''遍歷鏈表'''
            if self.is_empty():
                return
            cur = self.head  # 指針
            while cur.next != self.head:
                print(cur.value, end=' ')
                cur = cur.next
            print(cur.value)
    
        def add(self, value):
            '''頭部插入——頭插法'''
            node = Node(value)
            if self.is_empty():
                self.head = node
                node.next = self.head
            else:
                cur = self.head
                while cur.next != self.head:
                    cur = cur.next
                node.next = self.head  # 把新節點夾在head之前
                self.head = node       # 鏈表的新頭部編程node
                cur.next = self.head   # 尾部指向新頭部
    
    
        def append(self, value):
            '''尾部插入——尾插法'''
            node = Node(value)
            if self.is_empty():
                self.head = node
                node.next = self.head
            else:
                cur = self.head
                while cur.next != self.head:
                    cur = cur.next
                cur.next = node
                node.next = self.head
    
        def insert(self, index, value):
            '''
            :param index: the position of insert(start from 0)
            :param value: node.value
            :return:
            '''
            node = Node(value)
            if index <= 0:
                self.add(value)
            elif index > self.length():
                self.append(value)
            else:
                cur = self.head
                count = 0
                while count < index - 1:
                    count += 1
                    cur = cur.next
                node.next = cur.next
                cur.next = node
    
        def remove(self, item):
            '''從鏈表中刪除item'''
            # 如果爲空
            if self.head == None:
                return
            # 如果第一個命中
            if self.head.value == item:
                # 如果只有一個元素
                if self.length() == 1:
                    self.head = None
                    return
                else:
                    rear = self.head    # 用這個索引去找尾
                    while rear.next != self.head:
                        rear = rear.next
                    self.head = self.head.next
                    rear.next = self.head
            else:
                cur = self.head
                while cur.next != self.head and cur.next.value != item:
                    cur = cur.next
                if cur.next.value == item:
                    cur.next = cur.next.next
    
        def search(self, item):
            '''在鏈表中查找item,含有返回True,不含返回False(因爲是鏈表,返回索引也沒有意義)'''
            if self.is_empty():
                return False
            cur = self.head
            if cur.value == item:
                return True
            else:
                while cur != self.head:
                    cur = cur.next
                    if cur.value == item:
                        return True
                return False
    
    • 測試

      if __name__ == '__main__':
          sll1 = SinglLinkList()
          print(sll1.is_empty())
          print(sll1.length())
          sll1.append(6)
          print(sll1.is_empty())
          sll1.add(10)
          sll1.append(7)
          sll1.append(8)
          print(sll1.length())
          sll1.insert(4, 22)
          sll1.travel()
          sll1.remove(10)
          sll1.travel()
          sll1.remove(7)
          sll1.travel()
          sll1.remove(22)
          sll1.travel()
          sll1.remove(10)
          sll1.travel()
      
  • 順序表與鏈表

    • 鏈表可以實現分類內存
    • 鏈表無法直接定位到表中元素,增刪改查都需要遍歷表,時間複雜度相對更高一些;順序表可以直接定位元素
    • 在中間插入上,順序表的時間複雜度也是O(n),其時間消耗在於保序操作上;鏈表中的時間操作在於定位上
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章