23-All-About-Linked-Lists

23-All-About-Linked-Lists - CS106X Autumn 2010 Handout 23...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS106X Handout 23 Autumn 2010 October 22 nd , 2010 Fun With Linked Lists This handout was written by Julie Zelenski and Jerry Cain. This material doesn’t appear standalone anywhere in the reader, and linked lists are important enough as data structures that we should be discussing them as legitimate top- level containers. Eventually we’ll see that linked lists (and more generically, linked structures) are used to back the Queue , the Map , and the Set ). But linked lists exist outside the container framework, so it’s important we understand the mechanics involved in building, iterating over, and otherwise manipulating them. Simple Linked Lists First, here's our node definition: struct addressRec { string name; string address; string phone; addressRec *next; }; Here are the basic operations for creating and printing a single addressRec cell: addressRec *GetNewAddress() { cout << "Enter name (or return to quit): "; getline(cin, name); if (name == "") return NULL; addressRec *newOne = new addressRec; newOne->name = name; cout << "Enter address: "; getline(cin, newOne->address); cout << "Enter phone: "; getline(cin, newOne->phone); newOne->next = NULL; // initialize field to show no one follows return newOne; } void PrintAddress(addressRec *person) { cout << person->name << endl; cout << person->address << endl; cout << person->phone << endl; } Let's start with the first (unsorted) version of the linked address list. In creating the list, we prepend each new entry to the front of the list, since that's the easiest.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
addressRec *BuildAddressBook() { addressRec *list = NULL; while (true) { addressRec *newOne = GetNewAddress(); if (newOne == NULL) return list; newOne->next = list; // attach rest of list to new cell list = newOne; // new cell becomes the head of list } } Note that in freeing the list we have to be careful to not access the current cell after we have freed it. void FreeAddressBook(addressRec *list) { while (list != NULL) { addressRec *next = list->next; // save next ptr before we free this one delete list; list = next; } } We can use the idiomatic for loop, linked list traversal to print each address cell: void PrintAddressBook(addressRec *list) { for (addressRec *cur = list; cur != NULL; cur = cur->next) PrintAddress(cur); } A linear search is used to look up an entry by name: addressRec *FindPerson(addressRec *list, string name) { for (addressRec *cur = list; cur != NULL; cur = cur->next) if (name == cur->name) return cur; return NULL; } Now, we re-consider our decision to keep the list unordered and decide instead to maintain the list in alphabetical order. With this change, we can re-write our FindPerson function to be a bit smarter. We use string::compare
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

23-All-About-Linked-Lists - CS106X Autumn 2010 Handout 23...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online