preamble
Python itself already has implementations of sequential tables (List, Tupple), so here we start with the stack.
What is a stack?
Imagine a stack of books that have been piled up; this is the stack. The stack is characterized by the fact that the last book to be stacked in is always at the top. When you take a book out of the stack, which book is the most convenient to take? Definitely the top book. This data structure is characterized by the stack: Last In First Out (LIFO), i.e., the last data to be stacked in, the first to be taken out.
Python implementation of the stack
Stacks can be implemented as sequential lists or as chained lists. My big Python's built-in data structure is so powerful that you can use list to realize stack directly, simple and fast. Life is short, I use Python. the code is as follows:
class Stack(object): # Initialize the stack to an empty list def __init__(self): = [] # Determine if the stack is empty, return a boolean value def is_empty(self): return == [] # Return the top element of the stack def peek(self): return [len() - 1] # Return the size of the stack def size(self): return len() # Stack the new elements inside the stack (programmers like to call this process Press, Stack, Stack ......) def push(self, item): (item) # Drop the top element of the stack (programmers like to call this process out of the stack ......) def pop(self, item): return () if __name__ == __main__: # Initialize a stack object my_stack = Stack() # Throw 'h' on the stack # my_stack.push('h') # Throw 'a' on the stack # my_stack.push('a') # Look at the size of the stack (how many elements) # print my_stack.size() # Print the top stack element print my_stack.peek() # Throw out the top stack element and print it out # print my_stack.pop() # And look who's at the top of the stack print my_stack.peek() # What's the size of the stack at this point? print my_stack.size() # Drop another top stack element print my_stack.pop() # Look at the size of the stack # print my_stack.size # Is the stack empty? print my_stack.is_empty() # Whoa # # It's so good # print 'Yummy~'
Tips:
After reading the above code, smart students must know that the implementation of the stack inside Python is to wrap the list into a class and add some methods as the basic operation of the stack. Other data structures are implemented in Python in a similar way.
So, here's some have-nots to say~
This is fine if you want items[] to be a private property of the Stack class:
def __init__(self): self.__items = []
That's right, that's two underscores __ in front of items, and that's how private members of a class are defined in Python.
If you want to limit the Stack class to just items and not have other weirdos adding members, then this is fine:
class Stack(object): __slots__ = ('__items') def __init__(self): self.__items = []
It's much safer.
Python does not have the Javapublic/private/protected
modifiers like this, because the designers of Python thought, "Everyone's an adult now."~
summarize
The above is a small introduction to the Python implementation of the stack (Stack), I hope to help you, if you have any questions please leave me a message, I will promptly reply to you. I would also like to thank you very much for your support of my website!