Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 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.

2 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() {} }

3 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.

4 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.

5 Linked Lists J B P A First Node Last Node

6 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.

7 Linked Lists Example: –EmptyListException –List Class –ListTest program

8 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.

9 Linked Lists insertAtFront A P H List New node A P H List New node

10 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.

11 Linked Lists insertAtBack A P H List New node A P H List New node

12 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.

13 Linked Lists insertNodeAfter A P H List New node M P H List New node M A

14 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.

15 Linked Lists removeFromFront A P List A P Removed Item

16 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.

17 Linked Lists removeFromBack A P List A P Removed Item

18 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.

19 Linked Lists removeNodeAfter A P List M P H Removed item M A H

20 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

21 Searching Linked Lists Extend List2.java to implement a listSearch method. The method takes an Object and an int key.

22 Stacks Implementation of a Stack using a Linked List.

23 Queues Implementation of a Queue using a Linked List.


Download ppt "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."

Similar presentations


Ads by Google