1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
class XYZ
{
private:
double x;
double y;
double z;
XYZ* next;
public:
void report()
{
cout <<x <<" " <<y <<" " <<z << " " ;
}
// all coordinates have to be at least +1; Constructor to initialize node
XYZ( double x1, double y1, double z1 )
{
x = pushup(x1);
y = pushup(y1);
z = pushup(z1);
next = NULL;
}
// returns number at least +1
double pushup( double x )
{
if( x<1) { x = 1; }
return x;
}
// access
void setNext( XYZ *p ) { next = p; } // mutator: sets the value of Next
XYZ* getNext() { return next; } // accessor
};
// prototype
void addXYZ( double, double, double, XYZ*&);
void report( XYZ*);
int main () {
XYZ *coordList; // head of the list
coordList = NULL; // pointer is initialized as empty
addXYZ( 4 , 5 , 6, coordList );
addXYZ( 3 , 20 , 88, coordList );
addXYZ( 66 , 12 , 234, coordList );
report( coordList );
return 0;
}
// function to add an XYZ to the list
void addXYZ( double x, double y, double z, XYZ* &head )
{
XYZ* pCoord = new XYZ( x, y, z );
pCoord->setNext( head ); //pCoord->next = coordList;
head = pCoord;
}
// takes a list of XYZ objects and reports their values
void report( XYZ* head )
{
XYZ* current = head;
while ( current != NULL )
{
current->report(); // report the number at that node
current = current->getNext(); // move on to next node to report until it reaches the end (NULL)
}
}
|