Lesson 2p
Lesson 2p
Lists
l A list is a homogeneous collection of elements
Chapter 3 with a linear relationship between elements
l Linear Relationship: Each element except the
ADT Unsorted List first has a unique predecessor, and each
element except the last has a unique successor
l Length: The number of items in the list, which
can vary over time
1
6/10/18
2
6/10/18
3
6/10/18
Figure 3.4 Deleting an item in an unsorted list (a) Original list (b) Deleting Judy
(c) Deleting Bobby (move up) (d) Deleting Bobby (swap)
4
6/10/18
5
6/10/18
6
6/10/18
7
6/10/18
Figure 3.12 The second PutItem operation (a) new(location) (b) info(location)
<- newElement (c) Make next (location) point to List's tope node (d) Make List
point to the new node
Figure 3.9 Putting in the First Element
8
6/10/18
9
6/10/18
10
6/10/18
MakeEmpty GetItem
l MakeEmpty must deallocate each node l The algorithm is unchanged: Linear search
individually in order to empty the list through the list to find the desired item
l This is accomplished using a while loop l In fact, the implementation is largely
l Iteration starts at listData, the head of the list, unchanged; it just needs to use pointers instead
and continues using listData->next of array indices
l Iteration stops when listData is NULL
Figure 3.17 Retrieving an item in an unsorted linked list (a) Get Kit (b) Get Lila
11
6/10/18
12
6/10/18
13