-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
96 lines (71 loc) · 1.71 KB
/
Copy pathstack.py
File metadata and controls
96 lines (71 loc) · 1.71 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/python
class Stack(object):
def __init__(self, size = 8):
self.stack = []
self.size = size
self.top = -1
def set_size(self, size):
if self.top >= size:
raise Exception("StackWillOverFlow")
self.size = size
def is_full(self):
return True if self.size == self.top + 1 else False
def is_empty(self):
return True if self.top == -1 else False
def push(self, data):
if self.is_full():
raise Exception("StackOverFlow")
return
self.stack.append(data)
self.top += 1
def pop(self):
if self.is_empty():
raise Exception("Stackis_empty")
return
self.top -= 1
return self.stack.pop()
def top(self):
if self.is_empty():
raise Exception("Stackis_empty")
return -1
return self.stack[self.top]
def show(self):
print self.stack
def test_stack(data):
stack = Stack(data)
stack.show()
for i in range(data):
stack.push(i)
stack.show()
try:
stack.push(data)
except Exception, e:
print e
else:
stack.show()
try:
stack.set_size(data/2)
except Exception, e:
print e
else:
stack.show()
while not stack.is_empty():
stack.pop()
stack.show()
for i in range(data/2):
stack.push(i)
stack.show()
try:
stack.push(data)
except Exception, e:
print e
else:
stack.show()
try:
stack.set_size(data -1)
except Exception, e:
print e
else:
stack.show()
if __name__ == '__main__':
test_stack(8)