forked from ssjssh/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
51 lines (40 loc) · 1.27 KB
/
stack.py
File metadata and controls
51 lines (40 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
# -*- coding:utf-8 -*-
class Stack(object):
"""docstring for Stack"""
def __init__(self, cap):
super(Stack, self).__init__()
self.__cap = cap # 栈大小
self.__length = 0 # 栈长度
if self.__cap < 0:
raise ValueError("length of Stack can not be negtive")
self.__values = [0 for x in xrange(0, self.__cap)]
def empty(self):
return self.__length is 0
def push(self, x):
if self.__length >= self.__cap:
raise IndexError("stack is full, can not push any object")
self.__values[self.__length] = x
self.__length += 1
def pop(self):
if self.__length <= 0:
return None
self.__length -= 1
return self.__values[self.__length]
def __str__(self):
return "".join(["Stack, Value: ", str(self.__values), " cap: ", str(self.__cap),
" length:", str(self.__length)])
def main():
stack = Stack(5)
for x in xrange(0, 5):
stack.push(x)
print "full stack"
print stack
print "pop everything"
for x in xrange(0, 7):
print stack.pop()
print "index of bound error"
for x in xrange(0, 7):
stack.push(x)
if __name__ == '__main__':
main()