#if 1 #include #include using namespace std; struct Node { double data; Node *next; }; void add2front(Node *&list, double value); void printList(Node *list); void printListRecursiveBackToFront(Node* list); double calcSum(Node *list); double findMin(Node *list); Node* findMatchingNode(Node *list, double key); void add2back(Node * &list, double newVal); int count(Node*); void deleteFirst(Node* &); void insertInOrder(Node* &, double); void insertInOrderShorter(Node* &list, double val); void insertInOrderRecursive(Node* & list, double val); int main() { // make an empty list Node *list; list = NULL; // add some things to list add2front(list, 13.5); add2front(list, 12.4); add2front(list, 7.0); add2front(list, 3.14); insertInOrderShorter(list, 9); insertInOrder(list,24); insertInOrder(list,1); // print the list cout << "List using non-recursive print: "<data = value; // (*temp).data = value; temp->next = list; //make the list to point to new node list = temp; } // Prints all the data in the list void printList(Node *list) { Node* temp; temp = list; //point to the first element /* cout << temp->data << endl; //print 1st temp = temp->next; cout << temp->data << endl; //print 2nd temp = temp->next; cout << temp->data << endl; // print3rd */ //shorter version - works for any length cout << "Print using while " << endl; while(temp != NULL) { cout << temp->data << endl; temp = temp->next; } //using for cout << "Print using for " << endl; for(Node* temp = list; temp != NULL; temp = temp->next) { cout << temp->data << endl; } } // Returns the sum of all the elements in the list double calcSum(Node *list) { double sum = 0; //using for for(Node* temp = list; temp != NULL; temp = temp->next) { sum = sum + temp->data; } return sum; } // Returns the minimum value in the list // Assumes list is not empty! double findMin(Node *list) { double min = list->data; for(Node* temp = list; temp!=NULL; temp = temp->next) { if(temp->data < min) { min = temp->data; } } return min; } Node* findMatchingNode(Node *list, double key) { return NULL; } void add2back(Node * &list, double newVal) { } void printListRecursiveBackToFront(Node *list) { if (list != NULL) { printListRecursiveBackToFront(list->next); cout << list->data << endl; } } //compute length of list int count(Node* list) { int counter; counter = 0; Node* temp; temp = list; while(temp != NULL) { counter++; //count this element temp=temp->next; //go to next } return counter; } //compute length of list int countRecursive(Node* list) { //base case if (list == NULL) return 0; //recursive case int counter; counter = 1 + countRecursive(list->next); return counter; } //delete first node void deleteFirst(Node* &list) { if (list == NULL) return; Node* temp; temp = list; //keep a pointer list= list->next; //point to second delete temp; //free memory temp = NULL; //good practice } //insert a new value in a ordered list void insertInOrder(Node* &list, double val) { //if list empty if (list == NULL) { Node* newNode = new Node; newNode->data = val; newNode->next = list; list = newNode; return; } //if need to insert as first if (val < list->data) { //insert here Node* newNode = new Node; newNode->data = val; newNode->next = list; list = newNode; return; } //find its place Node* t = list; while(t->next != NULL && val > t->next->data) { t = t->next; } //add after t Node* newNode = new Node; newNode->data = val; newNode->next = t->next; t->next = newNode; t=NULL; } //shorter version of insert in order (use call to add2front) void insertInOrderShorter(Node* &list, double val) { //if need to insert as first element if (list == NULL || val < list->data) { add2front(list,val); return; } //find its place Node* t = list; while(t->next != NULL && val > t->next->data) { t = t->next; } //add after t (before t->next) add2front(t->next, val); } //recursive implementation for insert in order void insertInOrderRecursive(Node* & list, double val) { //base case if (list == NULL || val < list->data) { add2front(list,val); return; } //recursive case insertInOrderRecursive(list->next, val); } #endif