13 x 11 java lecture 6 cs 1311x self-referential structures building a queue 13 x 11

Post on 25-Dec-2015

216 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

13X11

Java Lecture 6

CS 1311X

Self-Referential Structures

Building a Queue

13X11

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

13X11

Linked Lists Nodes in Java

• Amazingly similar to a cons cell

• Stripped down version:

class Node

{

Object data;

Node next;

}

13X11

data

next

Node ref

data

next

data

next

Some Object Some Object

A Node ObjectA Node Object

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;

}

13X11

Node (continued)public String toString(){

return "Node: " + data;}

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

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

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

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

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

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

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

13X11

Questions?

13X11

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)

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

13X11

isEmpty

• Returns a boolean

• Something like:

return (head == null);

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

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

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

13X11

Enqueue(newData): isEmpty()

head

tail

Queue Object

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;

}

13X11

Enqueue(newData): isEmpty()

head

tail

Queue Object

data

next

newData

temp

13X11

Enqueue(newData): isEmpty()

head

tail

Queue Object

data

next

newData

temp

13X11

Enqueue(newData): isEmpty()

head

tail

Queue Object

data

next

newData

temp

13X11

Enqueue(newData): isEmpty()

head

tail

Queue Object

data

next

newData

13X11

Questions?

13X11

Enqueue(newData): ! isEmpty()

head

tail

Queue Object

data

next

13X11

Enqueue(newData): ! isEmpty()

head

tail

Queue Object

data

next

data

next

newData

temp

13X11

Enqueue(newData): ! isEmpty()

head

tail

Queue Object

data

next

data

next

newData

temp

13X11

Enqueue(newData): ! isEmpty()

head

tail

Queue Object

data

next

data

next

newData

temp

13X11

Enqueue(newData): ! isEmpty()

head

tail

Queue Object

data

next

data

next

newData

13X11

Questions?

13X11

Enqueue(newData): ! isEmpty()

head

tail

Queue Object

data

next

data

next

13X11

Enqueue(newData): ! isEmpty()

head

tail

Queue Object

data

next

data

next

temp

data

next

newData

13X11

Enqueue(newData): ! isEmpty()

head

tail

Queue Object

data

next

data

next

temp

data

next

newData

13X11

Enqueue(newData): ! isEmpty()

head

tail

Queue Object

data

next

data

next

temp

data

next

newData

13X11

Enqueue(newData): ! isEmpty()

head

tail

Queue Object

data

next

data

next

data

next

newData

13X11

Questions?

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;

}}

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

13X11

Red

Dequeue()

head

tail

Queue Object

data

next

data

next

Green

data

next

Blue

13X11

Red

Dequeue()

head

tail

Queue Object

data

next

data

next

Green

data

next

Blue

retval

13X11

Red

Dequeue()

head

tail

Queue Object

data

next

data

next

Green

data

next

Blue

retval

13X11

Red

Dequeue()

head

tail

Queue Object

data

next

Green

data

next

Blue

retval

13X11

Red

Dequeue()

head

tail

Queue Object

data

next

Green

data

next

Blue

retval

Return

13X11

Dequeue()

head

tail

Queue Object

data

next

Green

data

next

Blue

13X11

Questions?

13X11

Dequeue()

head

tail

Queue Object

data

next

Green

data

next

Blue

13X11

Dequeue()

head

tail

Queue Object

data

next

Green

data

next

Blue

retval

13X11

Dequeue()

head

tail

Queue Object

data

next

Green

data

next

Blue

retval

13X11

Dequeue()

head

tail

Queue ObjectGreen

data

next

Blue

retval

Return

13X11

Dequeue()

head

tail

Queue Object

data

next

Blue

13X11

Questions?

13X11

Dequeue()

head

tail

Queue Object

data

next

Blue

13X11

Dequeue()

head

tail

Queue Object

data

next

Blue

retval

13X11

Dequeue()

head

tail

Queue Object

data

next

Blue

retval

13X11

Dequeue()

head

tail

Queue Object

data

next

Blue

retval

isEmpty???

13X11

Dequeue()

head

tail

Queue Object

data

next

Blue

retval

13X11

Dequeue()

head

tail

Queue Object

Blue

retval

13X11

Dequeue()

head

tail

Queue Object

Blue

retval

Return

13X11

Dequeue()

head

tail

Queue Object

13X11

Dequeue Codepublic Object dequeue() {

Object retval;if(isEmpty()){

retval = null;}else{

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

tail = null;}

}return retval;

}

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

13X11

Questions?

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!

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

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

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

13X11

top related