(i) push_trail():
def push_trail(N, myStack):
last_five = N[-5:]
for item in last_five:
myStack.append(item)
print("Last 5 elements pushed onto the stack.")
This function takes the last 5 elements from list
N (using slicing
N[-5:]) and appends each to
myStack. A confirmation message is then displayed.
(ii) pop_one():
def pop_one(myStack):
if len(myStack) == 0:
print("Stack Underflow")
return None
else:
return myStack.pop()
This function checks if
myStack is empty. If so, it prints 'Stack Underflow' and returns
None. Otherwise, it removes and returns the top element using
pop().
(iii) display_all():
def display_all(myStack):
if len(myStack) == 0:
print("Empty Stack")
else:
for item in myStack:
print(item, end=" ")
print()
This function iterates through and prints all elements in
myStack, separated by spaces, without altering the stack's contents. If the stack is empty, it prints 'Empty Stack'.