// CS2310 Exercise 7 // // Name ____________________ SSN ____________________ // // Chapter 13 // p761 - 2: To this chapter's List class, we wish to // add a value-returning member function named // Occurrences that receives a single parameter, item, // and returns the number of times item occurs in the // list. // // IMPLEMENTATION FILE ARRAY-BASED LIST ( list.cpp ) #include "list.h" #include using namespace std; int List :: Length ( ) const // Post: Function value == length { return length ; } bool List :: IsFull ( ) const // Post: Function value == true, // if list == MAX_LENGTH // == false, otherwise { return ( length == MAX_LENGTH ) ; } List :: List ( ) // Constructor // Post: length == 0 { length = 0 ; } void List :: Insert ( /* in */ ItemType item ) // Pre: length < MAX_LENGTH && item is assigned // Post: data[length@entry] == item && // length == length@entry + 1 { data [ length ] = item ; length++ ; } bool List :: IsEmpty ( ) const // Post: Function value == true, if length == 0 // == false, otherwise { return ( length == 0 ) ; } bool List :: IsPresent ( /* in */ ItemType item ) const // Searches the list for item, reporting // whether it was found // Post: Function value == true, // if item is in data [ 0 . . length-1 ] // == false, otherwise { int index = 0 ; while ( index < length && item != data [ index ] ) index++ ; return ( index < length ) ; } void List :: Delete ( /* in */ ItemType item ) // Pre: length > 0 && item is assigned // Post: IF item is in data array at entry // First occurrence of item is no longer in array // && length == length@entry - 1 // ELSE // length and data array are unchanged { int index = 0 ; while ( index < length && item != data [ index ] ) index++; // if item found, move last element into item’s place if ( index < length ) { data [ index ] = data [length - 1 ] ; length-- ; } } void List :: Print ( ) // Prints the list // Post: Contents of data [0 . . length-1 ] // have been output { int index ; for ( index = 0 ; index < length ; index++ ) { if (index%10==0 && index!=0) cout << endl; cout << data [ index ]; } cout << endl; } void List :: SelSort ( ) // Sorts list into ascending order using selection sort { ItemType temp ; int passCount ; int sIndx ; int minIndx ; // index of minimum so far for ( passCount = 0 ; passCount < length - 1 ; passCount++ ) { minIndx = passCount ; // find index of smallest of data [ passCount . . length-1 ] for ( sIndx = passCount + 1 ; sIndx < length ; sIndx++ ) if ( data [ sIndx ] = data [ minIndx ] ) minIndx = sIndx ; temp = data [ minIndx ] ; // swap data [ minIndx ] = data [ passCount ] ; data [ passCount ] = temp ; } } int List::Occurrences ( ItemType item ) const { // Write the function definition here. } int main () { List list; int i; // Insert items into the list for (i=0; i<10; i++) list.Insert(i); for (i=0; i<10; i++) list.Insert(i); // Print out the list list.Print(); // Find out number of occurrences of 5 cout << "Number of occurrences of 5 is: " << list.Occurrences(5) << endl; cout << endl; // Delete one 5 cout << "Deleting one 5" << endl; list.Delete(5); // Find out number of occurrences of 5 cout << "Number of occurrences of 5 is: " << list.Occurrences(5) << endl; return 0; }