python數據結構之棧、隊列的實現

這個在官網中list支持,有實現。html

補充一下棧,隊列的特性:python

1.棧(stacks)是一種只能經過訪問其一端來實現數據存儲與檢索的線性數據結構,具備後進先出(last in first out,LIFO)的特徵數據結構

2.隊列(queue)是一種具備先進先出特徵的線性數據結構,元素的增長只能在一端進行,元素的刪除只能在另外一端進行。可以增長元素的隊列一端稱爲隊尾,能夠刪除元素的隊列一端則稱爲隊首。app

地址在 http://docs.python.org/2/tutorial/datastructures.html#more-on-lists ,下面的官方的代碼。spa

關於棧
>>>
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]

關於隊列
>>> 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'])

上面代碼很清晰的解釋了上面的2種結構
相關文章
相關標籤/搜索