+ All Categories
Home > Documents > 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

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

Date post: 25-Dec-2015
Category:
Upload: joanna-clark
View: 214 times
Download: 0 times
Share this document with a friend
71
13X11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13X11
Transcript
Page 1: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Java Lecture 6

CS 1311X

Self-Referential Structures

Building a Queue

13X11

Page 2: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

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

Page 3: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Linked Lists Nodes in Java

• Amazingly similar to a cons cell

• Stripped down version:

class Node

{

Object data;

Node next;

}

Page 4: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

data

next

Node ref

data

next

data

next

Some Object Some Object

A Node ObjectA Node Object

Page 5: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

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;

}

Page 6: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Node (continued)public String toString(){

return "Node: " + data;}

Page 7: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Pop Quizpublic String toString(){

return "Node: " + data + " Next:\n" + next;}

What does it do?What does it do?

13X11

data

next

Node ref

data

next

data

next

Some Object Some Object

Page 8: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Test Mainpublic 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);

Page 9: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Test Main OutputTest1Node: Hello Next:Node: World! Next:nullTest2Node: World! Next:nullTest3Node: Wynken, Next:Node: Blynken Next:Node: and Nod. Next:nullTest4Node: Wynken, Next:Node: Blynken Next:Node: and Nod. Next:null

13X11

Test Mainpublic 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);

Page 10: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

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

Page 11: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Test Main OutputTest5Node: Bob, Next:Node: Carol, Next:Node: Ted, Next:Node: and Alice. Next:null

13X11

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

Page 12: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

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

Page 13: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Test Main Output

Test6Node: Larry, Next:Node: *null* Next:Node: Moe, Next:Node: and Curly. Next:null

13X11

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

Page 14: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Questions?

Page 15: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Page 16: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

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)

Page 17: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

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

Page 18: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

isEmpty

• Returns a boolean

• Something like:

return (head == null);

Page 19: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

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);}

Page 20: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

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

Page 21: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

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

Page 22: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Enqueue(newData): isEmpty()

head

tail

Queue Object

Page 23: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Nota Bene

In the case of a Queue we will always make new Nodes with the next

reference set equal to null

In the case of a Queue we will always make new Nodes with the next

reference set equal to null

13X11

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;

}

Page 24: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Enqueue(newData): isEmpty()

head

tail

Queue Object

data

next

newData

temp

Page 25: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Enqueue(newData): isEmpty()

head

tail

Queue Object

data

next

newData

temp

Page 26: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Enqueue(newData): isEmpty()

head

tail

Queue Object

data

next

newData

temp

Page 27: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Enqueue(newData): isEmpty()

head

tail

Queue Object

data

next

newData

Page 28: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Questions?

Page 29: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Enqueue(newData): ! isEmpty()

head

tail

Queue Object

data

next

Page 30: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Enqueue(newData): ! isEmpty()

head

tail

Queue Object

data

next

data

next

newData

temp

Page 31: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Enqueue(newData): ! isEmpty()

head

tail

Queue Object

data

next

data

next

newData

temp

Page 32: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Enqueue(newData): ! isEmpty()

head

tail

Queue Object

data

next

data

next

newData

temp

Page 33: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Enqueue(newData): ! isEmpty()

head

tail

Queue Object

data

next

data

next

newData

Page 34: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Questions?

Page 35: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Enqueue(newData): ! isEmpty()

head

tail

Queue Object

data

next

data

next

Page 36: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Enqueue(newData): ! isEmpty()

head

tail

Queue Object

data

next

data

next

temp

data

next

newData

Page 37: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Enqueue(newData): ! isEmpty()

head

tail

Queue Object

data

next

data

next

temp

data

next

newData

Page 38: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Enqueue(newData): ! isEmpty()

head

tail

Queue Object

data

next

data

next

temp

data

next

newData

Page 39: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Enqueue(newData): ! isEmpty()

head

tail

Queue Object

data

next

data

next

data

next

newData

Page 40: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Questions?

Page 41: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

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;

}}

Page 42: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

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

Page 43: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Red

Dequeue()

head

tail

Queue Object

data

next

data

next

Green

data

next

Blue

Page 44: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Red

Dequeue()

head

tail

Queue Object

data

next

data

next

Green

data

next

Blue

retval

Page 45: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Red

Dequeue()

head

tail

Queue Object

data

next

data

next

Green

data

next

Blue

retval

Page 46: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Red

Dequeue()

head

tail

Queue Object

data

next

Green

data

next

Blue

retval

Page 47: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Red

Dequeue()

head

tail

Queue Object

data

next

Green

data

next

Blue

retval

Return

Page 48: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Dequeue()

head

tail

Queue Object

data

next

Green

data

next

Blue

Page 49: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Questions?

Page 50: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Dequeue()

head

tail

Queue Object

data

next

Green

data

next

Blue

Page 51: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Dequeue()

head

tail

Queue Object

data

next

Green

data

next

Blue

retval

Page 52: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Dequeue()

head

tail

Queue Object

data

next

Green

data

next

Blue

retval

Page 53: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Dequeue()

head

tail

Queue ObjectGreen

data

next

Blue

retval

Return

Page 54: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Dequeue()

head

tail

Queue Object

data

next

Blue

Page 55: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Questions?

Page 56: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Dequeue()

head

tail

Queue Object

data

next

Blue

Page 57: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Dequeue()

head

tail

Queue Object

data

next

Blue

retval

Page 58: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Dequeue()

head

tail

Queue Object

data

next

Blue

retval

Page 59: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Dequeue()

head

tail

Queue Object

data

next

Blue

retval

isEmpty???

Page 60: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Dequeue()

head

tail

Queue Object

data

next

Blue

retval

Page 61: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Dequeue()

head

tail

Queue Object

Blue

retval

Page 62: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Dequeue()

head

tail

Queue Object

Blue

retval

Return

Page 63: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Dequeue()

head

tail

Queue Object

Page 64: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Dequeue Codepublic Object dequeue() {

Object retval;if(isEmpty()){

retval = null;}else{

retval = head.data;head = head.next;if(isEmpty()){

tail = null;}

}return retval;

}

Page 65: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Test Mainpublic 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

Page 66: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

Questions?

Page 67: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

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!

Page 68: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

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

Page 69: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

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

Page 70: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11

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

Page 71: 13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.

13X11


Recommended