Java Reference
In-Depth Information
figure 6.25
The stack model:
Input to a stack is by
push , output is by top ,
and deletion is by pop.
push
pop, top
Stack
1 // Stack protocol
2
3 package weiss.nonstandard;
4
5 public interface Stack<AnyType>
6 {
7 void push( AnyType x ); // insert
8 void pop( ); // remove
9 AnyType top( ); // find
10 AnyType topAndPop( ); // find + remove
11
12 boolean isEmpty( );
13 void makeEmpty( );
14 }
figure 6.26
Protocol for the stack
What makes the stack useful are the many applications for which we need to
access only the most recently inserted item. An important use of stacks is in com-
piler design.
6.6.2 stacks and computer languages
Compilers check your programs for syntax errors. Often, however, a lack of
one symbol (e.g., a missing comment-ender */ or } ) causes the compiler to
spill out a hundred lines of diagnostics without identifying the real error; this
is especially true when using anonymous classes.
A useful tool in this situation is a program that checks whether everything
is balanced, that is, every { corresponds to a } , every [ to a ] , and so on. The
sequence [()] is legal but [(]) is not—so simply counting the numbers of
each symbol is insufficient. (Assume for now that we are processing only a
sequence of tokens and will not worry about problems such as the character
constant '{' not needing a matching '}' .)
 
Search WWH ::




Custom Search