-
Notifications
You must be signed in to change notification settings - Fork 14
/
StackListLimited.py
57 lines (50 loc) · 1.39 KB
/
StackListLimited.py
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
'''
We will create a stack using a python list datatype, which has a pre-defined
size-limit.
'''
class Stack:
def __init__(self, maxSize):
self.maxSize = maxSize
self.list = []
def __str__(self):
values = self.list.reverse()
values = [str(x) for x in self.list]
return '\n'.join(values)
# time - complexity - O(1)
# space - complexity - O(1)
def isEmpty(self):
if self.list == []:
return True
else:
return False
def isFull(self):
if len(self.list) == self.maxSize:
return True
else:
return False
def push(self, value):
if self.isFull():
return "The stack is full. Cannot push anymore elements."
else:
self.list.append(value)
return "The element has been successfully pushed into the stack."
def Pop(self):
if self.isEmpty():
return "No elements found in the stack."
else:
return self.list.pop()
def Peek(self):
if self.isEmpty():
return "No elements found in the stack."
else:
return self.list[len(self.list)-1]
def delete(self):
self.list = None
customStack = Stack(5)
print(customStack.isEmpty())
print(customStack.isFull())
customStack.push(2)
customStack.push(7)
customStack.push(9)
customStack.push(3)
print(customStack)