数据结构:线性数据结构(4)-列表(栈,队列,deques, 列表)

版权声明:作者:Rookiekk 联系邮箱:[email protected] 欢迎转载或分享,但请务必声明文章出处。 https://blog.csdn.net/qq_18888869/article/details/88138785

一、列表

1.1列表的抽象数据类型

列表是项的集合,其中每个项保持相对于其他项的相对位置。无序列表的结构是项的集合,其中每个项保持相对于其他项的相对位置。下面给出了一些可能的无序列表操作。

  • List() 创建一个新的空列表。它不需要参数,并返回一个空列表。
  • add(item) 向列表中添加一个新项。它需要 item 作为参数,并不返回任何内容。假定该 item 不在列表中。
  • remove(item) 从列表中删除该项。它需要 item 作为参数并修改列表。假设项存在于列表中。
  • search(item) 搜索列表中的项目。它需要 item 作为参数,并返回一个布尔值。
  • isEmpty() 检查列表是否为空。它不需要参数,并返回布尔值。
  • size()返回列表中的项数。它不需要参数,并返回一个整数。
  • append(item) 将一个新项添加到列表的末尾,使其成为集合中的最后一项。它需要 item 作为参数,并不返回任何内容。假定该项不在列表中。
  • index(item) 返回项在列表中的位置。它需要 item 作为参数并返回索引。假定该项在列表中。
  • insert(pos,item) 在位置 pos 处向列表中添加一个新项。它需要 item 作为参数并不返回任何内容。假设该项不在列表中,并且有足够的现有项使其有 pos 的位置。
  • pop() 删除并返回列表中的最后一个项。假设该列表至少有一个项。
  • pop(pos) 删除并返回位置 pos 处的项。它需要 pos 作为参数并返回项。假定该项在列表中。

1.2有序列表的抽象数据类型

有序列表的列表类型。例如,如果上面所示的整数列表是有序列表(升序),则它可以写为 17,26,31,54,77和93。由于 17 是最小项,它占据第一位置。同样,由于 93 是最大的,它占据最后的位置。

有序列表的结构是项的集合,其中每个项保存基于项的一些潜在特性的相对位置。排序通常是升序或降序,并且我们假设列表项具有已经定义的有意义的比较运算。许多有序列表操作与无序列表的操作相同。

  • OrderedList() 创建一个新的空列表。它不需要参数,并返回一个空列表。
  • add(item) 向列表中添加一个新项。它需要 item 作为参数,并不返回任何内容。假定该 item 不在列表中。
  • remove(item) 从列表中删除该项。它需要 item 作为参数并修改列表。假设项存在于列表中。
  • search(item) 搜索列表中的项目。它需要 item 作为参数,并返回一个布尔值。
  • isEmpty() 检查列表是否为空。它不需要参数,并返回布尔值。
  • size()返回列表中的项数。它不需要参数,并返回一个整数。
  • index(item) 返回项在列表中的位置。它需要 item 作为参数并返回索引。假定该项在列表中。
  • pop() 删除并返回列表中的最后一个项。假设该列表至少有一个项。
  • pop(pos) 删除并返回位置 pos 处的项。它需要 pos 作为参数并返回项。假定该项在列表中。

2.列表的python实现

2.1实现无序列表:链表

#链表
class Node(object):
    def __init__(self, data, nextNode = None ):
        self.data = data
        self.nextNode = nextNode
    
    def getData(self):
        return self.data
    
    def setData(self, newdata):
        self.data = newdata

    def getNext(self):
        return self.nextNode
    
    def setNext(self, newNext):
        self.nextNode = newNext
        
class unorderedList(object):
    def __init__(self, head = None):
        self.head = head
        
    def isEmpty(self):
        return self.head == None
    
    def add(self, data):
        newNode = Node(data)
        newNode.setNext(self.head)
        self.head = newNode
    
    def size(self) :
        current = self.head
        count = 0 
        while current != None:
            count += 1
            current = current.getNext ()   #
        return count
    
    def search(self, newdata):
        current = self.head
        found = False
        while not found and current != None:
            data = current.getData()
            if newdata == data:
                found = True
            else:
                current = current.getNext()
                
        return found
    
    def remove(self, newdata):
        current = self.head
        previous = None
        found = False
        while not found  :
            data = current.getData()
            if data == newdata:
                found = True
            else:
                previous = current
                current = current.getNext()
        if previous == None:
            self.head = current.getNext()
        else :
            previous.setNext(current.getNext())

2.2 有序链表

有区别的方法 add(),search()。

#有序列表
def orderdedList(object):
    def __init__(self, head = None):
        self.head = head
    
    def search(self,item):
        current = self.head
        found = False
        stop = False
        while current != None and not found and not stop:
            if current.getData() == item:
                found = True
            else:
                if current.getData() > item:
                    stop = True
                else:
                    current = current.getNext()

    return found
    
    def add(self,item):
        current = self.head
        previous = None
        stop = False
        while current != None and not stop:
            if current.getData() > item:
                stop = True
            else:
                previous = current
                current = current.getNext()

        temp = Node(item)
        if previous == None:
            temp.setNext(self.head)
            self.head = temp
        else:
            temp.setNext(current)
            previous.setNext(temp)

其他数据结构原理介绍实现:

栈:https://blog.csdn.net/qq_18888869/article/details/88086002

队列:https://blog.csdn.net/qq_18888869/article/details/88134592

deque:https://blog.csdn.net/qq_18888869/article/details/88137237

github代码:https://github.com/makang101/python-data-structure

猜你喜欢

转载自blog.csdn.net/qq_18888869/article/details/88138785