At the abstract level, according to how long as follows: Both to an element to the top a stack is an ordered they've been in the retrieve elements and of a stack is usually group of elements. The stack. The items that to assign new elements, called Push, and the removal of existing have been in the stack access only the top of operation that takes elements and the the longest are at the the stack. Operations the top element off the addition of new bottom; the most recent on Stacks The logical stack is referred to as elements can take place are at the top. At any picture of the Pop. We also must be only at the top of the time, given any two structure is only half able to tell whether a stack. For instance, if elements in a stack, the definition of a stack contains any your favorite blue one is higher than the class. The other half elements before we pop shirt is underneath a other. (For instance, is a set of operations it, so we need a faded, old, red one in the red shirt was that allows the user to Boolean operation a stack of shirts, you higher in the stack access and manipulate Empty. As a logical first must remove the than the blue shirt.) the elements stored in data structure, a stack red shirt (the top Because items are added the structure. Given is never conceptually element) from the and removed only from the abstract structure "full," but for a stack. Only then can the top of the stack, of a stack, what kinds particular you remove the desired the last element to be of operations do we implementation you may blue shirt, which is added is the first to need in order to use a need to test whether a now the top element in be removed. There is a stack? A stack is a stack is full before the stack. Then you may handy mnemonic to help dynamic structure; it pushing. We'll call replace the red shirt you remember this rule changes as elements are this Boolean operation on the top of the stack of stack behavior: A added to and removed Full. We also might or throw it away. The stack is a "last in, from it. When we begin want an operation that stack is considered an first out" (LIFO) using a stack, it destroys a stack, "ordered" group of structure. The should be empty getting rid of all the items because elements accessing operation of (contain no elements). elements left in it and occur in a sequence a stack is summarized The operation that adds leaving the stack empty. We'll call this Exceptions In addition stack package? Why not more meaningful error operation Clear. Figure to the stack operations just display an error message in this 6-2 shows how a stack, we discussed earlier, message? For example, situation would be envisioned as a stack this package contains instead of raising "There are no more of building blocks, is the declarations for UNDERFLOW when the Pop commands to undo." Part modified by several two exceptions. If a operation detects an of the task of writing Push and Pop stack is empty when we erroneous condition Pop a reusable component is operations. Next we try to pop an element could display an error to allow errors that must formalize the from it, the resulting message on the console are detected by the stack operations we error condition is such as Error! The component to be dealt have described. As with called stack underflow. stack is empty, you with at the appropriate our set and string The exception comments can't remove an item at level of abstraction. classes, we use a for procedure Pop this time. One problem Ada's exceptions are package specification indicate that the of this approach is the perfect tool for to accurately describe exception UNDERFLOW is that the error message this job. When the our class. We'll make raised if this we choose when we write stack package detects a this a generic package condition is detected. the stack package body problem, it raises an so that our stack Stack overflow is the probably has little exception that is abstraction can be condition resulting meaning in the context propagated back to the easily reused in from trying to push a of the client program client program to deal different applications. new element onto a that uses a stack. with. Exception We then can instantiate stack that is already Imagine the confusion propagation also stack packages for full, Procedure Push if the above message ensures that the error stacks containing any raises the exception were displayed when condition is not type of objects. Our OVERFLOW if this someone running a word ignored. If the client generic stack condition is detected. processing program has no handler for the specification is given Why bother declaring clicks their mouse on exception, it is in Specification 6-1. these exceptions in the the "undo" command. A propagated back to the operating system which that is, when no Application Level Now determine which ends execution of the exception is raised. let's look at an operation to carry out program and displays a The comment section example of how the first. You learned in (usually cryptic) error labeled Exceptions: operations in the stack your mathematics message. Exceptions and contains assertions package might be used classes that Postconditions that are true if the in a program. Since you multiplication is done Postconditions are package operation were in elementary before addition. You assertions that state terminates abnormally school you have written learned Ada's operator what results are to be by raising an arithmetic expressions precedence rules in expected at the exit of exception. Notice how using a format known as your first programming an operation or our exception comments infix notation. This class. In both procedure. Our style of specify exactly what same notation is used situations, we use commenting the state of the stack for writing arithmetic parentheses to override postconditions for is after the package expressions in Ada. The the normal ordering package operations is raises the exception. operator in an infix rules. It is easy to to divide the exit Other people prefer to expression is written make a mistake writing assertions into two have just a single in between its or interpreting an groups. The assertions postcondition comment operands. When an infix expression in the group labeled section that includes expression contains containing multiple Postconditions: are the exit assertions multiple operations nested sets of true if the operation found in both of our such as we need to use parentheses. That is terminates normally; comment sections. The a set of rules to the end of the story!