Preface
Python itself has an implementation of the sequential table (list, tupple), so it starts here from the stack. What is Stack
Imagine a pile of books piled up, and this is the stack. The feature of this pile of books is that the last books that are piled in are always on top. It is most convenient to take a book out of this pile of books. It must be the top one. This data structure of the stack is characterized by the fact that the LIFO (last in the first Out-lifo), which is finally being piled in, is first taken out. python implementation of Stacks
Stacks can be implemented in sequential tables or linked lists. I'm big Python's built-in data structure is too powerful, you can use the list directly to implement the stack, simple and quick. Life is short, I use Python. The code is as follows:
Class Stack (object): # initialization stack is empty list def __init__ (self): Self.items = [] # To determine if stack is empty, return Boolean def is_empty (
Self): return self.items = = [] # back to stack top element def peek (self): Returns Self.items[len (Self.items)-1] # Back to stack size def size (self): return Len (self.items) # put the new elements into the stack (programmers like to call this process the stack, into the stack, into the stack ...) def push (self, item): Self.items.append (item) # throws the top element out of the stack (programmers like to call this process a stack ...). def pops (self, item): Return Self.items.pop () if __name__ = = __main__: # Initializes a stack object my_stack = Stack ( # throw ' h ' into the stack My_stack.push (' h ') # put ' a ' in the stack My_stack.push (' a ') # look at the size of the stack (with several elements) print my_stack.size () # print stack top element print My_stack.peek () # Throw the top element out of the stack and print it Out My_stack.pop () # and then look at the top of the stack who print My_stac
K.peek () # What is the size of the stack at this time?
Print my_stack.size () # Throw one more stack top element print My_stack.pop () # Look at the size of the stack print My_stack.size # stack is not empty. Print my_stack.is_empty () # Wow, that's delicious ~ PRint ' yummy~ '
Tips:
Read the above code, smart students must know, Python inside the implementation stack, is the list packaging into a class, and then add some methods as the basic operation of the stack. Other data structures are also implemented in Python in a similar way.
Well, here are some that have nothing to say ~
If you want items[] to be a private property of the Stack class, that's fine:
def __init__ (self):
self.__items = []
Yes, it is to add two underscores in front of items, in Python, the private members of a class are defined in this way ~
If you want to qualify the members of the Stack class with items only, don't be a member of the other zombie group, then do it:
Class Stack (object):
__slots__ = (' __items ')
def __init__ (self):
self.__items = []
It's so much safer.
Python does not have modifiers like public/private/protected in Java, because the Python designer thinks, "Everyone is an adult" ~