1. 程式人生 > >資料結構( Pyhon 語言描述 ) — —第9章:列表

資料結構( Pyhon 語言描述 ) — —第9章:列表

    1. 概念
      • 列表是一個線性的集合,允許使用者在任意位置插入、刪除、訪問和替換元素
    2. 使用列表
      • 基於索引的操作
        • 基本操作
          • clip_image001
        • 陣列與列表的區別
          • 陣列是一種具體的資料結構,擁有基於單個的實體記憶體塊的一種特定的,不變的實現。
          • 列表是一種抽象的資料型別,可以由各種方式表示,陣列只是其中一種方式
      • 基於內容的操作
        • 基本操作
          • clip_image002
      • 基於位置的操作
        • 相對於遊標位置執行,這個操作允許程式設計師在通過移動遊標在列表中導航。其可通過列表迭代器來實現
        • 列表迭代器是附加到列表的後備儲存
          • clip_image003
        • 列表迭代器遊標的位置
          • 第一項之前
          • 相領兩項之間
          • 最後一項之後
          • 列表迭代器第一次例項化的時候,其遊標是未定義的
        • 列表迭代器的方法
          • 導航操作
            • clip_image004
          • 修改操作
            • clip_image005
            • remove replace 操作均需要滿足先驗條件
      • 列表的介面
        • 基本列表操作概覽
          • clip_image006
        • 列表和列表迭代器的介面和實現類
          • clip_image007
    3. 列表的應用
      • 列表可能是計算機領域應用最為廣泛的集合
      • 堆儲存管理
        • 在一個可用的列表中,將堆上可用空間的連續塊連結起來。當應用程式需要一個新的例項化物件時,PVM會在可用列表中搜索大小足夠容納物件的第一個塊。當不再需要物件的時候,垃圾收集程式會把物件的空間返回給可用列表
        • 為了減少搜尋時間,可以使用多個可用列表,分別包含不同位元組大小的塊;最後一個列表應該包含比某個指定的大小還要大的所有的塊。
      • 組織磁碟上的檔案
        • 計算機檔案系統有3個主要的部分:檔案的目錄、檔案自身以及可用空間。
        • 磁碟的物理格式
          • 磁碟的表面被劃分為多個同心圓磁軌,每個磁軌被進一步劃分為扇區。所有磁軌包含的扇區數相同,所有扇區包含的位元組數也相同
          • 示意圖
            • clip_image008
        • 檔案系統
          • 假設目錄佔用了磁碟上的前幾個磁軌並且為每個檔案包含了一個條目。這個條目包含檔案的資訊和扇區地址,這個扇區地址指向檔案中的第 1 個位元組
          • 根據檔案的大小,其可能完全包含在一個扇區,或者同時跨越幾個扇區。
          • 組成檔案的扇區並不需要物理相鄰,扇區的最後有一個指標,該指標指向了包含檔案的下一個扇區
        • 列表在檔案系統中的應用
          • 沒有使用的扇區在一個可用列表中被連結起來。當建立新檔案時,它們從這個列表中得到分配的空間,當刪除舊檔案時,它們的空間釋放到這個列表中
        • 磁碟讀寫步驟
          • /寫頭定位到正確的磁軌
          • 磁碟轉動,直到目標扇區在讀寫頭下
          • 進行資料讀寫
          • 3個步驟中,資料讀寫所需要時間最少
        • 效能優化
          • 根據磁碟的特性,當跨多個扇區的檔案,沒有在磁碟上分散開時,磁碟的效能會得到優化
          • 檔案系統中包含一個工具,它會重新組織檔案系統以便每個檔案中的扇區都是連續的,並且具有相同的物理順序和邏輯順序。從而優化磁碟的效能。
      • 其他集合的實現
        • 列表可用來實現其它集合,通常有兩種方法
          • 擴充套件列表類,讓新的類成為列表類的一個子類;
          • 在新的類中,使用列表類的一個例項,並且讓該列表包含資料項
    4. 列表實現
      • AbstractList 類的角色
        • 列表迭代器會使用 self._modCount 這個例項變數在某些方法上強制施加先驗條件
        • 程式碼實現
          • #!/usr/bin/env python

            # -*- coding:utf-8 -*-

            # Author:Lijunjie

             

            from abstractcollection import AbstractCollection

             

             

            class AbstractList(AbstractCollection):

                """An abstract list implementation."""

             

                def __init__(self, sourceCollection):

                    """Maintains a count of modifications to the left."""

                    self._modCount = 0

                    AbstractCollection.__init__(self, sourceCollection)

             

                def getModCount(self):

                    """Returns the count of modifications to the list."""

                    return self._modCount

             

                def incModCount(self):

                    """Increment the count of modifications."""

                    self._modCount += 1

             

                def index(self, item):

                    """Precondition: the item is in the list.

                    Return the position of the item.

                    Raise: ValueError if item isn't in the list"""

                    position = 0

                    for data in self:

                        if data == item:

                            return position

                        else:

                            position += 1

                    if position == len(self):

                        raise ValueError(str(item) + " not in list.")

             

                def add(self, item):

                    """Add the item to the end of the list."""

                    self.insert(len(self), item)

             

                def remove(self, item):

                    """Precondition: the item is in the list.

                    Raise: ValueError is item isn't in the list.

                    PostCondition:item is removed from self."""

                    position = self.index(item)

                    self.pop(position)

      • 基於陣列的實現
        • ArrayList 有一個初始的預設大小,當需要時,其大小會自動變化
        • 程式碼實現
          • #!/usr/bin/env python

            # -*- coding:utf-8 -*-

            # Author:Lijunjie

             

            from abstractcollection import AbstractCollection

             

             

            class AbstractList(AbstractCollection):

                """An abstract list implementation."""

             

                def __init__(self, sourceCollection):

                    """Maintains a count of modifications to the left."""

                    self._modCount = 0

                    AbstractCollection.__init__(self, sourceCollection)

             

                def getModCount(self):

                    """Returns the count of modifications to the list."""

                    return self._modCount

             

                def incModCount(self):

                    """Increment the count of modifications."""

                    self._modCount += 1

             

                def index(self, item):

                    """Precondition: the item is in the list.

                    Return the position of the item.

                    Raise: ValueError if item isn't in the list"""

                    position = 0

                    for data in self:

                        if data == item:

                            return position

                        else:

                            position += 1

                    if position == len(self):

                        raise ValueError(str(item) + " not in list.")

             

                def add(self, item):

                    """Add the item to the end of the list."""

                    self.insert(len(self), item)

             

                def remove(self, item):

                    """Precondition: the item is in the list.

                    Raise: ValueError is item isn't in the list.

                    PostCondition:item is removed from self."""

                    position = self.index(item)

                    self.pop(position)

      • 連結串列實現
        • 採用包含一個哨兵節點的雙鏈表結構