-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
55 lines (44 loc) · 1.14 KB
/
Copy pathstack.py
File metadata and controls
55 lines (44 loc) · 1.14 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
#!/usr/bin/python
# -*- coding: UTF-8 -*-
class Pystack:
def __init__(self, size):
self.size = size
self.stack = []
self.top = -1
def empty(self):
self.top = -1
self.stack = []
def isEmpty(self):
if self.top == -1:
return True
else:
return False
def isFull(self):
if self.top = self.size - 1:
return True
else:
return False
def push(self, element):
if self.isFull():
raise StackException('Pystack is full')
else:
self.stack.append(element)
self.top = self.top + 1
def pop(self):
if self.isEmpty():
raise StackException('Pystack is empty')
else:
tmp = self.stack.pop()
self.top = self.top - 1
return tmp
class StackException(Exception):
def __init__(self, data):
self.data = data
def __str__(self):
return self.data
if __name__ == '__main__':
stack = Pystack(20)
for i in range(1, 10):
stack.push(i)
for i in range(1, 10):
print stack.pop()