Ordering in stacks
A stack provides last in/first out (LIFO) data storage. The NumPy package provides
an actual stack implementation. In addition, Pandas associates stacks with objects
such as the
DataFrame
. However, both packages hide the stack implementation
details, and seeing how a stack works really does help. Consequently, the follow-
ing example implements a stack using a standard Python
list
. (You can find this
code in the
A4D; 06; Stacks, Queues, and Dictionaries.ipynb
file on the
Dummies site as part of the downloadable code; see the Introduction for details.)
MyStack = []
StackSize = 3
def DisplayStack():
print("Stack currently contains:")
for Item in MyStack:
print(Item)
def Push(Value):
if len(MyStack) < StackSize:
MyStack.append(Value)
122
PART 2
Do'stlaringiz bilan baham: |