CS 2114 Chapter Notes - Chapter 5: Operand, Subsequence, Java Class Library
Document Summary
The adt stack follows a last-in, first-out or lifo behavior such that all additions are added to the top. The top entry is always the newest item on the stack, therefore, restricting access to lower entries. However, the client is able to look at or remove the top entry. Adds a new entry to the top of the. Retrieves the stack"s top entry without changing the stack at all. Since the methods pop() and peek() are public methods, we cannot trust the client to honor any preconditions (i. e. what should happen when the stack is empty). Returning null can be dangerous since the client may not know whether it is a null data entry or an empty adt. Throwing an exception is the best option: example: demonstrating the stack methods the first section prints (cid:498)joe(cid:499) before removing him; the second section prints (cid:498)jill(cid:499) before removing her.