list implementations that link data
DESCRIPTION
List Implementations That Link Data. Chapter 6. Linked Data Forming a Chains The Class Node A Linked Implementation Adding to End of List Adding at Given Position Method remove Method replace Method getEntry. Method contains Remaining methods - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/1.jpg)
List Implementations That Link Data
Chapter 6
![Page 2: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/2.jpg)
2
Chapter Contents
Linked Data• Forming a Chains
The Class Node
A Linked Implementation• Adding to End of List• Adding at Given
Position• Method remove• Method replace• Method getEntry
• Method contains• Remaining methods• Using a Class Node
with Set and Get Methods
Tail References• Revision of List
Pros and Cons of Using Chain
Java Class Library:LinkedList
![Page 3: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/3.jpg)
3
Linked Data
Consider the analogy of desks in a classroom• Placed in classroom as needed• Each desk has a unique id, the “address”• The desks are linked by keeping the address
of another chair• We have a chain of chairs
![Page 4: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/4.jpg)
4
Linked Data
A chain of 5 desks.
![Page 5: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/5.jpg)
5
Forming a ChainFirst desk placed in room• Blank desk top, no links• Address of the first chair given to teacher
One desk in the room.
![Page 6: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/6.jpg)
6
Forming a Chain
Second student arrives, takes a desk• Address of first desk placed on new desk• Instructor “remembers” address of new desk
Two linked desks
![Page 7: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/7.jpg)
7
Forming a ChainThird desk arrives• New desk gets address of second desk• Instructor remembers address of new desk
Three linked desks, newest desk first.
![Page 8: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/8.jpg)
8
Forming Another Chain
This time the first student is always at the beginning of the chain• Instructor only remembers first address
The address of a new desk is placed on the previous desk (at end of chain)• End of chain found by following links • Newest desk does not have a pointer address
to any other desk
![Page 9: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/9.jpg)
9
Forming Another Chain
Two linked desks, newest desk last.
![Page 10: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/10.jpg)
10
Forming Another Chain
Three linked desks, newest desk last.
![Page 11: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/11.jpg)
11
Forming Another Chain
Five linked desks, newest desk last.
![Page 12: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/12.jpg)
12
Adding New Desk
Consider the requirement to organize the chain alphabetically• New arrivals are placed somewhere in the
chain, not necessarily at the end
Possibilities for placement of a new desk• Case 1: Before all current desks• Case 2: Between two existing desks• Case 3: After all current desks
![Page 13: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/13.jpg)
13
Adding New Desk: Case 1
Chain of desks prior to adding a new desk to beginning of the chain
![Page 14: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/14.jpg)
14
Adding New Desk: Case 1
Addition of a new desk to beginning of a chain of desks
![Page 15: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/15.jpg)
15
Adding New Desk: Case 2
Two consecutive desks within a chain prior to adding new desk between
![Page 16: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/16.jpg)
16
Adding New Desk: Case 2
Addition of a new desk between two other desks.
![Page 17: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/17.jpg)
17
Removing Desk
Removing an item from a chain
Possible cases• Case 1: Desk to be removed is first in the
chain• Case 2: Desk to be removed is between two
current desks• Case 3: Desk to be removed is last in the
chain
![Page 18: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/18.jpg)
18
Removing Desk: Case 1
A chain of desks just prior to removing first desk.
![Page 19: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/19.jpg)
19
Removing Desk: Case 1
A chain of desks just after removing first desk.
![Page 20: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/20.jpg)
20
Removing Desk: Case 2
A chain of desks just prior to removing a desk between two other desks.
![Page 21: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/21.jpg)
21
Removing Desk: Case 2
A chain of desks just after removing a desk between two other desks.
![Page 22: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/22.jpg)
22
Removing Desk: Case 3
Before and after removing last desk from a chain.
![Page 23: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/23.jpg)
23
The Class Node
Nodes are objects that are linked together to form a data structure
We will use nodes with two data fields• A reference to an entry in the list
• (the person sitting at the desk)
• A reference to another node • (the address on the paper on the desk)
![Page 24: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/24.jpg)
24
The Class Node
Two linked nodes with (a) primitive data; (b) class/object data
private class Node
{
private T data; //data value in that node entry
private Node next; //link to next node
…
}
![Page 25: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/25.jpg)
25
Inner Class Nodeprivate class Node{
private T data; //data value in that node entryprivate Node next; //link to next node
private Node (T dataPortion){
data = dataPortion;next = null;
} //end constructor
private Node( T dataPortion, Node nextNode){
data = dataPortion;next = nextNode;
} // end constructor
}
![Page 26: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/26.jpg)
26
A Linked Implementation of the ADT List
Use a chain of nodes• Remember the address of the first node in the
chain
Record a reference to the first node• The “head reference”
The implementation contains the class Node as an inner class
![Page 27: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/27.jpg)
27
A Linked Implementation of the ADT List
public class LList<T> implements ListInterface<T>{
private Node firstNode; //head reference to fist nodeprivate Node lastNode; // tail reference to last nodeprivate int length; //number of entries in list
public LList(){
clear();} //end constructor
public final void clear() {
firstNode = null;lastNode = null;length = 0;
} // end clear
// implementation of the public methods add, remove, replace, getEntry, contains, getLength, isEmpty, isFull, and display…
}
![Page 28: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/28.jpg)
28
Adding to the End of An empty List
(a) An empty list and a new node;(b) after adding a new node to a list that was empty
![Page 29: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/29.jpg)
29
Adding to the End of An Non-empty List
A chain of nodes (a) just prior to adding a node at the end; (b) just after adding a node at the end.
![Page 30: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/30.jpg)
30
Adding to the End of the List
public boolean add(T newEntry) {
Node newNode = new Node(newEntry);
if (isEmpty())firstNode = newNode;
elselastNode.next = newNode;
lastNode = newNode;
length++;return true;
}
![Page 31: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/31.jpg)
31
Adding to the Beginning of the List
A chain of nodes (a) prior to adding a node at the beginning; (b) after adding a node at the beginning.
![Page 32: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/32.jpg)
32
Adding at a Given Position Within the List
A chain of nodes (a) prior to adding node between adjacent nodes; (b) after adding node between
adjacent nodes
![Page 33: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/33.jpg)
33
Adding at a Given Position Within the List
The following Java Statements implement these steps:• Node newNode = new Node(newEntry);• Node nodeBefore = getNodeAt(newPosition -1);• Node nodeAfter = nodeBefore.next;• newNode.next = nodeAfter;• nodeBefore.next = newNode;
![Page 34: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/34.jpg)
34
Removing the first node
A chain of nodes (a) prior to removing first node; (b) after removing the first node
Need to see if there is only one node, if yes, update the lastnode.
![Page 35: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/35.jpg)
35
Removing other than the beginning of the list
A chain of nodes (a) prior to removing interior node; (b) after removing interior node
Need to see if nodeToRemove is at the end, if yes, update the lastnode.
![Page 36: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/36.jpg)
36
The Method remove: first node
firstNode = firstNode.next;
The Method remove: other than the beginning
Node nodeBefore = getNodeAt(givenPosition -1);
Node nodeToRemove = nodeBefore.next;
Node nodeAfter = nodeToRemove.next;
nodeBefore.next = nodeAfter;
nodeToRemove = null;
![Page 37: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/37.jpg)
37
Using Class Node that Has Set and Get Methods
Class Node is an inner class• The class LList can access private data
fields directly
• Stylistically better to use the Set and Get methods of the class Node
Thus better to use statements such as:currentNode.getData(); ordesiredNode.setData(newEntry);
![Page 38: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/38.jpg)
38
Using Class Node that Has Set and Get Methods
private T getData(){
return data;}private void setData(T newData){
data = newData;}
private Node getNextNode(){
return next;}
![Page 39: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/39.jpg)
39
Tail References
Consider a set of data where we repeatedly add data to the end of the list
Each time the getNodeAt method must traverse the whole list• This is inefficient
Solution: maintain a pointer that always keeps track of the end of the chain• The tail reference
![Page 40: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/40.jpg)
40
Tail References
A linked chain with a head and tail reference.
![Page 41: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/41.jpg)
41
Tail References
When adding to an empty list• Both head and tail references must point to the
new solitary node
When adding to a non empty list• No more need to traverse to the end of the list• lastNode points to it
• Adjust lastNode.next to new node and lastNode to new node
![Page 42: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/42.jpg)
42
Tail References
Adding a node to the end of a nonempty chain that has a tail reference
![Page 43: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/43.jpg)
43
Pros and Cons of a Chain for an ADT List
The chain (list) can grow as large as necessary
Can add and remove nodes without shifting existing entries
But …
Must traverse a chain to determine where to make addition/deletion
Retrieving an entry requires traversal• As opposed to direct access in an array
![Page 44: List Implementations That Link Data](https://reader035.vdocuments.us/reader035/viewer/2022062407/56812bc6550346895d9010c1/html5/thumbnails/44.jpg)
44
Java Class Library: The Class LinkedList
The standard java package java.util contains the class LinkedListThis class implements the interface ListContains additional methods
•addFirst()•addLast()•removeFirst()•removeLast()•getFirst()•getLast()