TA: Nouf Al-Harbi nouf200@hotmail.com Data Structures LAB 3 TA: Nouf Al-Harbi nouf200@hotmail.com
Data structures lab 3 Sorted List
objectives By the end of this lab you will be able to : Implement Sorted List Class with all its operations Sorted List
Sorted List List elements are in an order that is sorted in some way -- either numerically or alphabetically by the elements themselves, or by a component of the element (called a KEY member) . Sorted List
Sorted List Example .. Implement Sorted List of 10 integer elements
SortedType class (data & methods members) 3 data members Info Array of 10 elements Actual data of list Length Determines the length of list Ranges between 0 (No elements and 10 (max number of elements) CurrentPos As an index Ranges between -1 (prior to 1st element )and the length - 1(points to the last element) Private data: length info [ 0 ] [ 1 ] [ 2 ] [ 9 ] currentPos Sorted List
SortedType class (data & methods members) Constructor MakeEmpty Private data: length info [ 0 ] [ 1 ] [ 2 ] [ 9 ] currentPos InsertItem Transformers change state DeleteItem IsFull Observers Observe state LengthIs RetrieveItem ResetList Iterators Process All GetNextItem Sorted List
Implementing Sorted Class the class declaration is placed in one file (header file) Sorted.h the implementation of all the methods is put in another fileSorted.cpp the Main function is put in another file SortedList.cpp Foe each Sorted.cpp & SortedList we should inclusde Sorted.h file #include “Sorted. h” Sorted List
Implementing Sorted Class 1- class declaration Sorted.h Sorted List
Sorted.h // SPECIFICATION FILE ( Sorted.h ) const MAX_ELEMENTS=10; class SortedType // declares a class data type { public: // 8 public member functions SortedType(); void MakeEmpty ( ) ; bool IsFull ( ) const ; int LengthIs ( ) const ; // returns length of list bool RetrieveItem ( int item ) ; void InsertItem ( int item ) ; void DeleteItem ( int item ) ; void ResetList ( ); int GetNextItem ( ) ; private: // 3 private data members int info[MAX_ELEMENTS] ; int currentPos ; int length; } ; Sorted List
Implementing Sorted Class 2- implementation of all the methods Sorted.cpp Sorted List
SortedType Counstructor SortedType::SortedType() { length = 0; } Sorted List
MakeEmpty Method void SortedType::MakeEmpty ( ) { length = 0 ; } Sorted List
Inside constant functions we can’t change data values IsFull Method IsFull Full True Not full false Inside constant functions we can’t change data values bool SortedType::IsFull ( ) const { return (length == MAX_ITEMS) ; } Sorted List
LengthIs Method LengthIs int SortedType::LengthIs ( ) const { Length of the list int SortedType::LengthIs ( ) const { return length ; } Sorted List
ResetList Method void SortedType::ResetList ( ) { currentPos = -1 ; } Sorted List
The item that follows the current item GetNextItem Method GetNextItem The item that follows the current item int SortedType::GetNextItem ( ) { int item; currentPos++; item = info[currentPos]; return item ; } Sorted List
The item that will be inserted InsertItem Method InsertItem The item that will be inserted Insert Item algorithm : Find proper location for the new element in the sorted list. Create space for the new element by moving down all the list elements that will follow it. Put the new element in the list. Increment length.
InsertItem Method
The item that will be deleted DeleteItem Method DeleteItem The item that will be deleted Delete Item algorithm: Find the location of the element to be deleted from the sorted list. Eliminate space occupied by the item being deleted by moving up all the list elements that follow it. Decrement length
The item that will be deleted DeleteItem Method DeleteItem The item that will be deleted
The item that will be searched for RetrieveItem Method RetrieveItem The item that will be searched for Found true Not found false Since the list is sorted, we can improve the search operation So we don’t have to look at every element By using what’s called Binary Search
Sorted List
Binary Search in a Sorted List: Compare the searched for value to the middle array item if value == middle then quit. You've found it. else if value < middle then consider the sub list making up the first half of the array (before the middle item) else if value > middle then consider the sub list making up the last half of the array (after the middle item) If you didn't find the value, repeat the search on the sub list. You are done as soon as you find the item or whenever the sub list that you are supposed to search is empty. Sorted List
Binary Search Example 120<59 ? NO 120>59 ? YES first = 0 Searching 120… 12 48 55 58 59 78 99 120 122 154 9 8 7 6 5 4 3 2 1 first midPoint last 120<59 ? NO 120>59 ? YES first = 0 last = 9 midPoint = (0 + 9) / 2 = 4
Binary Search Example first = 5 last = 9 midPoint = (5 + 9) / 2 = 7 Searching 120… 12 48 55 58 59 78 99 120 122 154 9 8 7 6 5 4 3 2 1 first midPoint last 120< 120 NO 120> 120 120=120 ? YES first = 5 last = 9 midPoint = (5 + 9) / 2 = 7 Stoop searching
49< 59 YES Binary Search Example first = 0 last = 9 Searching 49… 12 48 55 58 59 78 99 120 122 154 9 8 7 6 5 4 3 2 1 first midPoint last 49< 59 YES first = 0 last = 9 midPoint = (0 + 9) / 2 = 4
49<48? NO 49>48? YES Binary Search Example first = 0 last = 3 Searching 49… 12 48 55 58 59 78 99 120 122 154 9 8 7 6 5 4 3 2 1 first midPoint last 49<48? NO 49>48? YES first = 0 last = 3 midPoint = (0 + 3) / 2 = 1
49<55 ? NO 49>55 ? YES Binary Search Example first = 2 Searching 49… 12 48 55 58 59 78 99 120 122 154 9 8 7 6 5 4 3 2 1 first last midPoint 49<55 ? NO 49>55 ? YES first = 2 last = 3 midPoint = (2 + 3) / 2 = 2
Binary Search Example first = 2 last = 1 Searching 49… not found 12 48 55 58 59 78 99 120 122 154 9 8 7 6 5 4 3 2 1 last first first = 2 last = 1 (first > last) => item not found
RetrieveItem Method
Implementing Sorted Class 3- Main function SortedList.cpp Sorted List
In Main function .. Create an sorted List using Array Implementation to store the following integer numbers 2,3,4,8,9,12,14,23,33,50 Display the elements in the list Retrieve the element 8 from the list Retrieve the element 12 from the list Ask about the length of the list Ask whether the list is full or not delete 2 and then 12 from the list Delete 33 from the list Sorted List