Python棧的實(shí)現(xiàn)方法示例【列表、單鏈表】
本文實(shí)例講述了Python棧的實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
Python實(shí)現(xiàn)棧
- 棧的數(shù)組實(shí)現(xiàn):利用python列表方法
代碼如下:
# 列表實(shí)現(xiàn)棧,利用python列表方法 class listStack(object): def __init__(self): self.items = [] def is_empty(self): return self.items == 0 def size(self): return len(self.items) def top(self): return self.items[len(self.items)-1] def push(self, value): return self.items.append(value) def pop(self): return self.items.pop() if __name__ =="__main__": stack = listStack() stack.push("welcome") stack.push("www") stack.push("jb51") stack.push("net") print "棧的長度:", stack.size() print "\n".join(['%s:%s' % item for item in stack.__dict__.items()]) #打印棧stack所有元素 print "出棧:",stack.pop() print "出棧:",stack.pop() print "出棧:",stack.pop()
運(yùn)行結(jié)果:
棧的長度: 4
items:['welcome', 'www', 'jb51', 'net']
出棧: net
出棧: jb51
出棧: www
- 棧的鏈表實(shí)現(xiàn):
棧的鏈表實(shí)現(xiàn)中,壓棧(push)類似于在單鏈表中表頭添加節(jié)點(diǎn);出棧(pop)類似于鏈表中表頭刪除節(jié)點(diǎn)并返回對(duì)應(yīng)節(jié)點(diǎn)值;棧頂元素(top)就是獲取鏈表中的第一個(gè)元素
鏈表節(jié)點(diǎn)的定義直接嵌套在鏈表?xiàng)n愔?/p>
代碼如下:
# 鏈表實(shí)現(xiàn)棧 class linkedStack(object): class Node(object): def __init__(self, value=None, next=None): self.value = value self.next = next def __init__(self): self.top = None self.length = 0 def is_empty(self): return self.length == 0 def size(self): return self.length # 獲取棧頂元素 def get(self): if self.is_empty(): raise Exception("Stack is empty!") return self.top.value # 壓棧 def push(self, value): node = self.Node(value) old_top = self.top self.top = node node.next = old_top self.length += 1 # 出棧 def pop(self): if self.length == 0: raise Exception("Stack is empty!") item = self.top.value curnode = self.top.next self.top.next = self.top self.top = curnode self.length -= 1 return item if __name__ =="__main__": stack = linkedStack() stack.push("welcome") stack.push("www") stack.push("jb51") stack.push("net") print "棧的長度:", stack.size() print "出棧:",stack.pop() print "出棧:",stack.pop() print "出棧:",stack.pop() print "出棧:",stack.pop()
運(yùn)行結(jié)果:
棧的長度: 4
出棧: net
出棧: jb51
出棧: www
出棧: welcome
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python加密解密算法與技巧總結(jié)》、《Python編碼操作技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- python數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)之實(shí)現(xiàn)線性表的順序
- python數(shù)據(jù)結(jié)構(gòu)之線性表的順序存儲(chǔ)結(jié)構(gòu)
- python實(shí)現(xiàn)單鏈表的方法示例
- python如何實(shí)現(xiàn)單鏈表的反轉(zhuǎn)
- Python單鏈表原理與實(shí)現(xiàn)方法詳解
- Python實(shí)現(xiàn)棧的方法詳解【基于數(shù)組和單鏈表兩種方法】
- python實(shí)現(xiàn)從尾到頭打印單鏈表操作示例
- 用python介紹4種常用的單鏈表翻轉(zhuǎn)的方法小結(jié)
- python版單鏈表反轉(zhuǎn)
- Python線性表種的單鏈表詳解
相關(guān)文章
python調(diào)用API接口實(shí)現(xiàn)登陸短信驗(yàn)證
在本篇文章里小編給大家整理的是關(guān)于python調(diào)用API接口實(shí)現(xiàn)登陸短信驗(yàn)證的實(shí)例內(nèi)容,需要的朋友們可以參考下。2020-05-05利用Python構(gòu)建Flutter應(yīng)用的教程詳解
Flutter在軟件研發(fā)領(lǐng)域是非常流行的,今天就讓我們深入了解一下,用?Python構(gòu)建flutter應(yīng)用程序的世界,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12Python函數(shù)關(guān)鍵字參數(shù)及用法詳解
本文主要介紹了Python函數(shù)關(guān)鍵字參數(shù)及用法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03django 通過ajax完成郵箱用戶注冊(cè)、激活賬號(hào)的方法
本篇文章主要介紹了django 通過ajax完成郵箱用戶注冊(cè)、激活賬號(hào)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04基于anaconda下強(qiáng)大的conda命令介紹
今天小編就為大家分享一篇基于anaconda下強(qiáng)大的conda命令介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06python中的print()函數(shù)end=' '的使用及說明
這篇文章主要介紹了python中的print()函數(shù)end=' '的使用及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02