两个栈实现链表
class StackForList():
def __init__(self):
self.stack1 = []
self.stack2 = []
def push(self,node):
self.stack1.append(node)
def pop(self):
if len(self.stack2) != 0:
return self.stack2.pop()
elif len(self.stack1) !=0 :
while len(self.stack1):
self.stack2.append(self.stack1.pop())
return self.stack2.pop()
else:
return None
listhandle=StackForList()
listTest=[1,2,3,4]
for count in range(2):
for node in listTest:
listhandle.push(node)
for i in range(2):
print 'stack1:%-15s' %listhandle.stack1,
print 'stack2:%-15s' %listhandle.stack2
print 'pop:',listhandle.pop()
>>>
stack1:[1, 2, 3, 4] stack2:[]
pop: 1
stack1:[] stack2:[4, 3, 2]
pop: 2
stack1:[1, 2, 3, 4] stack2:[4, 3]
pop: 3
stack1:[1, 2, 3, 4] stack2:[4]
pop: 4