-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathstackUsingArray.py
More file actions
45 lines (37 loc) · 915 Bytes
/
stackUsingArray.py
File metadata and controls
45 lines (37 loc) · 915 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
class stackuarr:
def __init__(self):
self.stack = []
self.top = -1
# def createstack(self):
# self.stack = []
def push(self, itemp):
if self.top == 15:
print("stack overflow")
else:
self.top += 1
# self.itemp = itemp
self.stack.append(itemp)
print("The item pushed is", itemp)
def pop(self):
self.top -= 1
self.stack.pop()
if self.top == -1:
print("stack underflow")
def peek(self):
print(self.stack[self.top])
def display(self):
print("top =", self.top)
for i in range(self.top, -1, -1):
if i == -1:
print(self.stack[i], "<-------Top")
else:
print(self.stack[i])
print("|")
p = stackuarr()
p.push(14)
p.push(11)
p.pop()
p.push(8)
p.display()
p.pop()
p.pop()