-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.py
59 lines (45 loc) · 1.13 KB
/
stack.py
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
class Stack:
def __init__(self,size=20):
self.stack= []
self.size= size;
self.top= -1
def setSize(self,size):
self.size=size;
def push(self,element):
if self.isFull():
raise "StackOverflow"
else:
self.stack.append(element)
self.top = self.top + 1
def pop(self):
if self.isEmpty():
raise "StackUnderflow"
else:
element=self.stack[-1]
self.top=self.top-1;
del self.stack[-1]
return element
def Top(self):
return self.top
def empty(self):
self.stack=[]
self.top=-1
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
if __name__ == "__main__":
stack=Stack()
for i in range(10):
stack.push(i)
print stack.Top()
for i in range(10):
print stack.pop()
stack.empty()
print stack.Top()