Title: List Implementations That Link Data
1List Implementations That Link Data
2Chapter Contents
- Linked Data
- The Class Node
- A Linked Implementation
- Tail References
- Pros and Cons of Using Chain
- Java Class Library
- java.util.LinkedList
3Linked 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
4Linked Data
Fig. 6-1 A chain of 5 desks.
5Forming a Chain
- First desk placed in room
- Blank desk top, no links
- Address of the first chair given to teacher
Fig. 6-2 One desk in the room.
6Forming a Chain
- Second student arrives, takes a desk
- Address of first desk placed on new desk
- Instructor remembers address of new desk
Fig. 6-3 Two linked desks
7Forming a Chain
- Third desk arrives
- New desk gets address of second desk
- Instructor remembers address of new desk
Fig. 6-4 Three linked desks, newest desk first.
8Forming 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
9Forming Another Chain
Fig. 6-5 Two linked desks, newest desk last.
10Forming Another Chain
Fig. 6-6 Three linked desks, newest desk last.
11Forming Another Chain
Fig. 6-7 Five linked desks, newest desk last.
12Forming Yet Another Chain
- 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
- Before all current desks
- Between two existing desks
- After all current desks
13Forming Yet Another Chain
Fig. 6-8 Add to beginning...before
14Forming Yet Another Chain
Fig. 6-9 Add to beggining...after
15Forming Yet Another Chain
Fig. 6-10 Add between...before
16Forming Yet Another Chain
Fig. 6-11 Add between...after
17Forming Yet Another Chain
- Removing an item from a chain
- Possible cases
- Desk is first (head)
- Desk is between two others
- Desk is last (tail)
18Forming Yet Another Chain
Fig. 6-12 Remove head...before
19Forming Yet Another Chain
Fig. 6-13 Remove head...after
20Forming Yet Another Chain
Fig. 6-14 Remove between...before
21Forming Yet Another Chain
Fig. 6-15 Remove between...after
22Forming Yet Another Chain
Fig. 6-16 Remove tail...before and after
23The 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)
24The Class Node
Fig. 6-17 Two linked nodes with (a) primitive
data (b) object data
25A Linked Implementation of the ADT List
- Use a chain of nodes
- Remember address of first node in chain
- Record reference to the first node
- The head reference
- The implementation contains the class Node as an
inner class
26Adding to an Empty List
Fig. 6-18 (a) An empty list and a new node(b)
after adding a new node to a list that was empty
27Adding to the End of the List
Fig. 6-19 A chain of nodes (a) just prior to
adding a node at the end (b) just after adding a
node at the end.
28Adding to the Beginning
Fig. 6-20 (a) before, and (b) after
29Adding to the Middle
Fig. 6-21 (a) before and (b) after
30remove() the head
Fig. 6-22 (a) before and (b) after
31remove() from middle
Fig. 6-23 (a) before (b) after
32Using Class Node that Has set and get Methods
- Class Node is an inner class of LList
- LList can access private fields directly
- Stylistically better to use the set and get
methods of the class Node e.g. - currentNode.getData() ordesiredNode.setData(ne
wEntry) - p.130 BYOH...subsequent refs to
- ltnodegt.data about 10
- ltnodegt.next about 30
33Tail 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
34Tail References
Fig. 6-24 A linked chain with a head and tail
reference.
35Tail 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 in new node and lastNode
36Tail References
Fig. 6-25 Adding a node to the end of a nonempty
chain that has a tail reference
37Tail References
Fig. 6-26 Removing a node from a chain that has
both head and tail references when the chain
contains (a) one node (b) more than one node
38Pros and Cons of a Chain for an ADT List
- Pro
- chain (list) can grow as large as necessary
- no need to shift for add(), remove()
- Con
- Must traverse a chain to determine where to make
addition/deletion - Retrieving an entry requires traversal
- As opposed to direct access in an array
39Java Class Library The Class LinkedList
- standard java contains the class
java.util.LinkedList - This class implements the interface List
- Contains additional methods
- addFirst()
- addLast()
- removeFirst()
- removeLast()
- getFirst()
- getLast()