Implementing Stack with Python

前端 未结 12 1117
长发绾君心
长发绾君心 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:02

    Your stack is an array...

    class stacked(): # Nodes in the stack
        def __init__(self,obj,next):
            self.obj = obj
            self.next = next
        def getObj(self):
            return(self.obj)
        def getNext(self):
            return(self.next)
    
    class stack(): # The stack itself
        def __init__(self):
            self.top=None
        def push(self,obj):
            self.top = stacked(obj,self.top)
        def pop(self):
            if(self.top == None):
                return(None)
            r = self.top.getObj()
            self.top = self.top.getNext()
            return(r)
    

提交回复
热议问题