// p.167 // // Unsorted List implemented with linked list // #include using namespace std; struct NodeType { int info; NodeType* next; }; class UnsortedList { int length; NodeType* listData; NodeType* pos; public: // p.171 UnsortedList() { length = 0; listData = NULL; } void InsertItem( int item ) { // p.170 insert to the top NodeType* temp = new NodeType; temp -> info = item; temp -> next = listData; listData = temp; } void GetCurrentItem(int& item) { if ( pos == NULL ) pos = listData; item = pos -> info; } // p.177 void GetNextItem(int& item) { if( pos == NULL ) pos = listData; else pos = pos->next; item = pos->info; } void ResetList() // pos points to the first { pos = listData; } void PrintFromCurrent() const { NodeType* temp = pos; while( temp != NULL) { cout << temp->info << " "; temp = temp->next; } cout << endl; } }; int main() { // Q: insert 12, 7 to the list? UnsortedList myList; myList.InsertItem(7); myList.InsertItem(12); // Q: current pos points to the first? myList.ResetList(); // Output the element of the second node? int item; myList.GetNextItem(item); cout << "The next item is: " << item << endl; myList.ResetList(); // Q: out the first node? myList.GetCurrentItem(item); cout << "The first item is: " << item << endl; myList.ResetList(); myList.PrintFromCurrent(); return 0; }