1. 概念
      • 列表是一个线性的集合,允许用户在任意位置插入、删除、访问和替换元素
    2. 使用列表
      • 基于索引的操作
        • 基本操作
        • 数组与列表的区别
          • 数组是一种具体的数据结构,拥有基于单个的物理内存块的一种特定的,不变的实现。
          • 列表是一种抽象的数据类型,可以由各种方式表示,数组只是其中一种方式
      • 基于内容的操作
        • 基本操作
      • 基于位置的操作
        • 相对于游标位置执行,这个操作允许程序员在通过移动游标在列表中导航。其可通过列表迭代器来实现
        • 列表迭代器是附加到列表的后备储存
        • 列表迭代器游标的位置
          • 第一项之前
          • 相领两项之间
          • 最后一项之后
          • 列表迭代器第一次实例化的时候,其游标是未定义的
        • 列表迭代器的方法
          • 导航操作
          • 修改操作
            • remove 和 replace 操作均需要满足先验条件
      • 列表的接口
        • 基本列表操作概览
        • 列表和列表迭代器的接口和实现类
    3. 列表的应用
      • 列表可能是计算机领域应用最为广泛的集合
      • 堆存储管理
        • 在一个可用的列表中,将堆上可用空间的连续块链接起来。当应用程序需要一个新的实例化对象时,PVM会在可用列表中搜索大小足够容纳对象的第一个块。当不再需要对象的时候,垃圾收集程序会把对象的空间返回给可用列表
        • 为了减少搜索时间,可以使用多个可用列表,分别包含不同字节大小的块;最后一个列表应该包含比某个指定的大小还要大的所有的块。
      • 组织磁盘上的文件
        • 计算机文件系统有3个主要的部分:文件的目录、文件自身以及可用空间。
        • 磁盘的物理格式
          • 磁盘的表面被划分为多个同心圆磁道,每个磁道被进一步划分为扇区。所有磁道包含的扇区数相同,所有扇区包含的字节数也相同
          • 示意图
        • 文件系统
          • 假设目录占用了磁盘上的前几个磁道并且为每个文件包含了一个条目。这个条目包含文件的信息和扇区地址,这个扇区地址指向文件中的第 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

            """

            File: ArrayList.py

            """

            from arrays import Array

            from abstractlist import  AbstractList

            from arraylistiterator import ArrayListIterator

            class ArrayList(AbstractList):

            """An array-based list implementation."""

            # Class variable

            DEFAULT_CAPACITY = 10

            def __init__(self, sourceCollection=None):

            """Sets the initial state of self, which includes the contents

            of sourceCollection, if it's present."""

            self._items = Array(ArrayList.DEFAULT_CAPACITY)

            AbstractList.__init__(self, sourceCollection)

            # Accessor method

            def __iter__(self):

            """Support iteration over a view of self."""

            cursor = 0

            while cursor < len(self):

            yield self._items[cursor]

            cursor += 1

            def __getitem__(self, i):

            """Precondition: 0<= i < len(self)

            Return the item at position i.

            Raises: IndexError"""

            if i < 0 or i >= len(self):

            raise IndexError("List out of range.")

            return self._items[i]

            # Mutator method

            def __setitem__(self, i, item):

            """Precondition: 0 <= i < len(self)

            Replace the item at position i.

            Raise: IndexError"""

            if i < 0 or i >= len(self):

            raise IndexError("List out of range.")

            self._items[i] = item

            def insert(self, i, item):

            """Inserts item at the position i."""

            # Resize the array if necessary

            self._growArray()

            # Set the position i between 0 and len(self)

            if i < 0: i = 0

            elif i > len(self): i = len(self)

            if i < len(self):

            for index in range(len(self), i, -1):

            self._items[index] = self._items[index - 1]

            self._items[i] = item

            self._size += 1

            self.incModCount()

            def pop(self, i=None):

            """Precondition: 0 <= i < len(self).

            Remove and return item at position i. if i is None, i is given a default of len(self) - 1"

            Raise: Index Error."""

            if i is None: i = len(self) - 1

            if i < 0 or i >= len(self):

            raise IndexError("List index out of range.")

            item = self._items[i]

            for index in range(i, len(self) - 1):

            self._items[index] = self._items[index + 1]

            self._items[len(self) - 1] = None

            self._size -= 1

            self.incModCount()

            # Resize array in necessary

            self._shrinkArray()

            return item

            def clear(self):

            """Clear the array list."""

            self._items = Array(ArrayList.DEFAULT_CAPACITY)

            self._size = 0

            self.incModCount()

            def listIterator(self):

            """Returns a list iterator."""

            return ArrayListIterator(self)

            def _growArray(self):

            """Grow the capacity of the array if necessary """

            physicalSize = len(self._items)

            if len(self) >= physicalSize:

            temp = Array(physicalSize * 2)

            index = 0

            for item in self:

            temp[index] = item

            index += 1

            self._items = temp

            def _shrinkArray(self):

            """Shrink the capacity of the array if necessary. """

            physicalSize = len(self._items)

            if len(self) <= physicalSize // 4 and physicalSize >= 2 * ArrayList.DEFAULT_CAPACITY:

            temp = Array(physicalSize // 2)

            index = 0

            for item in self:

            temp[index] = item

            index += 1

            self._items = temp

      • 链表实现
        • 采用包含一个哨兵节点的双链表结构
          • 示意图
          • 优点
            • 不需要一个单独的尾指针
            • 当插入或删除第一个或最后一个节点的时候,不需要重新设置头指针
        • _getNode(i) 辅助方法用来搜索第 i 个节点
        • 代码实现
          • #!/usr/bin/env python

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

            # Author:Lijunjie

            from node import TwoWayNode

            from abstractlist import AbstractList

            from linkedlistiterator import LinkedListIterator

            class LinkedList(AbstractList):

            """A link-based list implementation"""

            def __init__(self, sourceCollection=None):

            """Sets the initial state of self, which includes the contents

            of sourceCollection, if it's present."""

            self._head = TwoWayNode(None)

            self._head.previous = self._head.next = self._head

            AbstractList.__init__(self, sourceCollection)

            # Helper method returns node at position i

            def _getNode(self, i):

            """Helper method: returns a pointer to node at position i."""

            if i == len(self):  # Constant-time access to head node.

            return self._head

            if i == len(self) - 1: # or last data node

            return self._head.previous

            probe = self._head.next

            while i > 0:

            probe = probe.next

            i -= 1

            return probe

            # Accessor method

            def __iter__(self):

            """Support iteration over a view of self."""

            cursor = self._head.next

            while cursor != self._head:

            yield cursor.data

            cursor = cursor.next

            def __getitem__(self, i):

            """Precondition: 0<= i < len(self)

            Return the item at position i.

            Raises: IndexError"""

            if i < 0 or i >= len(self):

            raise IndexError("List out of range.")

            return self._getNode(i).data

            def getHead(self):

            return self._head

            # Mutator method

            def __setitem__(self, i, item):

            """Precondition: 0 <= i < len(self)

            Replace the item at position i.

            Raise: IndexError"""

            if i < 0 or i >= len(self):

            raise IndexError("List out of range.")

            self._getNode(i).data = item

            def insert(self, i, item):

            """Inserts item at the position i."""

            # Set the position i between 0 and len(self)

            if i < 0: i = 0

            elif i > len(self): i = len(self)

            theNode = self._getNode(i)

            newNode = TwoWayNode(item, theNode.previous, theNode)

            theNode.previous.next = newNode

            theNode.previous = newNode

            self._size += 1

            self.incModCount()

            def pop(self, i=None):

            """Precondition: 0 <= i < len(self).

            Remove and return item at position i. if i is None, i is given a default of len(self) - 1"

            Raise: Index Error."""

            if i is None: i = len(self) - 1

            if i < 0 or i >= len(self):

            raise IndexError("List index out of range.")

            theNode = self._getNode(i)

            item = theNode.data

            theNode.previous.next = theNode.next

            theNode.next.previous = theNode.previous

            self._size -= 1

            self.incModCount()

            return item

            def clear(self):

            """Clear the linked list."""

            self._head.previous = self._head.next = self._head

            self._size = 0

            self.incModCount()

            def listIterator(self):

            """Returns a list iterator."""

            return LinkedListIterator(self)

      • 两种实现的时间和空间分析
        • 列表操作的平均运行时间
        • 空间分析
          • 当数组的装载因子大于 1/3 时,其空间效率要比列表高,否则要低
    5. 实现列表迭代器
      • 列表迭代器的角色和作用
        • 列表迭代器对象依赖于相关联的列表,因为前者需要访问后者以定位项、替换项、插入项和删除项
        • 3种类型的先验条件
          • 如果hasNext 或 hasPrevious 分别返回 False,程序员不能运行 next 或 previous 操作
          • 不能在列表迭代器上运行连续性的修改器方法。在每次运行前,必须先运行一个 next 或 previous 以建立游标位置
            • 使用实例变量 self._lastItemPos 来判断,当位置未确定时,其值为 -1。当程序在列表迭代器上成功运行一个next 或 previous 操作时,其值变成了列表中的一个索引。
            • 列表迭代器中的修改器方法,通过检查 self._lastItemPos 以确保满足先验条件,并在成功修改列表后,将其重置为 -1
          • 在列表上使用列表迭代器方法时,程序不能使用列表修改器方法在列表自身之上进行修改(如果修改会影响列表的游标位置,__setitem__方法不会影响)
            • 通过比较列表的 modCount 和列表迭代器的 modCount 是否相同来判断
      • 数组实现
        • 代码
          • #!/usr/bin/env python

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

            # Author:Lijunjie

            """

            File:arraylistiterator.py

            """

            class ArrayListIterator(object):

            """Represents the list iterator for an array list."""

            def __init__(self, backingStore):

            """Set the initial state of the list iterator."""

            self._backingStore = backingStore

            self._modCount = backingStore.getModCount()

            self._cursor = 0

            self._lastItemPos = -1

            self.first()

            def first(self):

            """Resets the cursor to the beginning of the backing store."""

            self._cursor = 0

            self._lastItemPos = -1

            def last(self):

            """Moves the cursor to the end of the backing store."""

            self._cursor = len(self._backingStore)

            self._lastItemPos = -1

            def hasNext(self):

            """Return True if the iterator has a next item or False otherwise."""

            return self._cursor < len(self._backingStore)

            def next(self):

            """Precondition: hasNext returns True.The list has not been modified except by this iterator's mutator.

            Returns the current item and advances the cursor to the next item."""

            if not self.hasNext():

            raise ValueError("No next item in list iterator.")

            if self._modCount != self._backingStore.getModCount():

            raise AttributeError("Illegal modification of backing store.")

            self._lastItemPos = self._cursor

            self._cursor += 1

            return self._backingStore[self._lastItemPos]

            def hasPrevious(self):

            """Return True if the iterator has a previous item or False otherwise"""

            return self._cursor > 0

            def previous(self):

            """Precondition: hasPrevious returns True. The list has not been modified except by this iterator's mutator.

            """

            if not self.hasPrevious():

            raise ValueError("No previous item in list iterator.")

            if self._modCount != self._backingStore.getModCount():

            raise AttributeError("Illegal modification of backing store.")

            self._cursor -= 1

            self._lastItemPos = self._cursor

            return self._backingStore[self._lastItemPos]

            def replace(self, item):

            """Precondition: the current position is defined. The list has not been modified except by this iterator's

            mutator."""

            if self._lastItemPos == -1:

            raise AttributeError("The current position is undefined.")

            if self._modCount != self._backingStore.getModCount():

            raise AttributeError("Illegal modification of backing store.")

            self._backingStore[self._lastItemPos] = item

            self._lastItemPos = -1

            def insert(self, item):

            """Precondition: The list has not been modified except by this iterator's mutator."""

            if self._modCount != self._backingStore.getModCount():

            raise AttributeError("Illegal modification of backing store.")

            if self._lastItemPos == -1:

            # Cursor not defined, so add item to end of list.

            self._backingStore.add(item)

            else:

            self._backingStore.insert(self._lastItemPos, item)

            # If the item insert was obtained via next, move cursor forward

            if self._lastItemPos < self._cursor:

            self._cursor += 1

            self._lastItemPos = -1

            self._modCount += 1

            def remove(self):

            """Precondition: the current position is defined. The list has not been modified except by this iterator's

            mutator."""

            if self._lastItemPos == -1:

            raise AttributeError("The current position is undefined.")

            if self._modCount != self._backingStore.getModCount():

            raise AttributeError("List has been modified illegally.")

            self._backingStore.pop(self._lastItemPos)

            # If the item removed was obtained via next, move cursor back

            if self._lastItemPos < self._cursor:

            self._cursor -= 1

            self._modCount += 1

            self._lastItemPos = -1

      • 链表实现
        • 用后备储存链表结构中的节点来表示游标,通过将游标设置为下一个节点或者前一个节点来实现导航性方法。这些会使得可能的修改操作都成为常数时间
        • 代码
          • #!/usr/bin/env python

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

            # Author:Lijunjie

            """

            File:linkedlistiterator.py

            """

            from node import TwoWayNode

            class LinkedListIterator(object):

            """Represents the list iterator for an linked list."""

            def __init__(self, backingStore):

            """Set the initial state of the list iterator."""

            self._backingStore = backingStore

            self._modCount = backingStore.getModCount()

            self._head = backingStore.getHead()

            self._cursor = self._head

            self._lastItemPos = None

            self.first()

            def first(self):

            """Resets the cursor to the beginning of the backing store."""

            self._cursor = self._head.next

            self._lastItemPos = None

            def last(self):

            """Moves the cursor to the end of the backing store."""

            self._cursor = self._head.previous

            self._lastItemPos = None

            def hasNext(self):

            """Return True if the iterator has a next item or False otherwise."""

            return self._cursor.next != self._head

            def next(self):

            """Precondition: hasNext returns True.The list has not been modified except by this iterator's mutator.

            Returns the current item and advances the cursor to the next item."""

            if not self.hasNext():

            raise ValueError("No next item in list iterator.")

            if self._modCount != self._backingStore.getModCount():

            raise AttributeError("Illegal modification of backing store.")

            self._lastItemPos = self._cursor

            self._cursor = self._cursor.next

            return self._lastItemPos.data

            def hasPrevious(self):

            """Return True if the iterator has a previous item or False otherwise"""

            return self._cursor.previous != self._head

            def previous(self):

            """Precondition: hasPrevious returns True. The list has not been modified except by this iterator's mutator.

            """

            if not self.hasPrevious():

            raise ValueError("No previous item in list iterator.")

            if self._modCount != self._backingStore.getModCount():

            raise AttributeError("Illegal modification of backing store.")

            self._lastItemPos = self._cursor

            self._cursor = self._cursor.previous

            return self._lastItemPos.data

            def replace(self, item):

            """Precondition: the current position is defined. The list has not been modified except by this iterator's

            mutator."""

            if self._lastItemPos is None:

            raise AttributeError("The current position is undefined.")

            if self._modCount != self._backingStore.getModCount():

            raise AttributeError("Illegal modification of backing store.")

            self._lastItemPos.data = item

            self._lastItemPos = None

            def insert(self, item):

            """Precondition: The list has not been modified except by this iterator's mutator."""

            if self._modCount != self._backingStore.getModCount():

            raise AttributeError("Illegal modification of backing store.")

            if self._lastItemPos is None:

            # Cursor not defined, so add item to end of list.

            self._backingStore.add(item)

            else:

            newNode = TwoWayNode(item, self._lastItemPos.previous, self._lastItemPos)

            self._lastItemPos.previous.next = newNode

            self._lastItemPos.previous = newNode

            self._backingStore.incModCount()

            self._lastItemPos = None

            self._modCount += 1

            def remove(self):

            """Precondition: the current position is defined. The list has not been modified except by this iterator's

            mutator."""

            if self._lastItemPos is None:

            raise AttributeError("The current position is undefined.")

            if self._modCount != self._backingStore.getModCount():

            raise AttributeError("List has been modified illegally.")

            self._lastItemPos.previous.next = self._lastItemPos.next

            self._lastItemPos.next.previous = self._lastItemPos.previous

            self._backingStore.incModCount()

            self._modCount += 1

            self._lastItemPos = None

      • 列表迭代器的时间和空间分析
        • 链表实现,所有的运行方法都是O(1),数组实现的 insert 和 remove 方法都是 O(n)
    1. 开发一个有序的列表
      • 分析
        • 与常规列表的对比
          • 不能使用 insert 和 __setitem__ 方法
          • add 方法会在已有项之间,查找插入该项的一个合适的位置
          • index 操作,会利用列表是有序的这一事实,针对给定项执行一次二叉搜索
      • 设计
        • 为了支持二叉搜索,因此选用基于数组实现的列表
        • ArraySortedList 不能是 ArrayList 的一个子类,因为这样 ArraySortedList 会继承两个不能使用的方法
          • 关系图
        • 使用 ArrayList 的一个实例
          • 使用 ArrayList 的一个实例来使 ArraySortedList 作为类的容器而子类化。但是如此并没有实现代码复用,ArrayList 中的每个方法,在 ArraySortedList 中都必须重新定义
          • 实现策略
        • 将 ArrayList 作为 ArraySortedList 的一个子类
          • 列表实际上是带有两个额外方法( insert 和 __setitem__ )的有序列表,即列表接口是有序列表接口的一个扩展。
          • 实现策略
          • 列表方法分布
            • ArrayList 中的 __contains__ 方法也需要重写
          • 迭代器方法可以使用类似的方法实现
            • ArraySortedListIterator 中包含了所有的导航方法以及 remove 方法。其子类 ArrayListIterator 中则只包含 insert 和 replace 方法
      • 实现
        • ArraySortedList
          • #!/usr/bin/env python

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

            # Author:Lijunjie

            """

            File: arraysortedlist.py

            """

            from arrays import Array

            from abstractlist import AbstractList

            from arraysortedlistiterator import ArraySortedListIterator

            class ArraySortedList(AbstractList):

            """An array-based sorted list implementation."""

            # Class variable

            DEFAULT_CAPACITY = 10

            def __init__(self, sourceCollection=None):

            """Sets the initial state of self, which includes the contents

            of sourceCollection, if it's present."""

            self._items = Array(ArraySortedList.DEFAULT_CAPACITY)

            AbstractList.__init__(self, sourceCollection)

            # Accessor method

            def __iter__(self):

            """Support iteration over a view of self."""

            cursor = 0

            while cursor < len(self):

            yield self._items[cursor]

            cursor += 1

            def __getitem__(self, i):

            """Precondition: 0<= i < len(self)

            Return the item at position i.

            Raises: IndexError"""

            if i < 0 or i >= len(self):

            raise IndexError("List out of range.")

            return self._items[i]

            def __contains__(self, item):

            position = self._binarySearch(item)

            if self._items[position] == item:

            return True

            else:

            return False

            def _binarySearch(self, item):

            """Search item in the array sorted list."""

            left = 0

            right = len(self) - 1

            mid = len(self)

            while left <= right:

            mid = (left + right) // 2

            if self._items[mid] == item:

            break

            if self._items[mid] > item:

            right = mid - 1

            else:

            left = mid + 1

            return mid

            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 = self._binarySearch(item)

            if self._items[position] != item:

            raise ValueError(str(item) + " isn't in the list.")

            # find the first position if there is multiply items

            for i in range(position - 1, -1, -1):

            if self._items[i] != item:

            return i + 1

            # return the first place

            return 0

            # Mutator method

            def pop(self, i=None):

            """Precondition: 0 <= i < len(self).

            Remove and return item at position i. if i is None, i is given a default of len(self) - 1"

            Raise: Index Error."""

            if i is None: i = len(self) - 1

            if i < 0 or i >= len(self):

            raise IndexError("List index out of range.")

            item = self._items[i]

            for index in range(i, len(self) - 1):

            self._items[index] = self._items[index + 1]

            self._items[len(self) - 1] = None

            self._size -= 1

            self.incModCount()

            # Resize array in necessary

            self._shrinkArray()

            return item

            def add(self, item):

            """Add the item to the proper position of the list."""

            # Resize the array if necessary.

            self._growArray()

            # Find the position by binary search

            position = self._binarySearch(item)

            flag = True

            if position < len(self) and self._items[position] != item:

            if item > self._items[position]:

            for i in range(position + 1, len(self)):

            if item <= self._items[i]:

            position = i

            flag = False

            break

            if flag:

            position = len(self)

            else:

            for i in range(position - 1, -1, -1):

            if item >= self._items[i]:

            position = i + 1

            flag = False

            break

            if flag:

            position = 0

            # Insert item at the proper position.

            if position < len(self):

            for index in range(len(self), position, -1):

            self._items[index] = self._items[index - 1]

            self._items[position] = item

            self._size += 1

            self.incModCount()

            def clear(self):

            """Clear the array list."""

            self._items = Array(ArraySortedList.DEFAULT_CAPACITY)

            self._size = 0

            self.incModCount()

            def listIterator(self):

            """Returns a list iterator."""

            return ArraySortedListIterator(self)

            def _growArray(self):

            """Grow the capacity of the array if necessary """

            physicalSize = len(self._items)

            if len(self) >= physicalSize:

            temp = Array(physicalSize * 2)

            index = 0

            for item in self:

            temp[index] = item

            index += 1

            self._items = temp

            def _shrinkArray(self):

            """Shrink the capacity of the array if necessary. """

            physicalSize = len(self._items)

            if len(self) <= physicalSize // 4 and physicalSize >= 2 * ArraySortedList.DEFAULT_CAPACITY:

            temp = Array(physicalSize // 2)

            index = 0

            for item in self:

            temp[index] = item

            index += 1

            self._items = temp

        • ArrayList
          • #!/usr/bin/env python

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

            # Author:Lijunjie

            """

            File: ArrayList.py

            """

            from abstractlist import AbstractList

            from arraysortedlist import ArraySortedList

            from arraylistiterator import ArrayListIterator

            class ArrayList(ArraySortedList):

            """An array-based list implementation."""

            def __init__(self, sourceCollection=None):

            """Sets the initial state of self, which includes the contents

            of sourceCollection, if it's present."""

            ArraySortedList.__init__(self, sourceCollection)

            # Accessor method

            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"""

            AbstractList.index(self, item)

            def __contains__(self, item):

            for i in self:

            if i == item:

            return True

            return False

            # Mutator method

            def __setitem__(self, i, item):

            """Precondition: 0 <= i < len(self)

            Replace the item at position i.

            Raise: IndexError"""

            if i < 0 or i >= len(self):

            raise IndexError("List out of range.")

            self._items[i] = item

            def insert(self, i, item):

            """Inserts item at the position i."""

            # Resize the array if necessary

            self._growArray()

            # Set the position i between 0 and len(self)

            if i < 0: i = 0

            elif i > len(self): i = len(self)

            if i < len(self):

            for index in range(len(self), i, -1):

            self._items[index] = self._items[index - 1]

            self._items[i] = item

            self._size += 1

            self.incModCount()

            def add(self, item):

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

            AbstractList.add(self, item)

            def listIterator(self):

            """Returns a list iterator."""

            return ArrayListIterator(self)

    • ArraySortedListIterator
      • #!/usr/bin/env python

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

        # Author:Lijunjie

        """

        File:arraysortedlistiterator.py

        """

        class ArraySortedListIterator(object):

        """Represents the list iterator for a sorted array list."""

        def __init__(self, backingStore):

        """Set the initial state of the list iterator."""

        self._backingStore = backingStore

        self._modCount = backingStore.getModCount()

        self._cursor = 0

        self._lastItemPos = -1

        self.first()

        def first(self):

        """Resets the cursor to the beginning of the backing store."""

        self._cursor = 0

        self._lastItemPos = -1

        def last(self):

        """Moves the cursor to the end of the backing store."""

        self._cursor = len(self._backingStore)

        self._lastItemPos = -1

        def hasNext(self):

        """Return True if the iterator has a next item or False otherwise."""

        return self._cursor < len(self._backingStore)

        def next(self):

        """Precondition: hasNext returns True.The list has not been modified except by this iterator's mutator.

        Returns the current item and advances the cursor to the next item."""

        if not self.hasNext():

        raise ValueError("No next item in list iterator.")

        if self._modCount != self._backingStore.getModCount():

        raise AttributeError("Illegal modification of backing store.")

        self._lastItemPos = self._cursor

        self._cursor += 1

        return self._backingStore[self._lastItemPos]

        def hasPrevious(self):

        """Return True if the iterator has a previous item or False otherwise"""

        return self._cursor > 0

        def previous(self):

        """Precondition: hasPrevious returns True. The list has not been modified except by this iterator's mutator.

        """

        if not self.hasPrevious():

        raise ValueError("No previous item in list iterator.")

        if self._modCount != self._backingStore.getModCount():

        raise AttributeError("Illegal modification of backing store.")

        self._cursor -= 1

        self._lastItemPos = self._cursor

        return self._backingStore[self._lastItemPos]

        def remove(self):

        """Precondition: the current position is defined. The list has not been modified except by this iterator's

        mutator."""

        if self._lastItemPos == -1:

        raise AttributeError("The current position is undefined.")

        if self._modCount != self._backingStore.getModCount():

        raise AttributeError("List has been modified illegally.")

        self._backingStore.pop(self._lastItemPos)

        # If the item removed was obtained via next, move cursor back

        if self._lastItemPos < self._cursor:

        self._cursor -= 1

        self._modCount += 1

        self._lastItemPos = -1

    • ArrayListIteraotr
      • #!/usr/bin/env python

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

        # Author:Lijunjie

        """

        File:arraylistiterator.py

        """

        from arraysortedlistiterator import ArraySortedListIterator

        class ArrayListIterator(ArraySortedListIterator):

        """Represents the list iterator for an array list."""

        def __init__(self, backingStore):

        """Set the initial state of the list iterator."""

        ArraySortedListIterator.__init__(self, backingStore)

        def replace(self, item):

        """Precondition: the current position is defined. The list has not been modified except by this iterator's

        mutator."""

        if self._lastItemPos == -1:

        raise AttributeError("The current position is undefined.")

        if self._modCount != self._backingStore.getModCount():

        raise AttributeError("Illegal modification of backing store.")

        self._backingStore[self._lastItemPos] = item

        self._lastItemPos = -1

        def insert(self, item):

        """Precondition: The list has not been modified except by this iterator's mutator."""

        if self._modCount != self._backingStore.getModCount():

        raise AttributeError("Illegal modification of backing store.")

        if self._lastItemPos == -1:

        # Cursor not defined, so add item to end of list.

        self._backingStore.add(item)

        else:

        self._backingStore.insert(self._lastItemPos, item)

        # If the item insert was obtained via next, move cursor forward

        if self._lastItemPos < self._cursor:

        self._cursor += 1

        self._lastItemPos = -1

        self._modCount += 1

数据结构( Pyhon 语言描述 ) — —第9章:列表的更多相关文章

  1. 数据结构( Pyhon 语言描述 ) — —第10章:树

    树的概览 树是层级式的集合 树中最顶端的节点叫做根 个或多个后继(子节点). 没有子节点的节点叫做叶子节点 拥有子节点的节点叫做内部节点 ,其子节点位于层级1,依次类推.一个空树的层级为 -1 树的术 ...

  2. 数据结构( Pyhon 语言描述 ) — — 第7章:栈

    栈概览 栈是线性集合,遵从后进先出原则( Last - in first - out , LIFO )原则 栈常用的操作包括压入( push ) 和弹出( pop ) 栈的应用 将中缀表达式转换为后缀 ...

  3. 数据结构( Pyhon 语言描述 ) — — 第5章:接口、实现和多态

    接口 接口是软件资源用户可用的一组操作 接口中的内容是函数头和方法头,以及它们的文档 设计良好的软件系统会将接口与其实现分隔开来 多态 多态是在两个或多个类的实现中使用相同的运算符号.函数名或方法.多 ...

  4. 数据结构( Pyhon 语言描述 ) — — 第3章:搜索、排序和复杂度分析

    评估算法的性能 评价标准 正确性 可读性和易维护性 运行时间性能 空间性能(内存) 度量算法的运行时间 示例 """ Print the running times fo ...

  5. 数据结构( Pyhon 语言描述 ) — — 第2章:集合概览

    集合类型 定义 个或多个其他对象的对象.集合拥有访问对象.插入对象.删除对象.确定集合大小以及遍历或访问集合的对象的操作 分类 根据组织方式进行 线性集合 线性集合按照位置排列其项,除了第一项,每一项 ...

  6. 数据结构( Pyhon 语言描述 ) — — 第4章:数据和链表结构

    数据结构是表示一个集合中包含的数据的一个对象 数组数据结构 数组是一个数据结构 支持按照位置对某一项的随机访问,且这种访问的时间是常数 在创建数组时,给定了用于存储数据的位置的一个数目,并且数组的长度 ...

  7. 数据结构( Pyhon 语言描述 ) — —第11章:集和字典

    使用集 集是没有特定顺序的项的一个集合,集中的项中唯一的 集上可以执行的操作 返回集中项的数目 测试集是否为空 向集中添加一项 从集中删除一项 测试给定的项是否在集中 获取两个集的并集 获取两个集的交 ...

  8. 数据结构( Pyhon 语言描述 ) — — 第1章:Python编程基础

    变量和赋值语句 在同一条赋值语句中可以引入多个变量 交换变量a 和b 的值 a,b = b,a Python换行可以使用转义字符\,下一行的缩进量相同 )\ 帮助文档 help() 控制语句 条件式语 ...

  9. 数据结构( Pyhon 语言描述 ) — — 第8章:队列

    队列概览 队列是线性的集合 队列的插入限制在队尾,删除限制在队头.支持先进先出协议( FIFIO, first-in first-out ) 两个基本操作 add:在队尾添加一项 pop:从队头弹出一 ...

随机推荐

  1. 洛谷 P2623 物品选取

    https://www.luogu.org/problemnew/show/P2623 https://www.luogu.org/blog/test-1/solution-p2623 重点就是甲类物 ...

  2. HTML的基础结构

    <html>内容</html> 解释:HTML文档的文档标记,也称为HTML开始标记 功能:这对标记分别位于网页的最前端和最后端 <head>内容</head ...

  3. Incorrect configuration: namenode address dfs.namenode.servicerpc-address or dfs.namenode.rpc-address is not configured.

    在搭建Hadoop集群的时候,遇到了这样的一个报错. 就在启动HDFS调用命令: start-dfs.sh 的时候,报错: 然后输密码就一直很奇怪,反正一直运行不成功. 百度了半天,确定是core-s ...

  4. python flask学习(1)与Git基础操作

    今天从简单的flask开始完成Flask web开发的学习.今天学习了Git和GitHub项目的提交. Git尝试提交过程中出现了"Could not read from remote re ...

  5. Oracle 恢复数据后,数据库中中文变成问号解决方法

    1.右击---我的电脑---环境变量 2.新增环境变量 变量名:LANG=zh_CN.GBK NLS_LANG=SIMPLIFIED CHINESE_CHINA.ZHS16GBK 3.重启PLSQL或 ...

  6. if __FILE__ == $0 end

    if __FILE__ == $0 end __FILE__是一个“具有魔力”的变量,它代表了当前文件名.$0是用于启动程序的文件名.那么代码“if __FILE__ == $0”便意味着检查此文件是 ...

  7. PaaS优点与限制(3)

    PaaS优点与限制(3) PaaS学习笔记目录 PaaS基础学习(1) 在PaaS上开发Web.移动应用(2) PaaS优点与限制(3) 13. PaaS的核心服务 核心服务是指提供数据存储.SQl. ...

  8. Objective-C Runtime Reference

    This document describes the OS X Objective-C 2.0 runtime library support functions and data structur ...

  9. 小tip: 使用CSS将图片转换成黑白(灰色、置灰)[转]

        小tip: 使用CSS将图片转换成黑白(灰色.置灰) 这篇文章发布于 2012年08月19日,星期日,20:41,归类于 css相关, SVG相关. 阅读 159943 次, 今日 146 次 ...

  10. Oracle 存储过程调用语句

    #oracle 存储过程调用语句declare v_custName varchar2(10); --客户姓名 v_num number; --订单分布天数 v_time number; --每日订单 ...