0% found this document useful (0 votes)
3 views

ADT Python Code

The document outlines the implementation of a stack data structure in Python, including its initialization as an empty stack with a defined size. It describes two main operations: 'push', which adds an item to the stack if it is not full, and 'pop', which removes the top item from the stack if it is not empty. The code uses global variables to manage the stack's state and operations.

Uploaded by

agmshindi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

ADT Python Code

The document outlines the implementation of a stack data structure in Python, including its initialization as an empty stack with a defined size. It describes two main operations: 'push', which adds an item to the stack if it is not full, and 'pop', which removes the top item from the stack if it is not empty. The code uses global variables to manage the stack's state and operations.

Uploaded by

agmshindi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

1.

Empty Stack
# Empty Stack

stack = [None for index in range(0,10)] #


basePointer = 0
topPointer = -1
stackFull = 10
item = None

2. Stack Push an Operation(Adding an item to a Stack)

def push(item):
global topPointer
if topPointer < stackFull - 1: #stackFull is 9
topPointer = topPointer + 1
stack[topPointer] = item
else:
print("Stack is full, cannot push")

3. Stack Pop(Removing an Item from a Stack)

def pop():
global topPointer, basePointer, item #Global used within subroutine to access
variables
if topPointer == basePointer -1: #topPointer points to the top of the stack
print("Stack is empty, cannot pop")
else:
item = stack[topPointer] #stack[topPointer] is assigned to item because it is
being removed.
topPointer = topPointer -1 #deducted by 1 since it has been removed.

You might also like