-
Notifications
You must be signed in to change notification settings - Fork 0
/
stackclass.py
43 lines (32 loc) · 949 Bytes
/
stackclass.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
class StackClass:
NOELEMENTS = "The stack does not have elements."
def __init__(self):
self.items =[]
def push(self, item):
self.items.append(item)
msg = "Added " + item + " to the stack."
print(msg)
return None
# return "Added item to the stack."
def pop(self):
if self.items:
topitem = self.peek()
msg = topitem + " is on top of the stack."
print(msg)
self.items.pop()
msg = "Removed " + topitem + " from the stack."
print(msg)
return None
return self.NOELEMENTS
def peek(self):
if self.items:
return self.items[-1]
return self.NOELEMENTS
def size(self):
return len(self.items)
def is_empty(self):
return self.items==[]
# Test Module
test = StackClass()
test.push("Apple")
test.push("Orange")