

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


void PrintEntry(Entry *entry) { cout << entry->name << " " << entry->phone << endl; } void PrintList(Entry *list) { for (Entry *cur = list; cur!= NULL; cur = cur->next) PrintEntry(cur); } Idiomatic loop to iterate over list, compare to for (int i = 0; i < n; i++)
void PrintList(Entry *list) { for (Entry *cur = list; cur!= NULL; cur = cur->next) PrintEntry(cur); }
void PrintList(Entry *list) { if (list != NULL) { PrintEntry(list); PrintList(list->next); } } What happens if we switch the order of these two lines?
void Deallocate(Entry *list) { if (list != NULL) { Deallocate(list->next); delete list; } } int Length(Entry *list) { if (list == NULL) return 0; else return 1 + Length(list->next); }
void Prepend(Entry *ent, Entry *first) { ent->next = first; first = ent; // BUGGY! } Entry *BuildAddressBook() { Entry *listHead = NULL; while (true) { Entry *newOne = GetNewEntry(); if (newOne == NULL) break; Prepend(newOne, listHead); } return listHead; }
void Prepend(Entry *ent, Entry * & first) { ent->next = first; first = ent; } Entry *BuildAddressBook() { Entry *listHead = NULL; while (true) { Entry *newOne = GetNewEntry(); if (newOne == NULL) break; Prepend(newOne, listHead); } return listHead; }
What is true after the loop exits? void InsertSorted(Entry * &list, Entry * newOne) { Entry *cur; for (cur=list; cur!= NULL; cur=cur->next){ if (newOne->name < cur->name) break; } Kalev 123 Pine 555- Lauri 204 Elm 555- Matt 204 Elm 555- Thomas 89 Oak 555- Rein 19 Main 555-