forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamic_Stack.py
More file actions
48 lines (40 loc) · 732 Bytes
/
Dynamic_Stack.py
File metadata and controls
48 lines (40 loc) · 732 Bytes
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
class Dynamic_Stack:
capacity = 0
stack = []
def __init__(self, capacity):
self.capacity = capacity
self.stack = []
def isFull(self):
return len(self.stack) == self.capacity
def isEmpty(self):
return len(self.stack) == 0
def push(self, element):
if(self.isFull()):
return
else:
self.stack.append(element)
def pop(self):
if(self.isEmpty()):
return -1
else:
temp = self.stack[-1]
self.stack[:-2]
return temp
def peek(self):
if(self.isEmpty()):
return -1
else:
return self.stack[-1]
# Example
a = Dynamic_Stack(4)
print(a.isEmpty())
a.push(1)
print(a.isEmpty())
a.push(2)
a.push(3)
print(a.isFull())
a.push(4)
print(a.isFull())
print(a.peek())
print(a.pop())
print(a.peek())