Stack data structure in python

前端 未结 7 1450
轮回少年
轮回少年 2021-02-02 09:24

I have 2 issues with the code below:

  1. push(o) throws an exception TypeError: can only assign an iterable.
  2. Should I throw an exception if pop()

相关标签:
7条回答
  • 2021-02-02 09:37
        class Stack:
            def __init__(self):
                self.items=[]
    
            def isEmpty(self):
                return self.items==[]
    
            def push(self , item):
                self.items.append(item)
    
            def pop(self):
                return self.items.pop()
    
            def size(self):
                return len(self.items)
    
            def peek(self):
                return self.items[-1]
    

    Create a stack

    To create a new stack we can simply use Stack()

    for example:

         s=Stack()
    

    "s" is the name of new stack

    isEmpty

    By using isEmpty() we can check our stack is empty or not

    for example:

    we have two stacks name s1=(0,1,4,5,6) and s2=()

    if we use print(s1.isEmpty()) it will return False

    if we use print(s2.isEmpty()) it will return True

    push

    By using push operation we can add items to top of the stack

    we can add "6" to the stack name "s" using

        s.push(6)
    

    pop

    we can use pop operation to remove and return the top item of a stack

    if there is a stack name "s" with n amount items (n>0) we can remove it's top most item by using

        s.pop()
    

    size

    This operation will return how many items are in the stack

    if there is a stack name "s" s=(1,2,3,4,5,3)

        print(s.size())
    

    will return "6"

    peek This operation returns the top item without removing it

        print(s.peek())
    

    "we can print items of the stack using print(s.items)"

    0 讨论(0)
  • 2021-02-02 09:40
    class Stack:
        def __init__(self):
            self.stack = []
        def pop(self):
            if self.is_empty():
                return None
            else:
                return self.stack.pop()
        def push(self, d):
            return self.stack.append(d)
        def peek(self):
            if self.is_empty():
                return None
            else:
                return self.stack[-1]
        def size(self):
            return len(self.stack)
        def is_empty(self):
            return self.size() == 0
    
    0 讨论(0)
  • 2021-02-02 09:41

    Stack follows LIFO mechanism.You can create a list and do a normal append() to append the element to list and do pop() to retrieve the element out of the list which you just inserted.

    0 讨论(0)
  • 2021-02-02 09:44

    You are right to use composition instead of inheritance, because inheritance brings methods in that you don't want to expose.

    class Stack:
      def __init__(self):
        self.__storage = []
    
      def isEmpty(self):
        return len(self.__storage) == 0
    
      def push(self,p):
        self.__storage.append(p)
    
      def pop(self):
        return self.__storage.pop()
    

    This way your interface works pretty much like list (same behavior on pop for example), except that you've locked it to ensure nobody messes with the internals.

    0 讨论(0)
  • 2021-02-02 09:45

    No need to jump through these loops, See 5.1.1 Using Lists as Stacks

    If you insist on having methods isEmpty() and push() you can do:

    class stack(list):
        def push(self, item):
            self.append(item)
        def isEmpty(self):
            return not self
    
    0 讨论(0)
  • 2021-02-02 09:48

    Here is an example for stack class

    class Stack(object):
    
       def __init__(self):
          self.items = []
    
       def push(self, item):
          self.items.append(item)
    
       def pop(self):
           return self.items.pop()
    
       def peek(self):
           return self.items[-1]
    
       def isEmpty(self):
           return len(self.items) == 0
    
    0 讨论(0)
提交回复
热议问题