-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathExercise_1.py
More file actions
60 lines (49 loc) · 1.69 KB
/
Copy pathExercise_1.py
File metadata and controls
60 lines (49 loc) · 1.69 KB
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
60
class myStack:
#Please read sample.java file before starting.
#Kindly include Time and Space complexity at top of each file
#Time and Space Complexity
# Overall Space Complexity = O(N) where N is the MAX_SIZE of 1000
# __init__ function has the time and space complexity of O(N)
# isEmpty function has the time and space complexity of O(1)
# push function has the time and space complexity of O(1)
# pop function has the time and space complexity of O(1)
# peek function has the time and space complexity of O(1)
# size function has the time and space complexity of O(1)
# show function has the time and space complexity of O(M) where M dentoes the number of elements in the stack
def __init__(self):
self.MAX_SIZE=1000
self.array=[None]*self.MAX_SIZE
self.top=-1
def isEmpty(self):
if self.top==-1:
return True
return False
def push(self, item):
if self.top<self.MAX_SIZE-1:
self.top+=1
self.array[self.top]=item
else:
print("Stack Overflow")
return False
def pop(self):
if self.top>-1:
value=self.array[self.top]
self.top-=1
return value
else:
print("Stack Underflow")
return False
def peek(self):
if self.top==-1:
return False
else:
return self.array[self.top]
def size(self):
return self.top+1
def show(self):
return self.array[0: self.top+1][::-1]
s = myStack()
s.push('1')
s.push('2')
print(s.pop())
print(s.show())