Today’s announcements: ◮ PA1 out, due Sep 27, 23:59 ◮ Lecture videos out on YouTube Today’s Plan ◮ Memory model ◮ Linked lists ◮ List Abstract Data Type http://zeptobars.ru/en/read/how-to-open-microchip-asic-what-inside licensed under Creative Commons Attribution 3.0 Unported. 1 / 9
Pointers are memory addresses int * p, q; // what type is q? int x; p = &x; *p = 6; cout << x; cout << p; Write a statement that outputs the value of x using the variable p : 2 / 9
More fun with pointers int *p, *q; p = new int; q = p; *q = 8; cout << *p; // What is output? q = new int; *q = 9; p = NULL; // Do you like this? delete q; q = NULL; // Do you like this? Memory leak: Deleting a null pointer: Dereferencing a null pointer: 3 / 9
Using memory addresses in data structures template <class LIT> struct Node{ LIT data; Node * next; Node(LIT d, Node *p=NULL):data(d),next(p){} }; Write code to create these memory configurations (in order): 8 ∅ 8 ∅ 6 head head 4 / 9
Example 1 insertAtFront(head, cow); pig ∅ dog cat head void insertAtFront(Node * & curr, LIT e) { } Running time? 5 / 9
Example 2 3 ∅ 8 4 2 6 head void printReverse(Node * curr) { } Running time? 6 / 9
Example 3 3 ∅ 8 4 2 6 5 head void printOddRev(Node * curr) { } Running time? 7 / 9
Example 4 3 ∅ 8 4 2 6 5 head void reverse(Node * & curr) { } Running time? 8 / 9
Remove node from list given pointer 3 ∅ 8 4 2 6 5 head void removeNode(Node * & head, Node * curr) { } Running time? 9 / 9
Remove node from list given pointer 3 ∅ 8 4 2 6 5 head void removeNode(Node * & head, Node * curr) { } Running time? Constant time hack (When doesn’t this work?) curr->data = curr->next->data; curr->next = curr->next->next; 9 / 9
Recommend
More recommend