Topic 3 - Stack ADT Flashcards
Give examples of where LIFO is useful.
1) Backtracking in puzzles and games.
2) Browsers - to keep track of pages visited in a browser tab
3) Parenthesis - first closed parenthesis closes the previous open parenthesis
4) Undo operations - keeps track of most recent operations
5) Markup Languages - HMTL, XML, have formatting information (tags) as well as raw text - checks for matching tags
6) Stack calculators - to convert an infix expression to postfix, to make evaluation easier
7) Compilers - convert infix expressions to postfix, to make translation of high-level language such as Java or C to a lower level language easier
8) Call Stack (Runtime Stack) - stores information about the active subroutines of a computer program, used by runtime system when methods are invoked, for a method call / return processing
Every collection has a ___?
A set of operations that define how we interact with it, for example:
- add elements
- remove elements
- determine if the collection is empty
- determine the collection’s size
Define all the Stack Operations
Push: add an element at the top of the stack
Pop: remove the element at the top of the stack
Peek: examine the element at the top of the stack
isEmpty: determines whether the stack is empty
size: determines the number of elements in the stack
toString: returns a string representation of the stack
Define: Stack Abstract Data Type (Stack ADT)
it is a collection of data, together with the operations on that data - we just discussed the operations in previous slides
Java has a programming construct called an Interface that___?
that we use to formally define what the operations on a collection are in Java
Define Java Interface:
- A list of abstract methods and constant
- must be public
- constants must be declared as final static
Define Abstract Method
A method that does not have an implementation ie. it just consists of the header of the method. ex:
return type method name (parameter list)
Define: Infix Notation
Operators are between operands: 3+4*2
Parantheses force precedence: (3+4)*2
Define: postfix epxression
The operator comes after its two operands
Does an application program need to know how the Stack collection is implemented?
No - we are using the Stack collection for its functionality (what); how it is implemented is not relevant
The class ArrayStack implements______?
the StackADT interface: public class ArrayStack implements StackADT
constructors are not specified in the Java interface for the StackADT (why not?)
BecauseThe elements of the stack array are of generic type T
pop() does what?
Returns value of and deletes top element
Can we instantiate anything of a generic type? Ie. create a constructor that creates an instance of a generic type? If, yes, explain. If no, then what do we do instead.
No.
We need to instantiate an element of type Object and cast it into the type T
In a Java interface, can we have attributes? Constants? How do you declare each if yes.
No. Yes. Constants must be declared as final static