//-------------------------------------------------------------------- // // Laboratory 2 ptlist.C // // SOLUTION: Array implementation of the Point List ADT // //-------------------------------------------------------------------- #include #include #include #include "ptlist.h" //-------------------------------------------------------------------- PointList:: PointList ( int maxNumber ) // Creates an empty list. Allocates enough memory for maxNumber // points (defaults to defMaxListSize). : maxSize(maxNumber), size(0), cursor(-1) { ptlist = new Point [ maxSize ]; assert ( ptlist != 0 ); } //-------------------------------------------------------------------- PointList:: ~PointList () // Frees the memory used by a list. { delete [] ptlist; } //-------------------------------------------------------------------- void PointList:: append ( Point newPoint ) // Appends newPoint to a list. If the list is empty, then newPoint is // inserted as the first (and only) point in the list. Moves the // cursor to newPoint. { int j; // Loop counter assert ( size < maxSize ); // Requires that list is not full cursor = size++; ptlist[cursor] = newPoint; } //-------------------------------------------------------------------- void PointList:: clear () // Removes all the points from a list. { size = 0; } //-------------------------------------------------------------------- int PointList:: empty () const // Returns 1 if a list is empty. Otherwise, returns 0. { return ( size == 0 ); } //-------------------------------------------------------------------- int PointList:: full () const // Returns 1 if a list is full. Otherwise, returns 0. { return ( size == maxSize ); } //-------------------------------------------------------------------- Point PointList:: getCursor () const // Returns the point marked by the cursor. { assert ( size != 0 ); // Requires that the list is not empty return ptlist[cursor]; } //-------------------------------------------------------------------- int PointList:: gotoBeginning () // If a list is not empty, then moves the cursor to the beginning of // the list and returns 1. Otherwise, returns 0. { int result; // Result returned if ( size != 0 ) { cursor = 0; result = 1; } else result = 0; return result; } //-------------------------------------------------------------------- int PointList:: gotoEnd () // If a list is not empty, then moves the cursor to the end of the // list and returns 1. Otherwise, returns 0. { int result; // Result returned if ( size != 0 ) { cursor = size - 1; result = 1; } else result = 0; return result; } //-------------------------------------------------------------------- int PointList:: gotoNext () // If the cursor is not at the end of a list, then moves the cursor // to the next point in the list and returns 1. Otherwise, returns 0. { int result; // Result returned if ( cursor != size-1 ) { cursor++; result = 1; } else result = 0; return result; } //-------------------------------------------------------------------- int PointList:: gotoPrior () // If the cursor is not at the beginning of a list, then moves the // cursor to the preceeding point in the list and returns 1. // Otherwise, returns 0. { int result; // Result returned if ( cursor != 0 ) { cursor--; result = 1; } else result = 0; return result; } //-------------------------------------------------------------------- void PointList:: showStructure () const // Outputs the points in a list. If the list is empty, outputs // "Empty list". This operation is intended for testing/debugging // purposes only. { int j; // Loop counter if ( size == 0 ) cout << "Empty list" << endl; else { cout << "size = " << size << " cursor = " << cursor << endl; for ( j = 0 ; j < maxSize ; j++ ) cout << j << "\t"; cout << endl; for ( j = 0 ; j < size ; j++ ) cout << "(" << ptlist[j].x << "," << ptlist[j].y << ")\t"; cout << endl; } }