Stack of Python data structures

Source: Internet
Author: User

Stack

Stack, which is called a stack, is a container that can be stored in data elements, access elements, delete elements, and it is characterized only by allowing the addition of data (English: Push) and output data (English: Pop) to one end of the container (called the stack Top indicator, English: top). Without a location concept, it is guaranteed that any element that can be accessed or deleted at any time is the one that was last deposited, and a default access order was determined.

Because the stack data structure is only allowed to operate at one end, it operates according to the principle of last-in-first-out.

Enclose the complete code:

class Stack (object):     def __init__ (self):
#初始化        Self.items=[]    def  Empty (self):
#判断是否为空         return Self.items = = [    ]def  push (Self,item):
#压栈        self.items.append (item)    def  pop (self):
#进栈         return   self.items.pop ()    def  Peek (self):
#返回栈顶         return Self.items[len (self.items)-1]    def  size (self):
#返回长度         return Len (self.items) if __name__ " __main__ " :
#站函数 Stack=Stack () Stack.push ("Hello") Stack.push ("python") Stack.push ("Itcast")    Print(Stack.size ())Print(Stack.peek ())Print(Stack.pop ())Print(Stack.pop ())Print(Stack.pop ())

Stack of Python data structures

Related Article

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.