Skip to content

Commit 75ac338

Browse files
committed
stack implemented using array
1 parent 7b4356d commit 75ac338

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

stack_using_array.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from sys import maxsize
2+
3+
4+
def createStack():
5+
stack = []
6+
return stack
7+
8+
9+
def isEmpty(stack):
10+
return len(stack) == 0
11+
12+
13+
def push(stack, data):
14+
stack.append(data)
15+
print(data, "pushed to stack")
16+
17+
18+
def pop(stack):
19+
if isEmpty(stack):
20+
return str(-maxsize - 1)
21+
22+
return stack.pop()
23+
24+
25+
def peek(stack):
26+
if isEmpty(stack):
27+
return str(-maxsize - 1)
28+
else:
29+
return stack[len(stack) - 1]
30+
31+
32+
stack = createStack()
33+
push(stack, 10)
34+
push(stack, 20)
35+
push(stack, 30)
36+
print(stack)
37+
pop(stack)
38+
print(stack)
39+
print(peek(stack))
40+
print(isEmpty(stack))

0 commit comments

Comments
 (0)