# -*-coding: utf-8 -*-
# Author :writen by Qiusheng Li
# Time: 2014/08/15
# push Insert an item at the top of the stack
# pop Remove an item from the top of the stack
# peek Look up the element on the top
# empty/size Check whether the stack is empty or return its size
# How to implement with lists and its methods append() and pop()
class StackBefore:
'''define the stackbefore class'''
def __init__(self,a,b):
self.r = a
self.i = b
kind = 'struction' # all class variable shared by all instances
def add(self,x): # add self.r and self.i as argument add argument x
return self.i + self.r + int(x) # return total result
''' testing code
c = StackBefore(2,3)
d = StackBefore(3,4)
print c.r,c.i,c.kind
print d.r,d.i,d.kind
print 'c : add() ',c.add(2)
print 'd : add() ',d.add(2)
print 'OK'
'''
'''testing result
2 3 struction
3 4 struction
c : add() 7
d : add() 9
OK
'''
# first coding Stack(using list)
class Stack:
'''define the stack class'''
def __init__(self):
self.items = []
def push(self,items):
self.items.append(items)
def isEmpty(self):
return self.items == []
def pop(self):
if not self.isEmpty():
return self.items.pop()
else:
raise Exception('empty!')
def top(self):
if not self.isEmpty():
return self.items[-1]
else:
raise Exception('empty!')
def size(self):
return len(self.items)
def StackMain():
stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)
print 'size ',stack.size()
print 'top ',stack.top()
print 'pop ',stack.pop()
print 'pop ',stack.pop()
print 'pop ',stack.pop()
print 'size ',stack.size()
#second coding Stack(using myself data struction)
class Node(object):
def __init__(self,value=None):
self.value = value
self.next = None
class StackNode(object):
'''define the StackNode class'''
def __init__(self):
self.top = None
def push(self,value):
node = Node(value)
node.next = self.top
self.top = node
def isEmpty(self):
return not bool(self.top)
def pop(self):
node = self.top
if node:
self.top = node.next
return node.value
else:
raise Exception('StackNode is empty!')
def peek(self):
if bool(self.top):
return self.top.value
else:
raise Exception('StackNode is empty!')
def size(self):
node = self.top
i = 0
while node:
i += 1
node = node.next
return i
#testing code
def StackNodeMain():
stack = StackNode()
stack.push(1)
stack.push(2)
stack.push(3)
print 'size ',stack.size()
print 'top ',stack.peek()
print 'pop ',stack.pop()
print 'pop ',stack.pop()
print 'pop ',stack.pop()
print 'size ',stack.size()
print 'top ',stack.peek()
'''tesing result
size 3
top 3
pop 3
pop 2
pop 1
size 0
top
Traceback (most recent call last):
File "F:\201408\project\technoloybook\algorithms\codes\Stack.py", line 137, in <module>
StackNodeMain()
File "F:\201408\project\technoloybook\algorithms\codes\Stack.py", line 130, in StackNodeMain
print 'top ',stack.peek()
File "F:\201408\project\technoloybook\algorithms\codes\Stack.py", line 109, in peek
raise Exception('StackNode is empty!')
Exception: StackNode is empty!
'''
if __name__ == '__main__':
StackNodeMain()
Stack---python
最新推荐文章于 2024-09-15 22:31:42 发布