Computer Science
Write push(rollno)
and pop()
method in Python:
push(rollno)
— to add roll number in Stack
pop()
— to remove roll number from Stack
Python
Python Stack
3 Likes
Answer
stack = []
def push(rollno):
stack.append(rollno)
print("Roll number", rollno, "added to the stack.")
def pop():
if not is_empty():
rollno = stack.pop()
print("Roll number", rollno, "removed from the stack.")
else:
print("Stack is empty, cannot perform pop operation.")
def is_empty():
return len(stack) == 0
push(11)
push(12)
push(13)
pop()
pop()
Output
Roll number 11 added to the stack.
Roll number 12 added to the stack.
Roll number 13 added to the stack.
Roll number 13 removed from the stack.
Roll number 12 removed from the stack.
Answered By
1 Like
Related Questions
Define alternate key.
Define candidate key.
Write the output of the following code with justification if the contents of the file "VIVO.txt" are:
"Welcome to Python Programming!"
F1 = open("VIVO.txt", "r") size = len(F1.read()) print(size) data = F1.read(5) print(data)
Simrat has written a code to input an integer and display its first 10 multiples (from 1 to 10). Her code is having errors. Rewrite the correct code and underline the corrections made.
n = int(input(Enter an integer:)) for i in range(1, 11): m = n * j print(m; End = '')