#栈的实现,入栈判断是否Full,出栈判断是否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!"
举例:
>>>q=stack(7) #初始化栈的容量为7,并赋给q
>>>q.Empty() #检查栈是否为空
Ture
>>>q.push("hello") #进栈,推送“hello”到栈内
>>>q.Empty() #检查栈是否为空
Flase
>>>q.out #出栈
时间: 2024-10-07 11:52:41