您的位置:首页 > 编程语言 > Python开发

Python 使用单链表实现堆栈 (基于class, 包含迭代器)

2015-01-28 14:42 295 查看
#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
Created on 2015-1-28
@author: beyondzhou
@name: test_linkliststack.py
'''

def test_linkliststack():

# import linkListStack
from mystack import LinkListStack

print '#Init a stack named smith using push'
smith = LinkListStack()
smith.push('CSCI-112')
smith.push('MATH-121')
smith.push('HIST-340')
smith.push('ECON-101')

print '\n#output smith stack'
for element in smith:
print element

print '\n#pop one item'
smith.pop()

print '\n#output smith stack after pop'
for element in smith:
print element

print '\n#get the peek item'
peek_item = smith.peek()
print 'peek item is ', peek_item

print '\n#get the length of stack'
print 'the lenght of stack is ', len(smith)

print '\n#check wheter the stack is empty'
if smith.isEmpty():
print 'stack is empty!'
else:
print 'stack is not empty!'

print '\n#pop all items'
while not smith.isEmpty():
smith.pop()

print '\n#check wheter the stack is empty after pop all items'
if smith.isEmpty():
print 'stack is empty!'
else:
print 'stack is not empty!'

if __name__ == "__main__":
test_linkliststack()


# Implementation of the Stack ADT using a singly linked list
class LinkListStack:
# Creates an empty stack
def __init__(self):
self._top = None
self._size = 0

# Returns True if the stack is empty or False otherwise
def isEmpty(self):
return self._top is None

# Returns the number of items in the stack
def __len__(self):
return self._size

# Returns the top item on the stack without removing it
def peek(self):
assert not self.isEmpty(), "Cannot peek at an empty stack"
return self._top.item

# Removes and returns the top item on the stack
def pop(self):
assert not self.isEmpty(), "Cannot pop from an empty stack"
node = self._top
self._top = self._top.next
self._size -= 1
print 'pop item:', node.item
return node.item

# Pushes an item onto the top of the stack
def push(self, item):
self._top = _StackNode(item, self._top)
self._size += 1

def __iter__(self):
return _LinkStackIterator(self._top)

# The private storage class for creating stack nodes
class _StackNode:
def __init__(self, item, link):
self.item = item
self.next = link

# Implementation of linked list stack iter
class _LinkStackIterator:
def __init__(self, listHead):
self._curNode = listHead

def __iter__(self):
return self

def next(self):
if self._curNode is None:
raise StopIteration
else:
item = self._curNode.item
self._curNode = self._curNode.next
return item


#Init a stack named smith using push

#output smith stack
ECON-101
HIST-340
MATH-121
CSCI-112

#pop one item
pop item: ECON-101

#output smith stack after pop
HIST-340
MATH-121
CSCI-112

#get the peek item
peek item is  HIST-340

#get the length of stack
the lenght of stack is  3

#check wheter the stack is empty
stack is not empty!

#pop all items
pop item: HIST-340
pop item: MATH-121
pop item: CSCI-112

#check wheter the stack is empty after pop all items
stack is empty!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐