Chapter 13 Array-Based List
-
Upload
india-keith -
Category
Documents
-
view
23 -
download
2
description
Transcript of Chapter 13 Array-Based List
1
Chapter 13 Array-Based List
2
List
Static List: no adding or deletingDynamic List: can add or delete items from
the listBoth static and dynamic lists:
linear search, update item valuesOnly dynamic list:
add, and delete an element
3
Add an object to the list
Student stuList[MAX_STUDENTS];int numStudents;
numStudents = 4
MAX_STUDENTS = 10stu
int Add( const Student& stu ) { if ( Find( stu ) == -1 && numStudents < MAX_STUDENTS ) { stuList[numStudents] = stu ; numStudents ++; return 0; } return -1; }
numStudents = 5
4
Delete an object from the list
numStudents = 4
MAX_STUDENTS = 10
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; }
// Delete the Student object// with ID 1002
numStudents = 3
5
Sorted List
How to sort an unsorted list?
912
1 5 310
21
15
1 3 5 910
12
15
21
Unsorted List:
Sorted List (Ascending):
21
15
12
10
9 5 3 1Sorted List (Descending):
Selection Sort Example9
12
1 5 310
21
15
Find the smallest value between s[0] and s[7]:
s[]:
112
9 5 310
21
15
Exchange s[0] and s[2]1 at index 2
Find the smallest value between s[1] and s[7]:Exchange s[1] and s[4]
3 at index 4
1 3 9 512
10
21
15
Find the smallest value between s[2] and s[7]:Exchange s[2] and s[3]
5 at index 3
1 3 5 912
10
21
15
Find the smallest value between s[3] and s[7]:Exchange s[3] and s[3]
9 at index 3
1 3 5 912
10
21
15
6
7
Selection Sort Example (cont.)1 3 5 9
12
10
21
15
Find the smallest value between s[4] and s[7]:Exchange s[4] and s[5]
10 at index 5
1 3 5 910
12
21
15
Find the smallest value between s[5] and s[7]:Exchange s[5] and s[5]
12 at index 5
1 3 5 910
12
21
15
Find the smallest value between s[6] and s[7]:Exchange s[6] and s[7]
15 at index 6
1 3 5 910
12
15
21
8
Pseudo Code for Selection SortGiven 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[] )
9
Sort Student ListIn 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!
10
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; }
11
Swap two elements in an array
void Swap ( int i, int j ) { if ( i != j ) { Student stu = stuList[i]; stuList[i] = stuList[j]; stuList[j] = stu; } } // correct one!
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]!
12
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?
13
Insert an object to the sorted list
1 3 5
numStudents = 4
MAX_STUDENTS = 10
4
stu10
5
// Insert stu as the third// element of the list
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 ++
14
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;}
int Position ( const Student& stu ) const{ for ( int i = 0; i < numStudents; i ++ ) if ( stuList[i].GetID() > stu.GetID() ) return i; return numStudents;}
Insert an object to the sorted list
15
Private or Public?In the StudentList class:void Read( int size )void Print() constint Find( const Student& stu ) constint Find( long stuID ) constvoid UpdateGPA ( long id, float gpa )void GetStats( float& max, float& min, float& average ) constint 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