Implementation stack

"""Implement stack-->Sequence table-->python list"""


class Stack(object):
    def __init__(self):
        """Create an empty stack"""
        self.__list = []

    def push(self, item):
        """压栈"""
        self.__list.append(item)

    def pop(self):
        """弹栈"""
        return self.__list.pop()

    def peek(self):
        """Return the top element of the stack"""
        # Note: special case: empty lists do not support slicing operations
        if self.__list:
            # if the list is not empty
            return self.__list[-1]
        else:
            return None

    def is_empty(self):
        """Is the stack empty?"""
        return self.__list == []
        # Equivalent to return not self.__list

    def size(self):
        """Return the number of elements in the stack"""
        return len(self.__list)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325306347&siteId=291194637
Recommended