List Implementations That Link Data - PowerPoint PPT Presentation

1 / 39
About This Presentation
Title:

List Implementations That Link Data

Description:

Each desk has a unique id, the 'address' The desks are linked by keeping the address of ... Consider the requirement to organize the chain alphabetically ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 40
Provided by: steve1088
Category:

less

Transcript and Presenter's Notes

Title: List Implementations That Link Data


1
List Implementations That Link Data
  • Chapter 6

2
Chapter Contents
  • Linked Data
  • The Class Node
  • A Linked Implementation
  • Tail References
  • Pros and Cons of Using Chain
  • Java Class Library
  • java.util.LinkedList

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

4
Linked Data
Fig. 6-1 A chain of 5 desks.
5
Forming 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.
6
Forming 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
7
Forming 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.
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

9
Forming Another Chain
Fig. 6-5 Two linked desks, newest desk last.
10
Forming Another Chain
Fig. 6-6 Three linked desks, newest desk last.
11
Forming Another Chain
Fig. 6-7 Five linked desks, newest desk last.
12
Forming 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

13
Forming Yet Another Chain
Fig. 6-8 Add to beginning...before
14
Forming Yet Another Chain
Fig. 6-9 Add to beggining...after
15
Forming Yet Another Chain
Fig. 6-10 Add between...before
16
Forming Yet Another Chain
Fig. 6-11 Add between...after
17
Forming Yet Another Chain
  • Removing an item from a chain
  • Possible cases
  • Desk is first (head)
  • Desk is between two others
  • Desk is last (tail)

18
Forming Yet Another Chain
Fig. 6-12 Remove head...before
19
Forming Yet Another Chain
Fig. 6-13 Remove head...after
20
Forming Yet Another Chain
Fig. 6-14 Remove between...before
21
Forming Yet Another Chain
Fig. 6-15 Remove between...after
22
Forming Yet Another Chain
Fig. 6-16 Remove tail...before and after
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)

24
The Class Node
Fig. 6-17 Two linked nodes with (a) primitive
data (b) object data
25
A 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

26
Adding 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
27
Adding 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.
28
Adding to the Beginning
Fig. 6-20 (a) before, and (b) after
29
Adding to the Middle
Fig. 6-21 (a) before and (b) after
30
remove() the head
Fig. 6-22 (a) before and (b) after
31
remove() from middle
Fig. 6-23 (a) before (b) after
32
Using 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

33
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

34
Tail References
Fig. 6-24 A linked chain with a head and tail
reference.
35
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 in new node and lastNode

36
Tail References
Fig. 6-25 Adding a node to the end of a nonempty
chain that has a tail reference
37
Tail 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
38
Pros 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

39
Java 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()
Write a Comment
User Comments (0)
About PowerShow.com