1 / 71

Java Lecture 6

Java Lecture 6. CS 1311X Self-Referential Structures Building a Queue. 13 X 11. Self-Referential?. Simply means that a class has a reference to an object of that class Common applications Linked list nodes Binary tree nodes. Linked Lists Nodes in Java. Amazingly similar to a cons cell

keitaro-rin
Télécharger la présentation

Java Lecture 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13X11

  2. Self-Referential? • Simply means that a class has a reference to an object of that class • Common applications • Linked list nodes • Binary tree nodes

  3. Linked Lists Nodes in Java • Amazingly similar to a cons cell • Stripped down version: class Node { Object data; Node next; }

  4. data data data next next next A Node Object Some Object Some Object Node ref

  5. More useful? class Node { public Object data; public Node next; public Node(Object data) { this(data, null); } public Node(Object data, Node next) { this.data = data; this.next = next; }

  6. Node (continued) public String toString() { return "Node: " + data; }

  7. Pop Quiz public String toString() { return "Node: " + data + " Next:\n" + next; } What does it do?

  8. Test Main public static void main(String args[]) { Node n1 = new Node("Hello "); Node n2 = new Node("World!")' n1.next = n2; System.out.println("Test1\n" + n1); System.out.println("Test2\n" + n2); Node nz = new Node("and Nod."); Node ny = new Node("Blynken ", nz); Node nx = new Node("Wynken, ", ny); System.out.println("Test3\n" + nx); ny = null; nz = null; System.out.println("Test4\n" + nx);

  9. Test Main Output Test1 Node: Hello Next: Node: World! Next: null Test2 Node: World! Next: null Test3 Node: Wynken, Next: Node: Blynken Next: Node: and Nod. Next: null Test4 Node: Wynken, Next: Node: Blynken Next: Node: and Nod. Next: null

  10. Test Main Node head = new Node("Bob,", new Node("Carol, ", new Node("Ted, ", new Node("and Alice.")))); System.out.println("Test5\n" + head); Scheme like construction

  11. Test Main Output Test5 Node: Bob, Next: Node: Carol, Next: Node: Ted, Next: Node: and Alice. Next: null

  12. Test Main Node list = new Node("Larry,", new Node(null, new Node("Moe, ", new Node("and Curly.")))); System.out.println("Test6\n" + list); } // main } // Node Another Scheme like construction

  13. Test Main Output Test6 Node: Larry, Next: Node: *null* Next: Node: Moe, Next: Node: and Curly. Next: null

  14. Questions?

  15. C'mon, Kids! Let's build a Queue!

  16. So What's a Queue? • First-In First-Out Data Structure • British word for line (Queue up for a pint.) • French word for tail (Like a horse's tail). • Multiple ways to implement • Common to use Linked list • etc. • Typical behaviors • isEmpty • enqueue • dequeue • head (or front or top or peek)

  17. Linked List Implementation • Can use our Node class • Will need another class with a catchy name like Queue • What's in the Queue class? • head pointer (reference) • tail pointer (reference) • Note: If head == null then tail == null (and vice versa) and the Queue isEmpty! • implementation of behaviors

  18. isEmpty • Returns a boolean • Something like: return (head == null);

  19. Let's write some code! class Queue { private Node head; private Node tail; public Queue() { head = null; tail = null; } public boolean isEmpty() { return (head == null); }

  20. Case: isEmpty() Create a new Node data points to newData Make head and tail point to the new Node Case: ! isEmpty() Create a new Node data points to newData Make the old tail Node point to the new Node Make the tail pointer point to the new Node Enqueue(Object newData)

  21. Create a new Node data points to newData Case: isEmpty(); Make head and tail point to the new Node Create a new Node data points to newData Case: ! isEmpty() Make the old tail Node point to the new Node Make the tail pointer point to the new Node Enqueue(Object newData)

  22. Enqueue(newData): isEmpty() Queue Object head tail

  23. Nota Bene In the case of a Queue we will always make new Nodes with the next reference set equal to null

  24. data next Enqueue(newData): isEmpty() newData Queue Object temp head tail

  25. data next Enqueue(newData): isEmpty() newData Queue Object temp head tail

  26. data next Enqueue(newData): isEmpty() newData Queue Object temp head tail

  27. data next Enqueue(newData): isEmpty() newData Queue Object head tail

  28. Questions?

  29. data next Enqueue(newData): ! isEmpty() Queue Object head tail

  30. data data next next Enqueue(newData): ! isEmpty() newData Queue Object temp head tail

  31. data data next next Enqueue(newData): ! isEmpty() newData Queue Object temp head tail

  32. data data next next Enqueue(newData): ! isEmpty() newData Queue Object temp head tail

  33. data data next next Enqueue(newData): ! isEmpty() newData Queue Object head tail

  34. Questions?

  35. data data next next Enqueue(newData): ! isEmpty() Queue Object head tail

  36. data data data next next next Enqueue(newData): ! isEmpty() Queue Object newData head tail temp

  37. data data data next next next Enqueue(newData): ! isEmpty() Queue Object newData head tail temp

  38. data data data next next next Enqueue(newData): ! isEmpty() Queue Object newData head tail temp

  39. data data data next next next Enqueue(newData): ! isEmpty() Queue Object newData head tail

  40. Questions?

  41. Let's write some more code! public void enqueue(Object o) { Node temp; temp = new Node(o); if(isEmpty()) { head = temp; tail = temp; } else // Queue is not empty... { tail.next = temp; tail = temp; } }

  42. Assume ! isEmpty we'll check Save value that head data reference is pointing to (return value) Make head pointer point to whatever first node's next is pointing to... Case: head is not null Assume ! isEmpty we'll check Save value that head data reference is pointing to (return value) Make head pointer point to whatever first node's next is pointing to... Case: head is null Set tail to null Almost done! Now Dequeue

  43. data data data next next next Dequeue() Red Green Queue Object Blue head tail

  44. data data data next next next Dequeue() retval Red Green Queue Object Blue head tail

  45. data data data next next next Dequeue() retval Red Green Queue Object Blue head tail

  46. data data next next Dequeue() retval Red Green Queue Object Blue head tail

  47. data data next next Dequeue() Return retval Red Green Queue Object Blue head tail

  48. data data next next Dequeue() Green Queue Object Blue head tail

  49. Questions?

  50. data data next next Dequeue() Green Queue Object Blue head tail

More Related