only with arrays, but also with linked nodes • Linked nodes are objects called “nodes” that contain a link (pointer) to another object and to another node
only two data fields 1. A variable (data) with an address to an object 2. A variable (next) with an address to the next node • See Node.java for more details on the data fields and methods
an object in order to call methods on it, otherwise get NullPointerException String name = new String("Nami"); name = null; int len = name.length(); //throws NullPointerException name null Nami
a driver program that tests the methods of the Node class • In order for you to understand how linked nodes work, you need to be able to draw the variables and objects created by each line of this code
• Five (5) nodes with String data are created and linked in a certain order • A for loop is used to loop through the 5 nodes, so you can see how the nodes have been linked together
with a Node class? • The Stack class has data field top, which points to the first node in the list • If the stack is empty, top is null • Push and pop to the top (head) of the linked nodes
the same interface for both the ArrayStack and LinkedStack • See LinkedStack.java • Note that the constructor and methods of the LinkedStack class are listed first, followed by a main() method to test the constructor and methods of the class
data field, we need a variable that contains the address of the first node in the linked list of nodes • If no nodes have been pushed to the stack, or all nodes have been popped off, then the variable top is null private Node<T> top = null;
linked list, what is the Big-O for each method? • If we look at each method, none of them have any looping • Therefore, methods empty(), peek(), pop(), and push() are all O(1)
task of the compiler is to check to see if the curly braces ({ and }) are balanced in your program • Of course, if the curly braces are not balanced, then your program cannot compile
to an already existing { (right brace) 1. Push each { to the stack 2. When a } is encountered, pop one { off the stack 3. By the end of the program file, each { has to be matched, so no more {’s should be on the stack • See BalancedBraces.java for an implementation of the algorithm
give you an idea how this program checks for balanced braces • A file such as this is balanced • {abc{def{}}jklm}n • If input file has too many braces on the left, the program will leave extra {’s on the stack • {{{{{{abc{def{}}jklm}n
must take two steps to evaluate an algebraic expression • Step #1: use a stack to convert infix expression to a postfix expression • Step #2: use a stack to evaluate the postfix expression
the postfix integer expression 2. Loop from the 0th position to the last position in the input string a. Isolate a single character string from the input string b. If the character string is an integer, push integer on the Integer stack
operator (*, /, %, +, -), pop integer2, then pop integer1 from the Integer stack, calculate the result of integer1 operator integer2, and push the result back on the Integer stack 3. After looping, the top of the Integer stack is the result
1.Do the assignment corresponding to this lecture 2.Email me any questions you may have about the material 3.Turn in the assignment before the next lecture