Skip to content

Commit b99dd81

Browse files
Nabarun Paljainaman224
Nabarun Pal
authored andcommitted
Added Dynamic Stack using Python (jainaman224#249)
* Added Dynnamic Stack using Python * .pyc removed
1 parent 124fe76 commit b99dd81

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

Dynamic_Stack_Arrays/Dynamic_Stack.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Dynamic_Stack:
2+
capacity = 0
3+
stack = []
4+
5+
def __init__(self, capacity):
6+
self.capacity = capacity
7+
self.stack = []
8+
9+
def isFull(self):
10+
return len(self.stack) == self.capacity
11+
12+
def isEmpty(self):
13+
return len(self.stack) == 0
14+
15+
def push(self, element):
16+
if(self.isFull()):
17+
return
18+
else:
19+
self.stack.append(element)
20+
21+
def pop(self):
22+
if(self.isEmpty()):
23+
return -1
24+
else:
25+
temp = self.stack[-1]
26+
self.stack[:-2]
27+
return temp
28+
29+
def peek(self):
30+
if(self.isEmpty()):
31+
return -1
32+
else:
33+
return self.stack[-1]
34+
35+
# Example
36+
a = Dynamic_Stack(4)
37+
38+
print(a.isEmpty())
39+
a.push(1)
40+
print(a.isEmpty())
41+
a.push(2)
42+
a.push(3)
43+
print(a.isFull())
44+
a.push(4)
45+
print(a.isFull())
46+
print(a.peek())
47+
print(a.pop())
48+
print(a.peek())

0 commit comments

Comments
 (0)