CBSE · 083Class XII · 2025Section C3 marks
Question 30(or)
4 March 2025 · Computer Science (083)
Write the following user-defined functions in Python: (i) push_trail(N, myStack): Here N and myStack are lists, and myStack represents a stack. The function should push the last 5 elements from the list N onto the stack myStack.
(ii) pop_one(myStack): The function should pop an element from the stack myStack, and return this element. If the stack is empty, then the function should display the message 'Stack Underflow', and return None.
(iii) display_all(myStack): The function should display all the elements of the stack myStack, without deleting them. If the stack is empty, the function should display the message 'Empty Stack'.
Answer
python
def push_trail(N,myStack):
for i in range(-5,0,1): # Any other correct loop
myStack.append(N[i])
def pop_one(myStack):
if not myStack: #OR if myStack==[]: #OR if len(myStack)==0:
print('Stack Underflow')
else:
return myStack.pop()
def display_all(myStack):
if not myStack: #OR if myStack==[]: #OR if len(myStack)==0:
print("Empty Stack")
else:
for i in myStack[::-1]:
print(i,end=' ')