![Page 1: Chapter 3 The easy stuff. Lists If you only need to store a few things, the simplest and easiest approach might be to put them in a list Only if you need](https://reader036.vdocuments.us/reader036/viewer/2022062504/5a4d1b117f8b9ab05998f8df/html5/thumbnails/1.jpg)
Chapter 3Chapter 3
The easy stuffThe easy stuff
![Page 2: Chapter 3 The easy stuff. Lists If you only need to store a few things, the simplest and easiest approach might be to put them in a list Only if you need](https://reader036.vdocuments.us/reader036/viewer/2022062504/5a4d1b117f8b9ab05998f8df/html5/thumbnails/2.jpg)
ListsLists
• If you only need to store a few things, the simplest and easiest approach might be to put them in a list
• Only if you need to search or some other more intensive operation on the data would a more complex structure be needed
![Page 3: Chapter 3 The easy stuff. Lists If you only need to store a few things, the simplest and easiest approach might be to put them in a list Only if you need](https://reader036.vdocuments.us/reader036/viewer/2022062504/5a4d1b117f8b9ab05998f8df/html5/thumbnails/3.jpg)
ListsLists
• A list is a finite, ordered sequence of data items know as elements
• Each element has a position• It’s empty when it contains no elements• The number of current elements is its
length• The first element is the head• The last element is the tail
![Page 4: Chapter 3 The easy stuff. Lists If you only need to store a few things, the simplest and easiest approach might be to put them in a list Only if you need](https://reader036.vdocuments.us/reader036/viewer/2022062504/5a4d1b117f8b9ab05998f8df/html5/thumbnails/4.jpg)
Basic OperationsBasic Operations
• Must be able to insert and delete anywhere along the list
• Should be able to gain access to an elements value
• Must be able to create and clear the list• Convenient to gain access to the next
element from the current one
![Page 5: Chapter 3 The easy stuff. Lists If you only need to store a few things, the simplest and easiest approach might be to put them in a list Only if you need](https://reader036.vdocuments.us/reader036/viewer/2022062504/5a4d1b117f8b9ab05998f8df/html5/thumbnails/5.jpg)
List ADTList ADT
• In C++ we will use the notion of an abstract class for the List
• We will increase the flexibility by making it a template
• The abstract class does not specify how the operations are implemented
• Given that we want to support the concept of a sequence, the key decision embodied in the ADT is the notion of position
![Page 6: Chapter 3 The easy stuff. Lists If you only need to store a few things, the simplest and easiest approach might be to put them in a list Only if you need](https://reader036.vdocuments.us/reader036/viewer/2022062504/5a4d1b117f8b9ab05998f8df/html5/thumbnails/6.jpg)
DetailsDetails
• Assuming that we all have implemented a linked list
• There are 4 steps for insertion– Create an empty node– Initialize the data member to the value to be
stored– Fix the pointers for next and prev– Make sure any head and tail pointers are still
valide
![Page 7: Chapter 3 The easy stuff. Lists If you only need to store a few things, the simplest and easiest approach might be to put them in a list Only if you need](https://reader036.vdocuments.us/reader036/viewer/2022062504/5a4d1b117f8b9ab05998f8df/html5/thumbnails/7.jpg)
DeletionDeletion
• The node to be deleted is targeted• A temporary pointer to that node is created• The node is redirected around• The temporary pointer is used to delete
the node
![Page 8: Chapter 3 The easy stuff. Lists If you only need to store a few things, the simplest and easiest approach might be to put them in a list Only if you need](https://reader036.vdocuments.us/reader036/viewer/2022062504/5a4d1b117f8b9ab05998f8df/html5/thumbnails/8.jpg)
Circular ListsCircular Lists
• Normally not a good idea• But there are times when one might be useful
– When you have multiple processes using a resource• The end of the list simply points back to the
head• There is a tail pointer and that’s it.• Data can be inserted either at the front or at the
end
![Page 9: Chapter 3 The easy stuff. Lists If you only need to store a few things, the simplest and easiest approach might be to put them in a list Only if you need](https://reader036.vdocuments.us/reader036/viewer/2022062504/5a4d1b117f8b9ab05998f8df/html5/thumbnails/9.jpg)
Skip ListsSkip Lists
• Linked lists have the drawback that each node must be searched when looking for data
• Ordering the data can solve this to some degree, but you still must sequentially scan the list
• Skip lists solve this problem of sequential search.
![Page 10: Chapter 3 The easy stuff. Lists If you only need to store a few things, the simplest and easiest approach might be to put them in a list Only if you need](https://reader036.vdocuments.us/reader036/viewer/2022062504/5a4d1b117f8b9ab05998f8df/html5/thumbnails/10.jpg)
SkipLists PictureSkipLists Picture
![Page 11: Chapter 3 The easy stuff. Lists If you only need to store a few things, the simplest and easiest approach might be to put them in a list Only if you need](https://reader036.vdocuments.us/reader036/viewer/2022062504/5a4d1b117f8b9ab05998f8df/html5/thumbnails/11.jpg)
SearchingSearching
• When searching a skiplist the search begins at the first node at the highest level
• If the element we are looking for is not found, then we drop down a level and try again
• This is continued until the element is found, or the list is exhausted.
![Page 12: Chapter 3 The easy stuff. Lists If you only need to store a few things, the simplest and easiest approach might be to put them in a list Only if you need](https://reader036.vdocuments.us/reader036/viewer/2022062504/5a4d1b117f8b9ab05998f8df/html5/thumbnails/12.jpg)
InsertionInsertion
• Searching seems straight forward, but what about insertion
• How do you decide how many levels to make the new node?
• We abandon the notion of having nodes with a certain number of levels in certain positions
• We keep the idea of nodes having a certain number of levels
![Page 13: Chapter 3 The easy stuff. Lists If you only need to store a few things, the simplest and easiest approach might be to put them in a list Only if you need](https://reader036.vdocuments.us/reader036/viewer/2022062504/5a4d1b117f8b9ab05998f8df/html5/thumbnails/13.jpg)
More on InsertionMore on Insertion
• But still, how do we choose the levels?• If we assume that the maximum number of
levels for a node is 4, then– For 15 elements, we need:
• 8 – 1 pointer nodes• 4 – 2 pointer nodes• 2 – 3 pointer nodes• 1 – 4 pointer nodes
![Page 14: Chapter 3 The easy stuff. Lists If you only need to store a few things, the simplest and easiest approach might be to put them in a list Only if you need](https://reader036.vdocuments.us/reader036/viewer/2022062504/5a4d1b117f8b9ab05998f8df/html5/thumbnails/14.jpg)
Still insertionStill insertion
• So when we create a new node, we randomly choose the number of levels the new node will have
• When the list is created we initialize an array of powers that will hold the cut off for each level.
• The array is filled using 2^j + 1for j=0:maxlevel-1
![Page 15: Chapter 3 The easy stuff. Lists If you only need to store a few things, the simplest and easiest approach might be to put them in a list Only if you need](https://reader036.vdocuments.us/reader036/viewer/2022062504/5a4d1b117f8b9ab05998f8df/html5/thumbnails/15.jpg)
Space considerationsSpace considerations
• Not all nodes need to have an array for the pointers containing all of the levels
• So we have a pointer to an array of pointers that will be the array of levels.
![Page 16: Chapter 3 The easy stuff. Lists If you only need to store a few things, the simplest and easiest approach might be to put them in a list Only if you need](https://reader036.vdocuments.us/reader036/viewer/2022062504/5a4d1b117f8b9ab05998f8df/html5/thumbnails/16.jpg)
Self-Organizing ListsSelf-Organizing Lists
• The motivation for skiplists was to speed the search process.
• A different idea, actually reorganizes the list through use
• The main idea is when an element is searched for and found, its position in the list is modified to make it easier to find next time
![Page 17: Chapter 3 The easy stuff. Lists If you only need to store a few things, the simplest and easiest approach might be to put them in a list Only if you need](https://reader036.vdocuments.us/reader036/viewer/2022062504/5a4d1b117f8b9ab05998f8df/html5/thumbnails/17.jpg)
Ordering HeuristicsOrdering Heuristics
• There are many ways to re-order the list, here are 4
1. Move-to-front: put the element at the beginning of the list
2. Transpose: swap the element with its preceding neighbor
3. Count: order the list by the number of times an element has been accessed
4. Ordering: use some natural criteria for the ordering
![Page 18: Chapter 3 The easy stuff. Lists If you only need to store a few things, the simplest and easiest approach might be to put them in a list Only if you need](https://reader036.vdocuments.us/reader036/viewer/2022062504/5a4d1b117f8b9ab05998f8df/html5/thumbnails/18.jpg)
Sparse TablesSparse Tables
• Many times, we need to store information about stuff, and a table is the natural way to store it
• For example, all the grades for all students in a university
• The problem here is that not all students take all courses and a lot of the table is empty.
![Page 19: Chapter 3 The easy stuff. Lists If you only need to store a few things, the simplest and easiest approach might be to put them in a list Only if you need](https://reader036.vdocuments.us/reader036/viewer/2022062504/5a4d1b117f8b9ab05998f8df/html5/thumbnails/19.jpg)
What do you do?What do you do?
• You can use, two one dimensional arrays of linked lists
• One of the arrays is the classes and they point off to a linked list of students taking that course
• The other array is the students in the university which points off to the classes taken by the student
![Page 20: Chapter 3 The easy stuff. Lists If you only need to store a few things, the simplest and easiest approach might be to put them in a list Only if you need](https://reader036.vdocuments.us/reader036/viewer/2022062504/5a4d1b117f8b9ab05998f8df/html5/thumbnails/20.jpg)
Ok…what about the linked list?Ok…what about the linked list?
• The nodes in the linked list contain five data members– The student number– The class number– Grade– A pointer to the next student taking this
course– A pointer to the next course this student is
taking
![Page 21: Chapter 3 The easy stuff. Lists If you only need to store a few things, the simplest and easiest approach might be to put them in a list Only if you need](https://reader036.vdocuments.us/reader036/viewer/2022062504/5a4d1b117f8b9ab05998f8df/html5/thumbnails/21.jpg)
PicturePicture