160 likes | 306 Views
Chapter 13 Array-Based List. List. Static List: no adding or deleting Dynamic List: can add or delete items from the list Both static and dynamic lists: linear search, update item values Only dynamic list: add, and delete an element. Add an object to the list. MAX_STUDENTS = 10. stu.
E N D
List • Static List: no adding or deleting • Dynamic List: can add or delete items from the list • Both static and dynamic lists: • linear search, update item values • Only dynamic list: • add, and delete an element
Add an object to the list MAX_STUDENTS = 10 stu numStudents = 5 numStudents = 4 Student stuList[MAX_STUDENTS]; int numStudents; int Add( const Student& stu ) { if ( Find( stu ) == -1 && numStudents < MAX_STUDENTS ) { stuList[numStudents] = stu ; numStudents ++; return 0; } return -1; }
Delete an object from the list MAX_STUDENTS = 10 // Delete the Student object // with ID 1002 numStudents = 3 numStudents = 4 int Delete( long stuId ) { int index; index = Find( stuId ); if ( index != -1) { for ( int i = index; i < numStudents - 1; i++ ) stuList[i] = stuList[i+1]; numStudents --; return 0; } return -1; }
Sorted List • How to sort an unsorted list? 9 1 21 12 15 3 1 12 5 5 10 9 3 9 10 10 5 12 15 21 3 21 1 15 Unsorted List: Sorted List (Ascending): Sorted List (Descending):
Selection Sort Example 9 12 1 5 3 10 21 15 s[]: 1 at index 2 Find the smallest value between s[0] and s[7]: Exchange s[0] and s[2] 1 12 9 5 3 10 21 15 3 at index 4 Find the smallest value between s[1] and s[7]: Exchange s[1] and s[4] 1 3 9 5 12 10 21 15 5 at index 3 Find the smallest value between s[2] and s[7]: Exchange s[2] and s[3] 1 3 5 9 12 10 21 15 9 at index 3 Find the smallest value between s[3] and s[7]: Exchange s[3] and s[3] 1 3 5 9 12 10 21 15
Selection Sort Example (cont.) 1 3 5 9 12 10 21 15 10 at index 5 Find the smallest value between s[4] and s[7]: Exchange s[4] and s[5] 1 3 5 9 10 12 21 15 12 at index 5 Find the smallest value between s[5] and s[7]: Exchange s[5] and s[5] 1 3 5 9 10 12 21 15 15 at index 6 Find the smallest value between s[6] and s[7]: Exchange s[6] and s[7] 1 3 5 9 10 12 15 21
Pseudo Code for Selection Sort • Given s[MAX_SIZE], size; • for i = 0 to size - 2 • find the index of a smallest element between s[i] and s[size - 1] • swap s[i] and s[index] What functions do we need? IndexOfMin ( int begin, int end, const DataType s[] ) Swap ( int i, int j, DataType s[] )
Sort Student List • In StudentList class, we can have the following functions: void SortByID( ) { int indexLow; for ( int i = 0; i <= numStudents - 2; i++ ) { indexLow = IndexOfMinID( i, numStudents - 1 ); Swap( i, indexLow ); } } Why don’t we need the Student object array as a parameter for IndexOfMinID and Swap? Because they are member functions of StudentList class!
IndexOfMinID function int IndexOfMinID( int begin, int end ) { int index = begin; for ( int i = begin + 1; i <= end; i ++ ) if ( stuList[i].LessThanByID( stuList[index] ) ) index = i; return index; }
Swap two elements in an array void Swap ( int i, int j ) { if ( i != j ) { stuList[i] = stuList[j]; stuList[j] = stuList[i]; } } // Is it correct? NO! both stuList[i] and stuList[j] will be the original stuList[j]! void Swap ( int i, int j ) { if ( i != j ) { Student stu = stuList[i]; stuList[i] = stuList[j]; stuList[j] = stu; } } // correct one!
Swap two variable values in general void Swap ( int num1, int num2 ) { if ( num1 != num2 ) { int temp = num1; num1 = num2; num2 = temp; } } // Is it correct? NO! The exchange does not remain after the function call, because num1 and num2 are IN parameters! void Swap ( int& num1, int& num2 ) { if ( num1 != num2 ) { int temp = num1; num1 = num2; num2 = temp; } } // Is it correct?
Insert an object to the sorted list MAX_STUDENTS = 10 stu Pseudo code to insert to an ascending ordered list: • Given s[MAX_SIZE], size; • if the object to insert is not yet in s[] • position = index of the first element larger than the object to insert • for i = size-1 to position • move s[i] to s[i+1] • s[position] = object to insert • size ++ 1 3 5 5 10 4 // Insert stu as the third // element of the list numStudents = 4
Insert an object to the sorted list int Position ( const Student& stu ) const { for ( int i = 0; i < numStudents; i ++ ) if ( stuList[i].GetID() > stu.GetID() ) return i; return numStudents; } int Insert( const Student& stu ) { if ( Find( stu ) == -1 && numStudents < MAX_STUDENTS ) { int index = Position(stu); for ( int i = numStudents; i > index; i -- ) stuList[i] = stuList[i-1]; stuList[index] = stu; numStudents ++; return 0; } return -1; }
Private or Public? • In the StudentList class: void Read( int size ) void Print() const int Find( const Student& stu ) const int Find( long stuID ) const void UpdateGPA ( long id, float gpa ) void GetStats( float& max, float& min, float& average ) const int IndexOfMinID( int begin, int end ) void Swap ( int i, int j ) void SortByID( ) int Position ( const Student& stu ) int Add( const Student& stu ) int Delete( long stuId ) int Insert( const Student& stu ) // private // private // private