
Initialization:
stack = [None for _ in range(10)]
basePointer = 0
topPointer = -1
stackFull = 10 # constant
item = None
| Variable Name | Meaning |
|---|---|
| stack | array to store items |
| basePointer | always zero, no changes made, used to compare with topPointer to check if stack is empty (when topPointer<basePointer) |
| topPointer | points to the index where newest element is stored/ next element to be popped |
| stackFull | CONSTANT 10, no change |
| item | to store the popped value |
1. Push(item)
push deals with incrementing topPointer
def Push(item):
global stack, topPointer, basePointer, stackFull, item
if topPointer == StackFull -1: # full
print('stack full, cannot push')
else:
topPointer += 1 # comes before push
stack[topPointer] = item
2. Pop()
pop deals with decrementing topPointer
def Pop():
global stack, topPointer, basePointer, stackFull, item
if topPointer == -1: # topPointer < basePointer
print('stack empty, cannot pop.')
else:
item= stack[topPointer]
topPointer -=1