#include #include using namespace std; struct Node { double data; Node *next; }; void add2front(Node *&list, double value); void printList(Node *list); int length(Node *list); Node* findMatchingNode(Node *list, double key); void add2back(Node * &list, double newVal); void deleteFirstNode (Node * &list); int main() { // make an empty list Node *list; list = 0; cout << "Length is " << length(list) << endl; // add some things to list add2front(list, 10); add2front(list, 9.5); add2front(list, 9.0); add2front(list, 5.0); deleteFirstNode(list); // print the list printList(list); cout << "Length is " << length(list) << endl; return 0; } void deleteFirstNode (Node * &list) { Node *temp; temp = list; // remember first node list = list->next; // bypass first node delete temp; // delete first node } // Recursive function to print the list void printList(Node* p) { if (p!=0){ printList(p->next); cout << p ->data << endl; } } // returns the length of the list, 0 if empty int length(Node *list) { // base case if (list == NULL) { return 0; } // recursive step int length_of_rest = length(list->next); int total = 1 + length_of_rest; cout << "right now, length is : " << total << endl; return total; } // returns the length of the list, 0 if empty int length_iterative(Node *list) { int count =0; while (list != NULL) { count++; // count this node list = list->next; // bump to next } return count; } // 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; } 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; }