資料結構( Pyhon 語言描述 ) — —第9章:列表
- 概念
- 列表是一個線性的集合,允許使用者在任意位置插入、刪除、訪問和替換元素
- 使用列表
- 基於索引的操作
- 基於內容的操作
- 基於位置的操作
- 列表的介面
- 列表的應用
- 列表可能是計算機領域應用最為廣泛的集合
- 堆儲存管理
- 在一個可用的列表中,將堆上可用空間的連續塊連結起來。當應用程式需要一個新的例項化物件時,PVM會在可用列表中搜索大小足夠容納物件的第一個塊。當不再需要物件的時候,垃圾收集程式會把物件的空間返回給可用列表
- 為了減少搜尋時間,可以使用多個可用列表,分別包含不同位元組大小的塊;最後一個列表應該包含比某個指定的大小還要大的所有的塊。
- 組織磁碟上的檔案
- 計算機檔案系統有3個主要的部分:檔案的目錄、檔案自身以及可用空間。
- 磁碟的物理格式
- 檔案系統
- 假設目錄佔用了磁碟上的前幾個磁軌並且為每個檔案包含了一個條目。這個條目包含檔案的資訊和扇區地址,這個扇區地址指向檔案中的第 1 個位元組
- 根據檔案的大小,其可能完全包含在一個扇區,或者同時跨越幾個扇區。
- 組成檔案的扇區並不需要物理相鄰,扇區的最後有一個指標,該指標指向了包含檔案的下一個扇區
- 列表在檔案系統中的應用
- 沒有使用的扇區在一個可用列表中被連結起來。當建立新檔案時,它們從這個列表中得到分配的空間,當刪除舊檔案時,它們的空間釋放到這個列表中
- 磁碟讀寫步驟
- 讀/寫頭定位到正確的磁軌
- 磁碟轉動,直到目標扇區在讀寫頭下
- 進行資料讀寫
- 3個步驟中,資料讀寫所需要時間最少
- 效能優化
- 根據磁碟的特性,當跨多個扇區的檔案,沒有在磁碟上分散開時,磁碟的效能會得到優化
- 檔案系統中包含一個工具,它會重新組織檔案系統以便每個檔案中的扇區都是連續的,並且具有相同的物理順序和邏輯順序。從而優化磁碟的效能。
- 其他集合的實現
- 列表可用來實現其它集合,通常有兩種方法
- 擴充套件列表類,讓新的類成為列表類的一個子類;
- 在新的類中,使用列表類的一個例項,並且讓該列表包含資料項
- 列表可用來實現其它集合,通常有兩種方法
- 列表實現
- 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)
-
- 連結串列實現
- 採用包含一個哨兵節點的雙鏈表結構
- 採用包含一個哨兵節點的雙鏈表結構
- AbstractList 類的角色