国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python常見數據結構之棧與隊列用法示例

2020-02-16 00:38:07
字體:
來源:轉載
供稿:網友

本文實例講述了Python常見數據結構之棧與隊列用法。分享給大家供大家參考,具體如下:

Python常見數據結構之-棧

首先,棧是一種數據結構。具有后進先出特性。

#棧的實現class Stack():  def __init__(self,size):    self.stack=[]    self.size=size    self.top=-1  def push(self,content):    if self.Full():      print "Stack is Full"    else:      self.stack.append(content)      self.top=self.top+1  def out(self):    if self.Empty():      print "Stack is Empty"    else:      self.top-=1  def Full(self):    if self.top==self.size-1:      return True    else:      return False  def Empty(self):    if self.top==-1:      print "Stack is Empty"if __name__=="__main__":  q=Stack(7)  q.Empty()  q.push("hello")  q.Empty()

運行結果:

Stack is Empty

Python常見數據結構之-隊列

隊列是一種先進先出的數據結構。

#隊列的實現class Queue():  def __init__(self,size):    self.queue=[]    self.size=size    self.head=-1    self.tail=-1  def Empty(self):    if self.head==self.tail:      return True    else:      return False  def Full(self):    if self.tail-self.head==self.size-1:      return True    else:      return False  def enQueue(self,content):    if self.Full():      print "Queue is Full"    else:      self.queue.append(content)      self.tail+=1  def outQueue(self):    if self.Empty():      print "Queue is Empty!"    else:      self.head+=1if __name__=="__main__":  q=Queue(6)  print q.Empty() # True  q.enQueue("123")  print q.Empty() #False  q.outQueue()

運行結果:

True
False

更多關于Python相關內容感興趣的讀者可查看本站專題:《Python數據結構與算法教程》、《Python加密解密算法與技巧總結》、《Python編碼操作技巧總結》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》及《Python入門與進階經典教程》

希望本文所述對大家Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 迁安市| 色达县| 石林| 务川| 石棉县| 兴业县| 保靖县| 崇义县| 廊坊市| 本溪市| 博白县| 永福县| 新竹市| 永嘉县| 湛江市| 尚志市| 鲜城| 五峰| 扶风县| 林口县| 山西省| 平罗县| 宜城市| 凌海市| 海城市| 舒城县| 南靖县| 沙坪坝区| 武定县| 阜南县| 长岭县| 金平| 广丰县| 沙湾县| 神池县| 库车县| 株洲县| 大足县| 金平| 崇信县| 莒南县|