Displaydata applied programming cout data endl endl

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: curr != 0) { void DisplayData(int& data) void DisplayData(int { DisplayData(curr->data); // a function to display the data curr = curr->next; curr } As the argument to DisplayData() iis an s As DisplayData instance of the type int, we would need a int we DisplayData() for such a type. DisplayData Applied Programming cout << data << endl; << endl // print out an int // int } But, we have to provide the prototype for But, DisplayData() iin our class interface. n DisplayData How do we do that for any datatypeApplied Programming datatype? To be able to find where a new item should To go, we need a comparison routine. go, Thus we have a template function This time, let's write a comparison which This operates like strcmp(). strcmp Here's the function, as supplied by the user, for Here's int types. int That is, the function will return -1 if the first argument is before the second in the list order specified 0 if the arguments are equal +1 otherwise Applied Programming The comparisons can be quite complex. For example, consider our struct Video { char Title[40]; ... int Year; }; Our list order is defined as ascending alphabetic Our by Title. If titles are the same, Year iis checked Title If Year s for descending order. for That is Hamlet 1997 That Hamlet goes before Hamlet 1986 Applied Programming goes Hamlet Back to the insertion. Suppose our new data is Back newdata. Here's how we find where it goes. newdata set the pointer to the current node to head while the current node pointer actually points at something and we haven't yet found where to insert if new data goes before the current node's data we've found the insert point otherwise set the current node pointer to next node pointer if we have found an insertion point new data goes before the current node otherwise the new data goes on the end of the list int ListDataCmp(const int&, const int&); &, int int ListDataCmp(const int& data1, const int& data2) const int { int result = 1; if (data1 < data2) result = -1; result else if (data1 == data2) result = 0; return res...
View Full Document

This document was uploaded on 04/07/2014.

Ask a homework question - tutors are online