/** * Get a list of integers and output a reversed list. * * @Author Mario Lopez * @Version 1.0 * @Date Feb 23 2007 */ #include #include #include using namespace std; // Function to read an inputted number int readNumber(int &i) { char tempChar; tempChar = cin.get(); // Makes sure the inputted stream is an integer if( tempChar > '0' && tempChar < '9' ) cin.unget(); else if( tempChar == '\n') return 0; cin >> i; return 1; } main() { list intList, reverseList; int tempInt, totalNum = 0; cout << "Input a list of integers to be reversed:" << endl; // Call the function to read in the numbers and put them on the list while(readNumber(tempInt)) intList.push_back(tempInt); // Create variable "foo" of type "list iterator" list::iterator foo; // Go through the list of numbers and add them to the reversed list for( foo = intList.begin(); foo != intList.end(); foo++) reverseList.push_front(*foo); cout << "The reversed list:" << endl; for( foo = reverseList.begin(); foo != reverseList.end(); foo++ ) cout << *foo << " "; cout << endl; return 0; }