Upgrade to Pro — share decks privately, control downloads, hide ads and more …

lecture14.pdf

Avatar for William Albritton William Albritton
September 14, 2014
81

 lecture14.pdf

Avatar for William Albritton

William Albritton

September 14, 2014
Tweet

Transcript

  1. Linked Nodes • Many data structures can be implemented not

    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
  2. Linked Nodes • A node is a class which has

    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
  3. Variables and Objects • A variable for an object contains

    an address that points to the object’s data • A box pointing to the data String name = new String("Nami"); name Nami
  4. Potential Bug • Your variable must contain an address to

    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
  5. Example Code • See Node.java • The main() method contains

    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
  6. Example Code • Declare variables that can point to objects

    of class Node with a data field that will point to String objects Node<String> head, tail; head tail null null
  7. Example Code • Instantiate String objects String fruit1 = new

    String("apple"); String fruit2 = new String("banana"); head fruit1 tail fruit2 null null apple banana
  8. Example Code • Instantiate Node objects head=new Node<String>(fruit1, null); tail=new

    Node<String>(fruit2, null); head fruit1 tail fruit2 apple banana data next null data next null
  9. Example Code • Link the two nodes together head.setNext(tail); head

    fruit1 tail fruit2 apple banana data next data next null
  10. Example Code • Point to first node and get its

    data Node<String> pointer = head; String fruitX = pointer.getData(); pointer fruitX head fruit1 tail fruit2 apple banana data next data next null
  11. Example Code • Point to next node and get its

    data pointer = pointer.getNext(); fruitX = pointer.getData(); pointer fruitX head fruit1 tail fruit2 apple banana data next data next null
  12. Example Code • See the main() method of program Node.java

    • 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
  13. Linked Stack • How do we implement a Stack class

    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
  14. Example Code • See StackInterface.java • Note that we use

    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
  15. Linked Stack • See file LinkedStack.java • For the private

    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;
  16. Common Misunderstanding • top is NOT an instance of a

    Node! • top is a variable that stores a pointer (address) to an object • top stores the location (address) of 1st node on the stack
  17. Example Code • Instantiate a String, push to stack String

    letter = new String("A"); stack.push(letter); letter stack top A data next null
  18. Example Code • Instantiate a String, push to stack letter

    = new String("B"); stack.push(letter); letter stack top B data next A data next null
  19. Example Code • Instantiate a String, push to stack letter

    = new String("C"); stack.push(letter); letter stack top C data next B data next A data next null
  20. Example Code • Instantiate a String, push to stack letter

    = new String("D"); stack.push(letter); letter stack top D data next C data next B data next A data next null
  21. Example Code • Pop a String off the stack letter

    = stack.pop(); letter stack top D C data next B data next A data next null
  22. Example Code • Peek (do not remove) the top of

    stack letter = stack.peek(); letter stack top C data next B data next A data next null
  23. Example Code • Pop a String off the top of

    stack letter = stack.pop(); letter stack top C B data next A data next null
  24. Example Code • Pop a String off the top of

    stack letter = stack.pop(); letter stack top B A data next null
  25. Example Code • Pop last String off the top of

    stack, so stack is empty again letter = stack.pop(); letter stack top null A
  26. Big-O Break • When we implement the stack with a

    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)
  27. Balanced Curly Braces • When you run a compiler, one

    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
  28. Balanced Curly Braces • Algorithm matches each } (left brace)

    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
  29. Balanced Curly Braces • Here are some simplified examples to

    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
  30. Balanced Curly Braces • If an input file has too

    many braces on the right, the program will try to pop {’s from an empty stack, and throw an EmptyStackException exception • {abc{def{}}jklm}n}}}}
  31. Algebraic Expressions • The Java compiler (or any computer program)

    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
  32. Postfix Evaluation Algorithm 1. Input is a string which contains

    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
  33. Postfix Evaluation Algorithm c. If the character string is an

    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
  34. Postfix Evaluation Trace • Input: "345*+" • Loop: 0 •

    Character: 3 • Stack: 3 • Loop: 1 • Character: 4 • Stack: 4, 3 • Loop: 2 • Character: 5 • Stack: 5, 4, 3 • Loop: 3 • Character: * • Integer2: 5 • Integer1: 4 • Calculate: 4*5 • Stack: 20, 3
  35. Postfix Evaluation Trace • Input: "345*+" • Loop: 4 •

    Character: + • Integer2: 20 • Integer1: 3 • Calculate: 3+20 • Stack: 23 • The result is the top of the stack, integer 23 • Loop: 3 • Character: * • Integer2: 5 • Integer1: 4 • Calculate: 4*5 • Stack: 20, 3
  36. Postfix Evaluation Trace II • Input: "34+5-" • Loop: 0

    • Character: 3 • Stack: 3 • Loop: 1 • Character: 4 • Stack: 4, 3 • Loop: 2 • Character: + • Integer2: 4 • Integer1: 3 • Calculate: 3+4 • Stack: 7
  37. Postfix Evaluation Trace II • Loop: 2 • Character: +

    • Integer2: 4 • Integer1: 3 • Calculate: 3+4 • Stack: 7 • Loop: 3 • Character: 5 • Stack: 5, 7 • Input: "34+5-" • Loop: 4 • Character: - • Integer2: 5 • Integer1: 7 • Calculate: 7-5 • Stack: 2 • Result is top of stack, integer 2
  38. Assignment • Your assignment is to write a program that

    reads an input file with several lines of postfix expressions, evaluates the postfix expressions, and displays the results in the output window
  39. Integer Division  Division operator (/) • Divides two integers

    • NO rounding up!! int two = 11/4; //2, NOT 2.75
  40. Integer Division • Integer division “cuts-off” digits after the decimal

    point (does not round up) • int i = 1/3; // i==0 • i = 2/3; // i==0 • i = 3/3; // i==1 • i = 4/3; // i==1 • i = 5/3; // i==1 • i = 6/3; // i==2
  41. Modulus Operator  Modulus operator (%) • Remainder of the

    division of two integers int three = 11%4; //3
  42. Modulus Operator • % is the modulus operator, which returns

    the remainder of a division • int i = 4 % 5; //4 • i = 9 % 5; //4 • i = 19 % 6; //1 • i = 0 % 6; //0 • i = 1 % 7 //1 • i = 7 % 7 //0 • i = 50 % 7 //1
  43. Task Manager • Before the next class, you need to:

    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