Top Banner
Self-Referential Classes •A Self-referential class is a class that contains a reference to an object that has the same class type. – A self-referential class typically has a variable definition that is the same type as the Class. – This type of class can be used to create data structures like linked lists, stacks, etc. null is used to indicate the end of the data structure.
23

Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.

Jan 14, 2016

Download

Documents

Primrose Norton
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.

Self-Referential Classes• A Self-referential class is a class that

contains a reference to an object that has the same class type.– A self-referential class typically has a variable

definition that is the same type as the Class.– This type of class can be used to create data

structures like linked lists, stacks, etc.– null is used to indicate the end of the data

structure.

Page 2: Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.

Self-Referential Classes

Class Node {

private Object data;

private Node next;

public void Node(Object d) { }

public void setData(Object d) {}

public Object getData() {}

public void setNext(Node nextNode) {}

public Node getNext() {}

}

Page 3: Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.

Dynamic Memory Allocation

• The ability to have your program get more space in memory while your program is running is called dynamic memory allocation.– The new operator must be used to obtain

dynamic memory allocation.

Page 4: Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.

Linked Lists

• A linked list is a linear collection of nodes that are connected by memory reference links.– The first node has a reference to the second

node, the second node has a reference to the third node, etc.

– The last node of the list always has a null reference.

• This marks the end of the list.

Page 5: Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.

Linked Lists

J B P A

First Node Last Node

Page 6: Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.

Linked Lists vs. Arrays

• Arrays can become full, and they can not be easily resized while the program executes.

• Linked Lists can be used when the number of elements is unknown because the length of the list can increase and decrease as needed.

Page 7: Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.

Linked Lists

• Example:– EmptyListException– List Class– ListTest program

Page 8: Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.

Linked Lists

• The insertAtFront method first checks to see if the list is empty, if it is:– then the first and last nodes are set to the

same node, – otherwise, the new item is inserted at the

front of the list and the old first item becomes the second item.

Page 9: Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.

Linked Lists

• insertAtFront

AP

H

List

New node

AP

H

List

New node

Page 10: Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.

Linked Lists

• The insertAtBack method first checks to see if the list is empty, if it is:– then the first and last nodes are set to the

same node, – otherwise, the new item is inserted at the

end of the list and the old last item becomes the second to last item.

Page 11: Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.

Linked Lists

• insertAtBack

AP

H

List

New node

AP

H

List

New node

Page 12: Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.

Linked Lists

• The insertNodeAfter method first checks to see if the list is empty, if it is:– then the first and last nodes are set to the

same node, – otherwise, the node (n1) after which the

new item is to be inserted is located and the new item is inserted after n1.

Page 13: Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.

Linked Lists

• insertNodeAfter

AP

H

List

New node

MP

H

List

New node

M

A

Page 14: Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.

Linked Lists

• The removefromFront method first assigns the variable to hold the removed item to the first node. – If the first and last node are the same

node, then they are both set to null – Otherwise, the first node is set to be the

next node.

Page 15: Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.

Linked Lists

• removeFromFront

APList

APList

Removed Item

Page 16: Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.

Linked Lists

• The removefromBack method first assigns the variable to hold the removed item to the last node. – If the first and last node are the same

node, then they are both set to null – Otherwise, the second to last node is set to

be the last node.

Page 17: Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.

Linked Lists

• removeFromBack

APList

APList

Removed Item

Page 18: Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.

Linked Lists

• The removeNodeAfter method assigns the variable to hold the removed item to the next node. – If the first and last node are the same node,

then they are both set to null – otherwise, the node (n1) after which the item

is to be removed is located and the item is removed. Node (n1) is set to point to the next node in the list.

Page 19: Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.

Linked Lists

• removeNodeAfter

APList

MP

H

List

Removed item

M

AH

Page 20: Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.

Example

• Write a program that inserts 25 random integers from 0 to 100 in order in a linked list object. The program should calculate the sum of the elements, and the floating-point average of the elements. – List2.java

Page 21: Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.

Searching Linked Lists

• Extend List2.java to implement a listSearch method. The method takes an Object and an int key.

Page 22: Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.

Stacks

• Implementation of a Stack using a Linked List.

Page 23: Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.

Queues

• Implementation of a Queue using a Linked List.