python线性数据结构

1.栈(Stack)(后进先出)

栈的实现:

class Stack:
    def __init__(self):
        self.items = []
    def isEmpty(self):
        return self.items == []
    def push(self,item):
        return self.items.append(item)
    def pop(self):
        return self.items.pop()
    def peek(self):
        return self.items[-1]
    def size(self):
        return len(self.items)

2.队列(Queue)(先进先出)

队列的实现:

class Queue:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
#入队,从0
def enquque(self,item):
return self.items.insert(0,item)
#出队,从尾
def dequeue(self):
return self.items.pop()

3.双端队列(Dqueue)(从首段和尾端任一端添加和移除) 

双端队列实现:

class Deque:
    def __init__(self):
        self.items = []

    def isEmpty(self):
        return self.items == []

    def addFront(self, item):
        self.items.append(item)

    def addRear(self, item):
        self.items.insert(0,item)

    def removeFront(self):
        return self.items.pop()

    def removeRear(self):
        return self.items.pop(0)

    def size(self):
        return len(self.items)

  

posted @ 2019-12-13 14:10  奥布莱恩  阅读(454)  评论(0编辑  收藏  举报