Java Lecture 6 CS 1311 X SelfReferential Structures
































































![Test Main public static void main(String args[]) { Queue q; q = new Queue(); Test Main public static void main(String args[]) { Queue q; q = new Queue();](https://slidetodoc.com/presentation_image_h2/67da920d495fd4e75b8b35078a50bf9a/image-65.jpg)






- Slides: 71
Java Lecture 6 CS 1311 X 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 13 X 11
Linked Lists Nodes in Java • Amazingly similar to a cons cell • Stripped down version: class Node { Object data; Node next; } 13 X 11
Some Object Node ref data next A Node Object 13 X 11
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; } 13 X 11
Node (continued) public String to. String() { return "Node: " + data; } 13 X 11
Pop Quiz public String to. String() { return "Node: " + data + " Next: n" + next; } What does it do? 13 X 11
Test Main public static { Node n 1 Node n 2 n 1. next void main(String args[]) = new Node("Hello "); = new Node("World!")' = n 2; System. out. println("Test 1n" + n 1); System. out. println("Test 2n" + n 2); Node nz = new Node("and Nod. "); Node ny = new Node("Blynken ", nz); Node nx = new Node("Wynken, ", ny); System. out. println("Test 3n" + nx); ny = null; nz = null; System. out. println("Test 4n" + nx); 13 X 11
Test Main Output Test 1 Node: null Test 2 Node: null Test 3 Node: null Test 4 Node: null Hello Next: World! Next: Wynken, Next: Blynken Next: and Nod. Next: 13 X 11
Test Main Node head = new Node("Bob, ", new Node("Carol, ", new Node("Ted, ", new Node("and Alice. ")))); Scheme like construction System. out. println("Test 5n" + head); 13 X 11
Test Main Output Test 5 Node: null Bob, Next: Carol, Next: Ted, Next: and Alice. Next: 13 X 11
Test Main Node list = new Node("Larry, ", new Node(null, new Node("Moe, ", new Node("and Curly. ")))); Another Scheme like construction System. out. println("Test 6n" + list); } // main } // Node 13 X 11
Test Main Output Test 6 Node: null Larry, Next: *null* Next: Moe, Next: and Curly. Next: 13 X 11
Questions? 13 X 11
13 X 11
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 – – is. Empty enqueue dequeue head (or front or top or peek) 13 X 11
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 is. Empty! – implementation of behaviors 13 X 11
is. Empty • Returns a boolean • Something like: return (head == null); 13 X 11
Let's write some code! class Queue { private Node head; private Node tail; public Queue() { head = null; tail = null; } public boolean is. Empty() { return (head == null); } 13 X 11
Enqueue(Object new. Data) • Case: is. Empty() • Case: ! is. Empty() • Create a new Node – data points to new. Data • Make head and tail point to the new Node • Make the old tail Node point to the new Node • Make the tail pointer point to the new Node 13 X 11
Enqueue(Object new. Data) • Create a new Node – data points to new. Data • Case: is. Empty(); • Case: ! is. Empty() • Make head and tail point to the new Node • Make the old tail Node point to the new Node • Make the tail pointer point to the new Node 13 X 11
Enqueue(new. Data): is. Empty() Queue Object head tail 13 X 11
Nota Bene In the case of a Queue we will always make new Nodes with the next reference set equal to null 13 X 11
Enqueue(new. Data): is. Empty() Queue Object head new. Data temp data next tail 13 X 11
Enqueue(new. Data): is. Empty() Queue Object head new. Data temp data next tail 13 X 11
Enqueue(new. Data): is. Empty() Queue Object head new. Data temp data next tail 13 X 11
Enqueue(new. Data): is. Empty() new. Data Queue Object head data next tail 13 X 11
Questions? 13 X 11
Enqueue(new. Data): ! is. Empty() Queue Object head data next tail 13 X 11
Enqueue(new. Data): ! is. Empty() new. Data Queue Object temp head data next tail 13 X 11
Enqueue(new. Data): ! is. Empty() new. Data Queue Object temp head data next tail 13 X 11
Enqueue(new. Data): ! is. Empty() new. Data Queue Object temp head data next tail 13 X 11
Enqueue(new. Data): ! is. Empty() new. Data Queue Object head data next tail 13 X 11
Questions? 13 X 11
Enqueue(new. Data): ! is. Empty() Queue Object head data next tail 13 X 11
Enqueue(new. Data): ! is. Empty() Queue Object head data next new. Data data tail next temp 13 X 11
Enqueue(new. Data): ! is. Empty() Queue Object head data next new. Data data tail next temp 13 X 11
Enqueue(new. Data): ! is. Empty() Queue Object head data next new. Data data tail next temp 13 X 11
Enqueue(new. Data): ! is. Empty() Queue Object head data next new. Data data tail next 13 X 11
Questions? 13 X 11
Let's write some more code! public void enqueue(Object o) { Node temp; temp = new Node(o); if(is. Empty()) { head = temp; tail = temp; } else // Queue is not empty. . . { tail. next = temp; tail = temp; } } 13 X 11
Almost done! Now Dequeue • Assume ! is. Empty – 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 ! is. Empty – 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 13 X 11
Dequeue() Red Queue Object head Green data next Blue data tail next 13 X 11
Dequeue() retval Queue Object head Red Green data next Blue data tail next 13 X 11
Dequeue() retval Queue Object head Red Green data next Blue data tail next 13 X 11
Dequeue() retval Queue Object head Red Green data Blue next data tail next 13 X 11
Dequeue() retval Queue Object head rn u t Re Red Green data Blue next data tail next 13 X 11
Dequeue() Green Queue Object head data Blue next data tail next 13 X 11
Questions? 13 X 11
Dequeue() Green Queue Object head data Blue next data tail next 13 X 11
Dequeue() retval Green Queue Object head data Blue next data tail next 13 X 11
Dequeue() retval Green Queue Object head data Blue next data tail next 13 X 11
retval Queue Object head tur n Re Dequeue() Green Blue data tail next 13 X 11
Dequeue() Queue Object head Blue data tail next 13 X 11
Questions? 13 X 11
Dequeue() Queue Object head Blue data tail next 13 X 11
Dequeue() retval Queue Object head Blue data tail next 13 X 11
Dequeue() retval Queue Object head Blue data tail next 13 X 11
Dequeue() retval Queue Object Blue head data tail next is. Empty? ? ? 13 X 11
Dequeue() retval Queue Object head Blue data tail next 13 X 11
Dequeue() retval Queue Object head Blue tail 13 X 11
head tur Queue Object Re retval n Dequeue() Blue tail 13 X 11
Dequeue() Queue Object head tail 13 X 11
Dequeue Code public Object dequeue() { Object retval; if(is. Empty()) { retval = null; } else { retval = head. data; head = head. next; if(is. Empty()) { tail = null; } } return retval; } 13 X 11
Test Main public static void main(String args[]) { Queue q; q = new Queue(); q. enqueue("yada 1"); q. enqueue("yada 2"); q. enqueue("yada 3"); while(! q. is. Empty()) { System. out. println(q. dequeue()); } } // main } // Queue 13 X 11
Questions? 13 X 11
Over break. . . • Rewrite class Node with – private head and tail – get. Head and set. Head methods – get. Next and set. Next methods • Rewrite class Queue using your new Node • Build a working scale model of the Three-Mile Island Power Plant • Have a Merry Christmas! 13 X 11
What you should know about now • Syntax – – Operators Operator overloading Assignment statements Control structures • if else • case – Iterative structures • while • do while • for • Data Types – Primitives – References • class • attribute – access modifiers • public/private – static – final/constants – initialization 13 X 11
What you should know about now • constructors – – access modifiers default chaining overloading • methods – access modifiers • public/private – – – static return type/void main method accessors modifiers overloading 13 X 11
What you should know about now • object/instance • Inheritance – – – Redefinition (Overriding) Extension super class subclass abstract • Compilation – reference type checking – method checking – Type mismatch checking • Run Time – interpreting – dynamic binding – Java virtual machine • Polymorphism 13 X 11
13 X 11