Last Updated :
10 Apr, 2024
Declare a stack of integers
while ( there are more integers in the string to read )
{
read a integer
push the integer on the stack
}
while ( the stack is not empty )
{
pop a integer off the stack
write the integer to the screen
}
What is the output for \”123456789\”
(A) 123456789123456789
(B) 987654321
(C) 987654321123456789
(D) 123456789
Answer: (B)
Explanation:
Since the stack data structure follows LIFO order. When we pop() items from stack, they are popped in reverse order of their insertion (or push()).
Share your thoughts in the comments
Please Login to comment...