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

    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

            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)

      • 链表实现
        • 采用包含一个哨兵节点的双链表结构
          • 示意图
          • 优点
            • 不需要一个单独的尾指针
            • 当插入或删除第一个或最后一个节点的时候,不需要重新设置头指针
        • _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 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 self._items[position] is not None and 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 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

原文地址:https://www.cnblogs.com/lijunjie9502/p/9938654.html

时间: 2024-07-29 05:47:04

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

数据结构( Pyhon 语言描述 ) &mdash; &mdash;第11章:集和字典

使用集 集是没有特定顺序的项的一个集合,集中的项中唯一的 集上可以执行的操作 返回集中项的数目 测试集是否为空 向集中添加一项 从集中删除一项 测试给定的项是否在集中 获取两个集的并集 获取两个集的交集 获取两个集的差集 判断一个集是否是另一个集的子集 集上的差集和子集操作是不对称的 Python 中的 set 类 set 类中常用的方法 使用示例 >>> A = set([0,1,2]) >>> B = set() >>> 1 in A True &

数据结构与算法分析(C语言描述)第二章 算法分析【总结】

重点:大O记法,最大子序列和(4种算法),对数级算法(3个例子:对分查找.欧几里德算法.幂运算) 算法 算法(algorithm)是为求解一个问题需要遵循的.被清楚地指定的简单指令的集合. 数学基础 四个定义: 1.大O表示法:如果存在正常数 c 和 n0 使得当 N ≥ n0时,T(N) ≤ cf(N),则记为T(N) = O(f(N)). (描述了T(N)的相对增长率小于等于f(N)的相对增长率.) 2.大Ω表示法:如果存在正常数 c 和 n0 使得当 N ≥ n0时,T(N) ≥ cf(N

数据结构_Python语言描述(英)pdf

下载地址:网盘下载 <Python自然语言处理(影印版)>提供了非常易学的自然语言处理入门介绍,该领域涵盖从文本和电子邮件预测过滤,到自动总结和翻译等多种语言处理技术.在<Python自然语言处理(影印版)>中,你将学会编写Python程序处理大量非结构化文本.你还将通过使用综合语言数据结构访问含有丰富注释的数据集,理解用于分析书面通信内容和结构的主要算法. <Python自然语言处理>准备了充足的示例和练习,可以帮助你: 从非结构化文本中抽取信息,甚至猜测主题或识别&

数据结构(java语言描述)递归实现——汉诺塔问题

1.汉诺塔问题描述 N阶汉诺塔:假设有3个分别命名为x,y,z的三个塔座,在x上有n个盘子,直径大小不同,有小到大按标号1,2,3...n排列,要借助y将n个盘子转移到z上,期间不能让小盘子压在大盘子上.规则: 每次至移动一个盘子: 盘子可以插在x,y,z任意一个塔座上: 任何时候都不能将大盘压在小盘上. 2.解题思路 当n=1时,直接把盘子由x——>z; 当n>1时,需利用y,首先将(n-1)个盘子由x——>y,把第n个实现x——>z,然后把问题转换为实现(n-1)个盘子由y——

数据结构(java语言描述)哈夫曼编码

原理:哈夫曼编码是根据将已给出的权值作为叶子结点,生成一颗哈夫曼树,然后使得权重最小. 首先生成已给权重的所有的叶子结点,然后取所有节点中最小和次小的结点作为左右孩子生成一个哈夫曼树,计算出父节点的权重放入给出的权重森林中,并把之前的最小和次小的结点从森林中删除,再在种种森林中找最小和次小的结点生成权重树....直到最终只剩下一个树为止. 哈夫曼树的结点用如下结点表示: (有权重,左右孩子,父节点,然后设置一个标识符标志结点是否已经放入哈夫曼树) package tree;/**********

数据结构(java语言描述)链栈的定义

1.定义栈接口 package stack;public interface Istack {    public void clear();    public boolean isEmpty();    public int length();    public Object peek();    public void push(Object x) throws Exception;    public Object pop();} 2.定义Node结点 package stack; i

数据结构(java语言描述)顺序栈的使用(两个大数相加)

利用http://www.cnblogs.com/xleer/p/5289661.html中对顺序栈以及栈的进本方法的定义,实现超过整数上限的两个数的加法计算. 算法: package stack;/********************************************************************** * @author sch ********利用栈,计算两个大数的和.大数的值超过int存储的范围******************************

数据结构c++语言描述&mdash;&mdash;最大堆(MaxHeap)

一.最大堆的插入 图9-3a 给出了一个具有5个元素的最大堆.由于堆是完全二叉树,当加入一个元素形成6元素堆时,其结构必如9-3b 所示.如果插入元素的值为1,则插入后该元素成为2的左孩子,相反,若新元素的值为5,则该元素不能成为2的左孩子(否则将改变最大树的特性),应把2下移为左孩子(如图9 - 3 c所示),同时还得决定在最大堆中5是否占据2原来的位置.由于父元素20大于等于新插入的元素5,因此可以在原2所在位置插入新的元素.假设新元素的值为21而不是5,这时,同图9-3c 一样,把2下移为

《数据结构与算法分析:C语言描述》复习——第四章“树”——AVL树

2014.06.15 16:22 简介: AVL树是一种高度平衡的二叉搜索树,其命名源自于联合发明算法的三位科学家的名字的首字母.此处“平衡”的定义是:任意节点的左右子树的高度相差不超过1.有了这个平衡的性质,使得AVL树的高度H总是接近log(N),因此各种增删改查的操作的复杂度能够保证在对数级别.没有bad case是AVL树与普通的二叉搜索树的最大区别.为了实现平衡性质,我们需要记录每个节点的高度(或者平衡因子)来检测不平衡的情况.为了修正高度不平衡,需要用到“旋转”的方法,分为单旋转和双