// iter_list.cpp: navigating a list using iterators #include #include using namespace std; // print all members of the list, assuming << makes sense for T template void print_list(list& mylist) { // typename list::iterator it = mylist.begin(); typename list::reverse_iterator it = mylist.rbegin(); while (it != mylist.rend()) { cout << *(it++) << " "; } cout << endl; } /** * ----------------------------------------------------------------------------- * main body * ----------------------------------------------------------------------------- */ int main() { list mylist; // build a 10-node linked list for testing for (int i=0; i<10; i++) { mylist.push_back(i); } print_list(mylist); return 0; } struct Node { Node* next; string payload; }; /** * ----------------------------------------------------------------------------- * assume the list is already sorted, insert a new node with the given payload * into the list; return a pointer to the (potentially) new head * assume node_ptr points to an allocated node * ----------------------------------------------------------------------------- */ Node* insert_into_sorted_list(Node* head, Node* node_ptr) { if (head == NULL || node_ptr->payload < head->payload) { node_ptr->next = head; return node_ptr; } Node *prev = head, *temp = head->next; while (temp != NULL && temp->payload < node_ptr->payload) { prev = temp; temp = temp->next; } prev->next = node_ptr; node_ptr->next = temp; return head; } /** * ----------------------------------------------------------------------------- * reverse the list with given head pointer, return pointer to the new head. * ----------------------------------------------------------------------------- */ Node* reverse_sll(Node* head) { Node *prev = NULL, *temp; while (head != NULL) { temp = head->next; head->next = prev; prev = head; head = temp; } return prev; } /** * ----------------------------------------------------------------------------- * free the memory of all nodes starting from ptr down * ----------------------------------------------------------------------------- */ void free_list(Node* ptr) { Node* temp; while (ptr != NULL) { temp = ptr; ptr = ptr->next; delete temp; } } /** * ----------------------------------------------------------------------------- * delete the successor node of the node pointed to by ptr * ----------------------------------------------------------------------------- */ void del_successor(Node* ptr) { if (ptr == NULL || ptr->next == NULL) return; Node* temp = ptr->next; ptr->next = temp->next; delete temp; // always remember this } /** * ----------------------------------------------------------------------------- * search for the first occurrence of key in the list, starting from ptr * return NULL if not found, pointer to containing node is found * ----------------------------------------------------------------------------- */ Node* search(string key, Node* ptr) { while (ptr != NULL && ptr->payload != key) ptr = ptr->next; return ptr; } /** * ----------------------------------------------------------------------------- * search for the first occurrence of name in the list, starting from ptr * ----------------------------------------------------------------------------- */