• <bdo id='Nzdpx'></bdo><ul id='Nzdpx'></ul>
    <tfoot id='Nzdpx'></tfoot>

    <i id='Nzdpx'><tr id='Nzdpx'><dt id='Nzdpx'><q id='Nzdpx'><span id='Nzdpx'><b id='Nzdpx'><form id='Nzdpx'><ins id='Nzdpx'></ins><ul id='Nzdpx'></ul><sub id='Nzdpx'></sub></form><legend id='Nzdpx'></legend><bdo id='Nzdpx'><pre id='Nzdpx'><center id='Nzdpx'></center></pre></bdo></b><th id='Nzdpx'></th></span></q></dt></tr></i><div id='Nzdpx'><tfoot id='Nzdpx'></tfoot><dl id='Nzdpx'><fieldset id='Nzdpx'></fieldset></dl></div>

      1. <legend id='Nzdpx'><style id='Nzdpx'><dir id='Nzdpx'><q id='Nzdpx'></q></dir></style></legend>
      2. <small id='Nzdpx'></small><noframes id='Nzdpx'>

      3. 用 Python 实现堆栈

        时间:2023-09-27

        1. <tfoot id='OiCvx'></tfoot>
          • <bdo id='OiCvx'></bdo><ul id='OiCvx'></ul>

            <small id='OiCvx'></small><noframes id='OiCvx'>

            • <legend id='OiCvx'><style id='OiCvx'><dir id='OiCvx'><q id='OiCvx'></q></dir></style></legend>

              1. <i id='OiCvx'><tr id='OiCvx'><dt id='OiCvx'><q id='OiCvx'><span id='OiCvx'><b id='OiCvx'><form id='OiCvx'><ins id='OiCvx'></ins><ul id='OiCvx'></ul><sub id='OiCvx'></sub></form><legend id='OiCvx'></legend><bdo id='OiCvx'><pre id='OiCvx'><center id='OiCvx'></center></pre></bdo></b><th id='OiCvx'></th></span></q></dt></tr></i><div id='OiCvx'><tfoot id='OiCvx'></tfoot><dl id='OiCvx'><fieldset id='OiCvx'></fieldset></dl></div>
                  <tbody id='OiCvx'></tbody>
                  本文介绍了用 Python 实现堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用数组使用 Python 实现一个简单的堆栈.我想知道是否有人可以让我知道我的代码有什么问题.

                  I am trying to implement a simple stack with Python using arrays. I was wondering if someone could let me know what's wrong with my code.

                  class myStack:
                       def __init__(self):
                           self = []
                  
                       def isEmpty(self):
                           return self == []
                  
                       def push(self, item):
                           self.append(item)
                  
                       def pop(self):
                           return self.pop(0)
                  
                       def size(self):
                           return len(self)
                  
                      s = myStack()
                      s.push('1')
                      s.push('2')
                      print(s.pop())
                      print s
                  

                  推荐答案

                  我纠正了以下几个问题.此外,在抽象编程术语中,堆栈"通常是您从顶部添加和删除的集合,但您实现它的方式是添加到顶部并从底部删除,这使其成为队列.

                  I corrected a few problems below. Also, a 'stack', in abstract programming terms, is usually a collection where you add and remove from the top, but the way you implemented it, you're adding to the top and removing from the bottom, which makes it a queue.

                  class myStack:
                       def __init__(self):
                           self.container = []  # You don't want to assign [] to self - when you do that, you're just assigning to a new local variable called `self`.  You want your stack to *have* a list, not *be* a list.
                  
                       def isEmpty(self):
                           return self.size() == 0   # While there's nothing wrong with self.container == [], there is a builtin function for that purpose, so we may as well use it.  And while we're at it, it's often nice to use your own internal functions, so behavior is more consistent.
                  
                       def push(self, item):
                           self.container.append(item)  # appending to the *container*, not the instance itself.
                  
                       def pop(self):
                           return self.container.pop()  # pop from the container, this was fixed from the old version which was wrong
                  
                       def peek(self):
                           if self.isEmpty():
                               raise Exception("Stack empty!")
                           return self.container[-1]  # View element at top of the stack
                  
                       def size(self):
                           return len(self.container)  # length of the container
                  
                       def show(self):
                           return self.container  # display the entire stack as list
                  
                  
                  s = myStack()
                  s.push('1')
                  s.push('2')
                  print(s.pop())
                  print(s.show())
                  

                  这篇关于用 Python 实现堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:获取堆栈中上一级函数的 __file__ 下一篇:inspect.currentframe() 在某些实现下可能不起作用?

                  相关文章

                  1. <tfoot id='w0swU'></tfoot>
                  2. <small id='w0swU'></small><noframes id='w0swU'>

                    <legend id='w0swU'><style id='w0swU'><dir id='w0swU'><q id='w0swU'></q></dir></style></legend>

                      <i id='w0swU'><tr id='w0swU'><dt id='w0swU'><q id='w0swU'><span id='w0swU'><b id='w0swU'><form id='w0swU'><ins id='w0swU'></ins><ul id='w0swU'></ul><sub id='w0swU'></sub></form><legend id='w0swU'></legend><bdo id='w0swU'><pre id='w0swU'><center id='w0swU'></center></pre></bdo></b><th id='w0swU'></th></span></q></dt></tr></i><div id='w0swU'><tfoot id='w0swU'></tfoot><dl id='w0swU'><fieldset id='w0swU'></fieldset></dl></div>
                        <bdo id='w0swU'></bdo><ul id='w0swU'></ul>