- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackListCapacity.py
More file actions
Latest commit
54 lines (40 loc) · 1.16 KB
/
Copy pathstackListCapacity.py
File metadata and controls
54 lines (40 loc) · 1.16 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
# Implementing Stack using python Lists - max size
# Time and Space complexity of all methods is O(1) - constant
classStack:
# maximum size of stack
def__init__(self, max_size):
self.max_size=max_size
self.list= []
def__str__(self):
values=self.list.reverse()
values= [str(x) forxinself.list]
return'\n'.join(values)
defisEmpty(self):
ifself.list== []:
returnTrue
else:
returnFalse
# only for stack with max size
defisFull(self):
iflen(self.list) ==self.max_size:
returnTrue
else:
returnFalse
defpush(self, value):
ifself.isFull():
return"Stack is full"
else:
self.list.append(value)
return"Element inserted"
defpop(self):
ifself.isEmpty():
return"Stack is empty"
else:
returnself.list.pop()
defpeek(self):
ifself.isEmpty():
return"Stack is empty"
else:
returnself.list[len(self.list)-1]
defdelete(self):
self.list=None