Implementing Stack with Python

前端 未结 12 1259
长发绾君心
长发绾君心 2021-01-17 16:16

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:
            


        
12条回答
  •  不思量自难忘°
    2021-01-17 17:05

    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())
    

提交回复
热议问题