Download presentation
Presentation is loading. Please wait.
Published byJoanna Clark Modified over 9 years ago
1
13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11
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
13 X 11 Linked Lists Nodes in Java Amazingly similar to a cons cell Stripped down version: class Node { Object data; Node next; }
4
13 X 11 data next Node ref data next data next Some Object A Node Object
5
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; }
6
13 X 11 Node (continued) public String toString() { return "Node: " + data; }
7
13 X 11 Pop Quiz public String toString() { return "Node: " + data+ " Next:\n" + next; } What does it do?
8
13 X 11 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
13 X 11 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
13 X 11 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
13 X 11 Test Main Output Test5 Node: Bob, Next: Node: Carol, Next: Node: Ted, Next: Node: and Alice. Next: null
12
13 X 11 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
13 X 11 Test Main Output Test6 Node: Larry, Next: Node: *null* Next: Node: Moe, Next: Node: and Curly. Next: null
14
13 X 11 Questions?
15
13 X 11
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
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 isEmpty! –implementation of behaviors
18
13 X 11 isEmpty Returns a boolean Something like: return (head == null);
19
13 X 11 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
13 X 11 Enqueue(Object newData) 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
21
13 X 11 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
22
13 X 11 Enqueue(newData): isEmpty() head tail Queue Object
23
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
24
13 X 11 Enqueue(newData): isEmpty() head tail Queue Object data next newData temp
25
13 X 11 Enqueue(newData): isEmpty() head tail Queue Object data next newData temp
26
13 X 11 Enqueue(newData): isEmpty() head tail Queue Object data next newData temp
27
13 X 11 Enqueue(newData): isEmpty() head tail Queue Object data next newData
28
13 X 11 Questions?
29
13 X 11 Enqueue(newData): ! isEmpty() head tail Queue Object data next
30
13 X 11 Enqueue(newData): ! isEmpty() head tail Queue Object data next data next newData temp
31
13 X 11 Enqueue(newData): ! isEmpty() head tail Queue Object data next data next newData temp
32
13 X 11 Enqueue(newData): ! isEmpty() head tail Queue Object data next data next newData temp
33
13 X 11 Enqueue(newData): ! isEmpty() head tail Queue Object data next data next newData
34
13 X 11 Questions?
35
13 X 11 Enqueue(newData): ! isEmpty() head tail Queue Object data next data next
36
13 X 11 Enqueue(newData): ! isEmpty() head tail Queue Object data next data next temp data next newData
37
13 X 11 Enqueue(newData): ! isEmpty() head tail Queue Object data next data next temp data next newData
38
13 X 11 Enqueue(newData): ! isEmpty() head tail Queue Object data next data next temp data next newData
39
13 X 11 Enqueue(newData): ! isEmpty() head tail Queue Object data next data next data next newData
40
13 X 11 Questions?
41
13 X 11 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
13 X 11 Almost done! Now Dequeue 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
43
13 X 11 Red Dequeue() head tail Queue Object data next data next Green data next Blue
44
13 X 11 Red Dequeue() head tail Queue Object data next data next Green data next Blue retval
45
13 X 11 Red Dequeue() head tail Queue Object data next data next Green data next Blue retval
46
13 X 11 Red Dequeue() head tail Queue Object data next Green data next Blue retval
47
13 X 11 Red Dequeue() head tail Queue Object data next Green data next Blue retval Return
48
13 X 11 Dequeue() head tail Queue Object data next Green data next Blue
49
13 X 11 Questions?
50
13 X 11 Dequeue() head tail Queue Object data next Green data next Blue
51
13 X 11 Dequeue() head tail Queue Object data next Green data next Blue retval
52
13 X 11 Dequeue() head tail Queue Object data next Green data next Blue retval
53
13 X 11 Dequeue() head tail Queue Object Green data next Blue retval Return
54
13 X 11 Dequeue() head tail Queue Object data next Blue
55
13 X 11 Questions?
56
13 X 11 Dequeue() head tail Queue Object data next Blue
57
13 X 11 Dequeue() head tail Queue Object data next Blue retval
58
13 X 11 Dequeue() head tail Queue Object data next Blue retval
59
13 X 11 Dequeue() head tail Queue Object data next Blue retval isEmpty???
60
13 X 11 Dequeue() head tail Queue Object data next Blue retval
61
13 X 11 Dequeue() head tail Queue Object Blue retval
62
13 X 11 Dequeue() head tail Queue Object Blue retval Return
63
13 X 11 Dequeue() head tail Queue Object
64
13 X 11 Dequeue Code public Object dequeue() { Object retval; if(isEmpty()) { retval = null; } else { retval = head.data; head = head.next; if(isEmpty()) { tail = null; } return retval; }
65
13 X 11 Test Main public static void main(String args[]) { Queue q; q = new Queue(); q.enqueue("yada1"); q.enqueue("yada2"); q.enqueue("yada3"); while(! q.isEmpty()) { System.out.println(q.dequeue()); } } // main } // Queue
66
13 X 11 Questions?
67
13 X 11 Over break... Rewrite class Node with –private head and tail –getHead and setHead methods –getNext and setNext methods Rewrite class Queue using your new Node Build a working scale model of the Three-Mile Island Power Plant Have a Merry Christmas!
68
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
69
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
70
13 X 11 What you should know about now object/instance Inheritance –Redefinition (Overriding) –Extension –super class –subclass –abstract Polymorphism Compilation –reference type checking –method checking –Type mismatch checking Run Time –interpreting –dynamic binding –Java virtual machine
71
13 X 11
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.