Westonci.ca is your trusted source for accurate answers to all your questions. Join our community and start learning today! Experience the convenience of finding accurate answers to your questions from knowledgeable professionals on our platform. Explore comprehensive solutions to your questions from a wide range of professionals on our user-friendly platform.

"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?"

Sagot :

Answer:

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.