In a stack, if a user tries to remove an element from empty stack it is called _________ a) underflow b) empty collection c) overflow d) garbage collection
Answers
Answer:
a) underflow.
Explanation:
It is called Stack underflow.
In a stack, if a user tries to remove an element from an empty stack it is called underflow.
a) underflow
What is Stack and what do the terms underflow, empty collection, overflow, and garbage collection mean?
Stack is a collection of elements that are arranged in linear form. The two main operations which can be performed on a stack are push and pop.
The push operation adds an element to the collection whereas the pop operation removes the last added element from the collection.
Let's understand Stack through an example
Suppose there are 5 plates and each plate is named A, B, C, D, E respectively.
We kept plate A on the surface of the floor and on that plate A we kept plate B and then we kept plate C on plate B and so on. So, this will be our Stack of plates.
E
D
C
B
A
Now, the first plate which we kept was A and the last plate which we kept was E.
If we see it from the view of Stack operations then we have applied push operation on each plate to add them one by one.
After this stack is created if we apply pop operation that is we have to remove a plate then we can only remove the plate which was the last inserted plate in the Stack i.e. plate E.
We applied the pop operation now the Stack became
D
C
B
A
If we apply pop again then the D plate will be removed or if we apply push operation then the E plate will be added again.
This is called the LIFO(Last In First Out)/FILO(First In Last Out) order which the Stack follows.
In Stack, the push and pop operations happen only from one side.
We can't push a new element or pop an element in between directly.
Suppose if we have to remove plate B from this Stack as given below.
E
D
C
B
A
To remove plate B you will have to apply pop operation four times.
One more example
There are some pop and push operations written we have to tell the resulting Stack.
push(A);
pop();
push(B);
push(C);
pop();
push(D);
pop();
push(E);
The resulting stack will be
E
B
Explanation
In an empty Stack, we pushed element A and then used pop operation which means A got popped out and Stack is again empty.
After that, we pushed B and then C and then used pop operation which means C got popped out as it was the last element added in Stack and B remains in Stack now too.
Now, we pushed D and then used pop operation and as D was the last element added in Stack it got popped out and B remains in Stack now too.
Last step we pushed E.
So, two elements remain in Stack that is E and B.
Underflow - Underflow condition happens when we try to remove an element from Stack.
Empty Collection - Empty Collection as the name says tells whether the stack is empty or not. empty() function is used for this.
Overflow - Overflow condition happens when we try to add an element in Stack although the Stack is full and can't add more elements in it due to the defined size limit.
Garbage Collection - Sometimes we allocate more space in memory than we actually need so, Garbage collection manages it and allocates that unused memory to other tasks.