forked from IrvKalb/Object-Oriented-Python-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.py
More file actions
33 lines (27 loc) · 917 Bytes
/
Copy pathStack.py
File metadata and controls
33 lines (27 loc) · 917 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
# Stack class
class Stack():
''' Stack class implements a last in first out LIFO algorithm'''
def __init__(self, startingStackAsList=None):
if startingStackAsList is None:
self.dataList = [ ]
else:
self.dataList = startingStackAsList[:] # make a copy
def push(self, item):
self.dataList.append(item)
def pop(self):
if len(self.dataList) == 0:
raise IndexError
element = self.dataList.pop()
return element
def peek(self):
# Retrieve the top item, without removing it
item = self.dataList[-1]
return item
def getSize(self):
nElements = len(self.dataList)
return nElements
def show(self):
# Show the stack in a vertical orientation
print('Stack is:')
for value in reversed(self.dataList):
print(' ', value)