#include #include using namespace std; struct Node { double data; Node *next; }; void add2front(Node *&list, double value); void printList(Node *list); double findMin(Node *list); Node* findMatchingNode(Node *list, double key); void add2back(Node * &list, double newVal); int main() { // make an empty list Node *list; list = 0; cout << "Min is " << findMin(list) << endl; // add some things to list add2front(list, 3.14); add2front(list, 7.0); add2front(list, 13.5); add2front(list, 2.4); // print the list printList(list); cout << "Min is " << findMin(list) << endl; return 0; } // inserts 'value' at front of the list (head of list) void add2front(Node *&list, double value) { // create new node Node *temp = new Node; temp->data = value; // point new node to the old list temp->next = list; // update list to point to new node list = temp; } // Prints all the data in the list void printList(Node *list) { if (list == 0) return; cout << "newstuff: " << list->data << endl; // recursive step -- print rest of list! printList(list->next); } Node* findMatchingNode(Node *list, double key) { Node *ptr = list; for ( ; ptr != 0; ptr = ptr->next ) { if (ptr->data == key ) { return ptr; } } return 0; } // Prints all the data in the list void older_printList(Node *list) { Node *ptr; for (ptr = list; ptr != 0 ; ptr = ptr->next ) { cout << ptr->data << endl; // print } } // Prints all the data in the list void old_printList(Node *list) { Node *ptr; ptr = list; while (ptr != 0) { cout << ptr->data << endl; // print ptr = ptr->next; // bounce to next } } // Returns the sum of all elements in the list int calcSum(Node *list) { int sum = 0; Node *ptr = list; while (ptr != 0) { sum = sum + ptr->data; ptr = ptr->next; } return sum; } // Returns the minimum value in the list // Assumes list is not empty! double findMin(Node *list) { if (list == 0) return 0; double min = list->data; Node *ptr; for ( ptr = list ; ptr != 0; ptr = ptr->next) { if (ptr->data < min) { min = ptr->data; } } return min; }