14.5 Array Representation of Stacks
Earlier in this chapter, we discussed how a stack can be represented by a linked list. Since a stack is a last in, first out data structure, we coded the push (insert) and pop (delete) methods of the linked list to insert or delete at the beginning of the list. Linked lists offer the advantage of being expandable one object at a time, so we do not have to worry about running out of capacity.
However, if we know in advance that the number of objects on a stack will always be less than some maximum number, we can represent the stack using an array, which is easier to implement.
Table 14.6 shows the APIs of the push, pop, and peek methods for a stack implemented using an array.
To match the LIFO functionality ...
Get Java Illuminated, 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.