710 likes | 818 Vues
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
E N D
Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13X11
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 • Stripped down version: class Node { Object data; Node next; }
data data data next next next A Node Object Some Object Some Object Node ref
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; }
Node (continued) public String toString() { return "Node: " + data; }
Pop Quiz public String toString() { return "Node: " + data + " Next:\n" + next; } What does it do?
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);
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
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
Test Main Output Test5 Node: Bob, Next: Node: Carol, Next: Node: Ted, Next: Node: and Alice. Next: null
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
Test Main Output Test6 Node: Larry, Next: Node: *null* Next: Node: Moe, Next: Node: and Curly. Next: null
C'mon, Kids! Let's build a Queue!
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)
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
isEmpty • Returns a boolean • Something like: return (head == null);
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); }
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)
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)
Enqueue(newData): isEmpty() Queue Object head tail
Nota Bene In the case of a Queue we will always make new Nodes with the next reference set equal to null
data next Enqueue(newData): isEmpty() newData Queue Object temp head tail
data next Enqueue(newData): isEmpty() newData Queue Object temp head tail
data next Enqueue(newData): isEmpty() newData Queue Object temp head tail
data next Enqueue(newData): isEmpty() newData Queue Object head tail
data next Enqueue(newData): ! isEmpty() Queue Object head tail
data data next next Enqueue(newData): ! isEmpty() newData Queue Object temp head tail
data data next next Enqueue(newData): ! isEmpty() newData Queue Object temp head tail
data data next next Enqueue(newData): ! isEmpty() newData Queue Object temp head tail
data data next next Enqueue(newData): ! isEmpty() newData Queue Object head tail
data data next next Enqueue(newData): ! isEmpty() Queue Object head tail
data data data next next next Enqueue(newData): ! isEmpty() Queue Object newData head tail temp
data data data next next next Enqueue(newData): ! isEmpty() Queue Object newData head tail temp
data data data next next next Enqueue(newData): ! isEmpty() Queue Object newData head tail temp
data data data next next next Enqueue(newData): ! isEmpty() Queue Object newData head tail
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; } }
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
data data data next next next Dequeue() Red Green Queue Object Blue head tail
data data data next next next Dequeue() retval Red Green Queue Object Blue head tail
data data data next next next Dequeue() retval Red Green Queue Object Blue head tail
data data next next Dequeue() retval Red Green Queue Object Blue head tail
data data next next Dequeue() Return retval Red Green Queue Object Blue head tail
data data next next Dequeue() Green Queue Object Blue head tail
data data next next Dequeue() Green Queue Object Blue head tail