python中實(shí)現(xiàn)棧的三種方法
棧是一種線性數(shù)據(jù)結(jié)構(gòu),用先進(jìn)后出或者是后進(jìn)先出的方式存儲數(shù)據(jù),棧中數(shù)據(jù)的插入刪除操作都是在棧頂端進(jìn)行,常見棧的函數(shù)操作包括
- empty() – 返回棧是否為空 – Time Complexity : O(1)
- size() – 返回棧的長度 – Time Complexity : O(1)
- top() – 查看棧頂元素 – Time Complexity : O(1)
- push(g) – 向棧頂添加元素 – Time Complexity : O(1)
- pop() – 刪除棧頂元素 – Time Complexity : O(1)
python中??梢杂靡韵氯N方法實(shí)現(xiàn):
1)list
2)collections.deque
3)queue.LifoQueue
使用列表實(shí)現(xiàn)棧
python的內(nèi)置數(shù)據(jù)結(jié)構(gòu)list可以用來實(shí)現(xiàn)棧,用append()向棧頂添加元素, pop() 可以以后進(jìn)先出的順序刪除元素
但是列表本身有一些缺點(diǎn),主要問題就是當(dāng)列表不斷擴(kuò)大的時候會遇到速度瓶頸.列表是動態(tài)數(shù)組,因此往其中添加新元素而沒有空間保存新的元素時,它會自動重新分配內(nèi)存塊,并將原來的內(nèi)存中的值復(fù)制到新的內(nèi)存塊中.這就導(dǎo)致了一些append()操作會消耗更多的時間
>>> stack = [] >>> #append() fuction to push ... #element in list ... >>> stack.append('hello') >>> stack.append('world') >>> stack.append('!') >>> print('Initial stack') Initial stack >>> print(stack) ['hello', 'world', '!'] >>> #pop() function to pop element ... #from stack in LIFO order ... >>> print('\nElement poped from stack') Element poped from stack >>> print(stack.pop()) ! >>> print(stack.pop()) world >>> print(stack.pop()) hello >>> print('\nStack after all elements are poped') Stack after all elements are poped >>> print(stack) []
使用collections.deque實(shí)現(xiàn)棧
python中棧也可以用deque類實(shí)現(xiàn),當(dāng)我們想要在實(shí)現(xiàn)在容器兩端更快速地進(jìn)行append和pop操作時,deque比列表更合適.deque可以提供O(1)時間的append和pop操作,而列表則需要O(n)時間.
>>> from collections import deque >>> stack = deque() >>> # append() fuction to push ... #element in list ... >>> stack.append('hello') >>> stack.append('world') >>> stack.append('!') >>> print('Initial stack') Initial stack >>> print(stack) deque(['hello', 'world', '!']) >>> #pop() function to pop element ... #from stack in LIFO order ... >>> print('\nElement poped from stack') Element poped from stack >>> print(stack.pop()) ! >>> print(stack.pop()) world >>> print(stack.pop()) hello >>> print('\nStack after all elements are poped') Stack after all elements are poped >>> print(stack)deque([])
使用queue module實(shí)現(xiàn)棧
Queue模塊有LIFO queue,也就是棧結(jié)構(gòu).用put()和get()操作從Queue中添加和獲得數(shù)據(jù)
>>> from queue import LifoQueue >>> stack = LifoQueue(maxsize = 3) >>> print(stack.qsize()) 0 >>> stack.put('hello') >>> stack.put('world') >>> stack.put('!') >>> print('\nElement poped from stack') Element poped from stack >>> print(stack.get()) ! >>> print(stack.get()) world >>> print(stack.get()) hello >>> print('\nEmpty:', stack.empty()) Empty: True
以上就是python中實(shí)現(xiàn)棧的三種方法的詳細(xì)內(nèi)容,更多關(guān)于python 實(shí)現(xiàn)棧的資料請關(guān)注腳本之家其它相關(guān)文章!
- python防止棧溢出的實(shí)例講解
- 詳解python數(shù)據(jù)結(jié)構(gòu)之棧stack
- python全棧開發(fā)語法總結(jié)
- Python可以實(shí)現(xiàn)棧的結(jié)構(gòu)嗎
- Python捕獲異常堆棧信息的幾種方法(小結(jié))
- 使用Python將Exception異常錯誤堆棧信息寫入日志文件
- Python實(shí)現(xiàn)棧的方法詳解【基于數(shù)組和單鏈表兩種方法】
- Python棧的實(shí)現(xiàn)方法示例【列表、單鏈表】
- python實(shí)現(xiàn)異常信息堆棧輸出到日志文件
- Python中棧的詳細(xì)介紹
相關(guān)文章
python實(shí)現(xiàn)flappy bird游戲
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)flappy bird游戲,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12利用python+ffmpeg合并B站視頻及格式轉(zhuǎn)換的實(shí)例代碼
這篇文章主要介紹了利用python+ffmpeg合并B站視頻及格式轉(zhuǎn)換的實(shí)例代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11NumPy中np.c_ 和 np.r_ 的區(qū)別小結(jié)
np.c_和?np.r_是NumPy庫中兩個非常有用的函數(shù),它們分別用于按列和按行拼接數(shù)組本文主要介紹了NumPy中np.c_ 和 np.r_ 的區(qū)別小結(jié),具有一定的參考價值,感興趣的可以了解一下2024-02-02Python+matplotlib實(shí)現(xiàn)填充螺旋實(shí)例
這篇文章主要介紹了Python+matplotlib實(shí)現(xiàn)填充螺旋實(shí)例,具有一定借鑒價值,需要的朋友可以參考下2018-01-01pygame學(xué)習(xí)筆記(6):完成一個簡單的游戲
這篇文章主要介紹了pygame學(xué)習(xí)筆記(6):完成一個簡單的游戲,本文綜合了學(xué)習(xí)過的知識,完成一個簡單的游戲開發(fā),是本系列文章的最后一篇,需要的朋友可以參考下2015-04-04