python數據結構之棧與隊列

python數據結構之棧與隊列

用list實現堆棧stack

堆棧:後進先出
如何進?用append
如何出?用pop()html

>>>
>>> stack = [3, 4, 5]
>>> stack.append(6)
>>> stack.append(7)
>>> stack
[3, 4, 5, 6, 7]
>>> stack.pop()
7
>>> stack
[3, 4, 5, 6]
>>> stack.pop()
6
>>> stack.pop()
5
>>> stack
[3, 4]

用list實現隊列queue

隊列:先進先出
如何進?用append
如何出?用popleft()python

>>>
>>> from collections import deque
>>> queue = deque(["Eric", "John", "Michael"])
>>> queue.append("Terry")           # Terry arrives
>>> queue.append("Graham")          # Graham arrives
>>> queue.popleft()                 # The first to arrive now leaves
'Eric'
>>> queue.popleft()                 # The second to arrive now leaves
'John'
>>> queue                           # Remaining queue in order of arrival
deque(['Michael', 'Terry', 'Graham'])

自定義堆棧

棧是限定僅在表尾進行插入或刪除操做的線性表。對於棧來講,表尾稱爲棧頂,表頭稱爲棧底。不含元素的空表稱爲空棧。數據結構

堆棧ADT(抽象數據類型)通常提供如下接口:app

函數 說明
Stack() 建立堆棧
push(item) 向棧頂插入項
pop() 刪除棧頂的項
clear() 清空堆棧
is_empty() 判斷堆棧是否爲空
size() 返回堆棧中項的個數
top() 返回棧頂的項
print() 打印堆棧

class Stack(object):
    """堆棧"""
    def __init__(self, item = []):
        self.item = []
        if len(item):
            for i in item:
                self.item.append(i)

    def push(self, item):
            self.item.append(item)

    def clear(self):
        del self.item

    def is_empty(self):
        return self.size() == 0

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

    def print(self):
        print(self.item)

    def top(self):
        return self.item[-1]

    def pop(self):
        data = self.top()
        self.item.pop()
        return data

print("建立堆棧")
stack =  Stack([1,2,3])
stack.print()
print("向棧頂插入元素")
stack.push(4)
stack.print()
print("判斷堆棧是否爲空")
print(stack.is_empty())
print("返回堆棧中項的個數")
print(stack.size())
print("返回棧頂的項")
print(stack.top())
print("刪除棧頂的項")
stack.pop()
stack.print()
print("清空堆棧")
print(stack.clear())

程序輸出:函數

建立堆棧
[1, 2, 3]
向棧頂插入元素
[1, 2, 3, 4]
判斷堆棧是否爲空
False
返回堆棧中項的個數
4
返回棧頂的項
4
刪除棧頂的項
[1, 2, 3]
清空堆棧
None

自定義隊列

隊列是一種先進先出的線性表。它只容許在表的一端進行插入,在另外一端刪除元素。code

隊列ADT(抽象數據類型)通常提供如下接口:htm

函數 說明
Queue() 建立隊列
enqueue(item) 向隊列插入元素
dequeue() 刪除隊列的元素
clear() 清空隊列
is_empty() 判斷隊列是否爲空
size() 返回隊列中項的個數
print() 打印隊列
class Queue(object):
    """模擬隊列"""
    def __init__(self, item = []):
        self.item = []
        if len(item):
            for i in item:
                self.item.append(i)

    def enqueue(self, item):
        self.item.append(item)

    def dequeue(self):
        self.item.pop(0)

    def clear(self):
        del self.item

    def is_empty(self):
        return self.size() == 0

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

    def print(self):
        print(self.item)

print("建立隊列")
queue =  Queue([1,2,3])
queue.print()
print("向隊列插入元素")
queue.enqueue(4)
queue.print()
print("從隊列中刪除元素")
queue.dequeue()
queue.print()
print("判斷隊列是否爲空")
print(queue.is_empty())
print("返回隊列中項的個數")
print(queue.size())
queue.print()
print("清空隊列")
print(queue.clear())

程序輸出結果:接口

建立隊列
[1, 2, 3]
向隊列插入元素
[1, 2, 3, 4]
從隊列中刪除元素
[2, 3, 4]
判斷隊列是否爲空
False
返回隊列中項的個數
3
[2, 3, 4]
清空隊列
None

參考

《數據結構》嚴蔚敏
https://docs.python.org/3.6/tutorial/datastructures.html#more-on-lists隊列

相關文章
相關標籤/搜索