Stack implementation, into the stack to determine whether full, the stack to determine whether empty

Source: Internet
Author: User

#栈的实现, the stack to determine whether the full, the stack to determine whether empty


Class Stack ():

def __init__ (st,size):

St.stack=[];

St.size=size;

St.top=-1;


def push (st,content):

If St. Full ():

Print "Stack is full!"

Else

St.stack.append (content)

St.top=st.top+1

def out (ST):

If St. Empty ():

Print "Stack is empty!"

Else

St.top=st.top-1

def full (ST):

If st.top==st.size:

Return Ture

Else

Return False

def Empty (ST):

If St.top==-1:

Print "Stack is empty!"

Example:

>>>q=stack (7) #初始化栈的容量为7, and assign to Q

>>>q.empty () #检查栈是否为空

Ture

>>>q.push ("Hello") #进栈, push "Hello" inside the stack

>>>q.empty () #检查栈是否为空

Flase

>>>q.out #出栈

This article from "Life is too short, I use Python" blog, reprint please contact the author!

Stack implementation, into the stack to determine whether full, the stack to determine whether empty

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.