溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Python中有哪些實(shí)現(xiàn)棧的方法

發(fā)布時(shí)間:2021-05-07 15:37:33 來(lái)源:億速云 閱讀:184 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)Python中有哪些實(shí)現(xiàn)棧的方法,文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

python有哪些常用庫(kù)

python常用的庫(kù):1.requesuts;2.scrapy;3.pillow;4.twisted;5.numpy;6.matplotlib;7.pygama;8.ipyhton等。

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 "棧的長(zhǎng)度:", 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é)果:

棧的長(zhǎng)度: 4
items:['welcome', 'www', 'jb51', 'net']
出棧: net
出棧: jb51
出棧: www

  • 棧的鏈表實(shí)現(xiàn):

棧的鏈表實(shí)現(xiàn)中,壓棧(push)類(lèi)似于在單鏈表中表頭添加節(jié)點(diǎn);出棧(pop)類(lèi)似于鏈表中表頭刪除節(jié)點(diǎn)并返回對(duì)應(yīng)節(jié)點(diǎn)值;棧頂元素(top)就是獲取鏈表中的第一個(gè)元素

鏈表節(jié)點(diǎn)的定義直接嵌套在鏈表?xiàng)n?lèi)中

代碼如下:

# 鏈表實(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 "棧的長(zhǎng)度:", stack.size()
  print "出棧:",stack.pop()
  print "出棧:",stack.pop()
  print "出棧:",stack.pop()
  print "出棧:",stack.pop()

上述就是小編為大家分享的Python中有哪些實(shí)現(xiàn)棧的方法了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI