225,359 views
25 votes
25 votes
"Why do the linked stack and queue implementations not include iterators, as in the linked list classes of Chapter 16? Are there cases in which a stack or queue might need to be traversed privately, even if access cannot be provided publicly? If so, should an iterator be used for these cases?"

User Lajja Thaker
by
3.0k points

1 Answer

14 votes
14 votes

Answer:

Step-by-step explanation:

A linked stack is a LIFO (last in, first out) while a queue is a FIFO (first in, first out) data structure. This means that you can only access and modify the first or last elements of the stack or queue repectively. Therefore, there is no use for iterators in such a data structure which is why they do not exist. Iterators are mainly for traversing sequential access or random access within the given data structure which is not possible with these data structures, but is possible with lists. A stack or queue would either be made automatic so that it is completely private or simply made public so that it can be accessed by other methods and classes, regardless iterators would not be used.

User Ashish Rajput
by
2.4k points