Skip to content

Instantly share code, notes, and snippets.

@yokiy
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yokiy/f8bd12d4622703486f68 to your computer and use it in GitHub Desktop.
Save yokiy/f8bd12d4622703486f68 to your computer and use it in GitHub Desktop.
cc 3.3
# 3.3 set of stacks
from stack import Stack, Node
class SetOfStacks:
def __init__(self, limit):
self.last = Stack()
self.limit = limit
self.stacklist = [self.last]
def push(self, item):
'''if the size of each stack is over limit
move the last to next stack top'''
if self.last.size >= self.limit:
Next = Stack()
Next.push(item)
self.stacklist.append(Next)
self.last = Next
else:
self.last.push(item)
def pop(self):
print 'pop element: ', self.last.pop()
if self.last.size == 0 and self.stacklist is not None:
del self.stacklist[len(self.stacklist) - 1]
self.last = self.stacklist[len(self.stacklist) - 1]
print 'now lastis ', self.last.printStack()
def printSets(self):
if self is None:
print 'No element in stacklist'
for i in range(len(self.stacklist)):
self.stacklist[i].printStack()
print '\n-----------'
#test
A = SetOfStacks(4)
A.push(5)
A.push(3)
A.push(10)
A.push(7)
A.push(8)
A.printSets()
A.pop()
A.pop()
print 'After pop two element'
A.printSets()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment