Stack implemented by Python and stack implemented by python

Source: Internet
Author: User

Stack implemented by Python and stack implemented by python

Preface

Python already has the implementation of ordered tables (List and Tupple), so it starts from the stack.

What is stack?

Imagine a pile of books piled up. This is the stack. This pile of books is characterized by the fact that the books finally piled up are always at the top. Which book is the most convenient? It must be the top one. The characteristics of the data structure such as Stack are as follows: Last In First Out-LIFO, that is, the data that is finally piled In, is First taken Out.

Stack Python implementation

Stack can be implemented by means of sequential tables or linked lists. The built-in data structure of my big Python is too powerful. You can use list to directly implement the stack, which is simple and quick. My life is short. I use Python. The Code is as follows:

Class Stack (object): # The initialization Stack is an empty list of def _ init _ (self): self. items = [] # judge whether the stack is empty. return the Boolean value def is_empty (self): return self. items = [] # return stack top element def peek (self): return self. items [len (self. items)-1] # return stack size def size (self): return len (self. items) # stack new elements into the stack (programmers like to call this process as a pressure stack, an inbound stack, or a stack ......) Def push (self, item): self. items. append (item) # Drop the top element of the stack (programmers like to call this process a stack ......) Def pop (self, item): return self. items. pop () if _ name _ = _ main __: # initialize a Stack object my_stack = Stack () # Drop 'H' into the Stack my_stack.push ('H ') # throwing 'A' into the stack my_stack.push ('A') # Check the stack size (several elements) print my_stack.size () # print the top element of the stack print my_stack.peek () # Drop the top element of the stack and print it out. print my_stack.pop () # Check the top element of the stack and print my_stack.peek () # What is the size of the stack at this time? Print my_stack.size () # Drop another top element of the stack, print my_stack.pop () # Check the size of the stack. print my_stack.size # Is the stack empty? Print my_stack.is_empty () # Wow ~ Delicious ~ Print 'yum ~ '

Tips:

After reading the above Code, the smart guys will surely know that the Implementation stack in Python is to package the list into a class, and then add some methods as the basic operation of the stack. Other data structures are also implemented in a similar way in Python.
So there are some things you don't have to talk about here ~

If you want items [] to be a private attribute of the Stack class, do this:

def __init__(self):  self.__items = []

That's right, just add two underlines __in front of items. In Python, the private member of the class is defined like this ~

If you want to limit that only the members of the Stack class are items, and do not add other cool members, you can do this:

class Stack(object):  __slots__ = ('__items')  def __init__(self):    self.__items = []

This makes it much safer ~

Python does not includepublic/private/protectedThis modifier is because the Python designers believe that "everyone is an adult "~

Summary

The above is the Python Stack implementation introduced by xiaobian. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.