DATA STRUCTURES LAB 3 TA Nouf AlHarbi nouf
DATA STRUCTURES LAB 3 TA: Nouf Al-Harbi nouf 200@hotmail. com
DATA STRUCTURES LAB 3 Sorted List
objectives 3 By the end of this lab you will be able to : � Implement Sorted List Class with all its operations Sorted List
Sorted List 4 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
5 Sorted List Example. . Implement Sorted List of 10 integer elements Sorted List
6 Sorted. Type class (data & methods members) 3 data members � Info � Length � Array of 10 elements Actual data of list Determines the length of list Ranges between 0 (No elements and 10 (max number of elements) Current. Pos As an index Ranges between -1 (prior to 1 st element )and the length -1(points to the last element) Private data: length info [0] [1] [2] [9] current. Pos Sorted List
7 Sorted. Type class (data & methods members) Sorted. Type Constructor Transformers change state Make. Empty Private data: Insert. Item length Delete. Item info Is. Full Observers Observe state Length. Is [9] Retrieve. Item current. Pos Reset. List Iterators Process All [0] [1] [2] Get. Next. Item Sorted List
Implementing Sorted Class 8 � the class declaration is placed in one file (header file) Sorted. h � the implementation of all the methods is put in another file Sorted. cpp � the Main function is put in another file Sorted. List. cpp Foe each Sorted. cpp & Sorted. List we should inclusde Sorted. h file � #include “Sorted. h” Sorted List
9 Implementing Sorted Class 1 - class declaration Sorted. h Sorted List
Sorted. h 10 // SPECIFICATION FILE ( Sorted. h ) const MAX_ELEMENTS=10; class Sorted. Type // declares a class data type { public: // 8 public member functions Sorted. Type(); void Make. Empty ( ) ; bool Is. Full ( ) const ; int Length. Is ( ) const ; // returns length of list bool Retrieve. Item ( int item ) ; void Insert. Item ( int item ) ; void Delete. Item ( int item ) ; void Reset. List ( ); int Get. Next. Item ( ) ; private: // 3 private data members int info[MAX_ELEMENTS] ; int current. Pos ; int length; }; Sorted List
11 Implementing Sorted Class 2 - implementation of all the methods Sorted. cpp Sorted List
Sorted. Type Counstructor 12 Sorted. Type: : Sorted. Type() { length = 0; } Sorted List
Make. Empty Method 13 void Sorted. Type: : Make. Empty ( ) { length = 0 ; } Sorted List
Is. Full Method 14 Is. Ful l • Full True • Not full false bool Sorted. Type: : Is. Full ( ) const { return (length == MAX_ITEMS) ; Sorted List Inside constant functions we can’t change data values
Length. Is Method 15 Length. I s • Length of the list int Sorted. Type: : Length. Is ( ) const { return length ; } Sorted List
Reset. List Method 16 void Sorted. Type: : Reset. List ( ) { current. Pos = -1 ; } Sorted List
Get. Next. Item Method 17 Get. Next. Ite m The item that follows the current item int Sorted. Type: : Get. Next. Item ( ) { int item; current. Pos++; item = info[current. Pos]; Sorted List return item ;
Insert. Item Method 18 • The item that will be inserted Insert. Ite m 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.
Insert. Item Method 19
Delete. Item Method 20 • The item that will be deleted Delete. Ite m 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
Delete. Item Method 21 • The item that will be deleted Delete. Ite m
Retrieve. Item Method 22 • The item that will be searched for Retrieve. Item • 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
23 Sorted List
Binary Search in a Sorted List: 24 Compare the searched for value to the middle array item � if value == middle � 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 quit. You've found it. 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 Sorted List empty. �
Binary Search Example Searching 120… 12 48 55 58 59 78 99 120 122 154 0 1 2 3 4 5 6 7 8 9 first mid. Point 120<59 ? NO 120>59 ? YES first = 0 last = 9 mid. Point = (0 + 9) / 2 = 4 last
Binary Search Example Searching 120… 12 48 55 58 59 78 99 120 122 154 0 1 2 3 4 5 6 7 8 9 first 120< 120 NO 120> 120 NO 120=120 ? YES Sto sea op rch ing mid. Point first = 5 last = 9 mid. Point = (5 + 9) / 2 = 7 last
Binary Search Example Searching 49… 12 48 55 58 59 78 99 120 122 154 0 1 2 3 4 5 6 7 8 9 first mid. Point 49< 59 YES first = 0 last = 9 mid. Point = (0 + 9) / 2 = 4 last
Binary Search Example Searching 49… 12 48 55 58 59 78 99 120 122 154 0 1 2 3 4 5 6 7 8 9 first mid. Point 49<48? NO 49>48? YES last first = 0 last = 3 mid. Point = (0 + 3) / 2 = 1
Binary Search Example Searching 49… 12 48 55 58 59 78 99 120 122 154 0 1 2 3 4 5 6 7 8 9 first last mid. Point 49<55 ? NO 49>55 ? YES first = 2 last = 3 mid. Point = (2 + 3) / 2 = 2
Binary Search Example Searching 49… not found 12 48 55 58 59 78 99 120 122 154 0 1 2 3 4 5 6 7 8 9 last first = 2 last = 1 (first > last) => item not found
Retrieve. Item Method 31
32 Implementing Sorted Class 3 - Main function Sorted. List. cpp Sorted List
In Main function. . 33 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 Ask whether the list is full or not Delete 33 from the list Display the elements in the list Sorted List
- Slides: 33