python实现Stack

上一篇 / 下一篇  2015-11-04 13:49:25 / 个人分类:python

very 简单,用list去模拟栈:
代码:

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+=1
def full(st):
if st.top==st.size:
return True
else:
return False
def empty(st):
if st.top==-1:
print "stack is empty"
def pull(st):
if st.empty():
print "stack is empty,no content to pull"
else:
st.top-=1

TAG: Python python

mimilog的职业角 引用 删除 mimilog   /   2015-11-05 21:22:07
实现队列:
还是使用python内置的数据结构list:

class Queue:
  def __init__(self):
    self.items = []
  def enqueue(self, item):
    self.items.append(item)
  def dequeue(self):
    return self.items.pop(0)
  def empty(self):
    return self.size() == 0
  def size(self):
    return len(self.items)
 

评分:0

我来说两句

Open Toolbar