棧,隊列的特性:
1.棧(stacks)是一種隻能通過通路其一端來實作資料存儲與檢索的線性資料結構,具有後進先出(last in first out,LIFO)的特征
2.隊列(queue)是一種具有先進先出特征的線性資料結構,元素的增加隻能在一端進行,元素的删除隻能在另一端進行。能夠增加元素的隊列一端稱為隊尾,可以删除元素的隊列一端則稱為隊首。
位址在 http://docs.python.org/2/tutorial/datastructures.html#more-on-lists ,下面的官方的代碼。
關于棧
>>> 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種結構。
