CS110_10aDoubleLinkedLists

CS110_10aDoubleLinkedLists - EECS110 10a Build Things with...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: EECS110: 10a Build Things with Structures: Linked Lists Jack Tumblin [email protected] . 1 Pointers can Arrange Structures Pointers can link together structures Linked structures can have any shape: A `chain' or `loop' (linked list) A `tree' or a `graph' A `grid' or `matrix' or `table' Linked structures are easy, fast to change 2 Example: chain or `Linked List' Define a data structure called nameT that has an element that points to its own kind: typedef struct nameT{ char msg[80]; struct nameT *pNext; } nameT; msg[80] pNext int main() { nameT list[3]; int k; for(k=0; k<20; k++) list[k].pNext = &(list[(k+1)%20]); } 3 // name of person // next person on list Example: chain or `Linked List' Point to other objects of same type (nameT) to to make a chain or a loop: typedef struct nameT{ char msg[80]; struct nameT *pNext; } nameT; list[0] msg[80] pNext int main() { nameT list[3]; int k; // name of person // next person on list list[1] msg[80] pNext list[2] msg[80] pNext // several `nameT' objects for(k=0; k<3; k++) list[k].pNext = &(list[(k+1)%3]); } 4 Traverse a Chain of Pointers Let pointer `pNow' set current object, Copy address `pNext' to `pNow': pNow list[0] list[1] nameT list[3]; msg[80] msg[80] nameT *pNow; pNext pNext int k; . . . for(k=0; k<3; k++) list[k].pNext = &(list[(k+1)%3]); pNow = &(list[0]); // start here . . . pNow = pNow->pNext; // move on. pNow = pNow->pNext; // move on. pNow = pNow->pNext; // move on. list[2] msg[80] pNext 5 Traverse a Chain of Pointers Let pointer `pNow' set current object, Copy address `pNext' to `pNow': pNow list[0] list[1] nameT list[3]; msg[80] msg[80] nameT *pNow; pNext pNext int k; . . . for(k=0; k<3; k++) list[k].pNext = &(list[(k+1)%3]); pNow = &(list[0]); // start here . . . pNow = pNow->pNext; // move on. pNow = pNow->pNext; // move on. pNow = pNow->pNext; // move on. } list[2] msg[80] pNext 6 Traverse a Chain of Pointers Let pointer `pNow' set current object, Copy address `pNext' to `pNow': pNow list[0] list[1] nameT list[3]; msg[80] msg[80] nameT *pNow; pNext pNext int k; . . . for(k=0; k<3; k++) list[k].pNext = &(list[(k+1)%3]); pNow = &(list[0]); // start here . . . pNow = pNow->pNext; // move on. pNow = pNow->pNext; // move on. pNow = pNow->pNext; // move on. list[2] msg[80] pNext 7 Traverse a Chain of Pointers Let pointer `pNow' set current object, Copy address `pNext' to `pNow': pNow list[0] list[1] nameT list[3]; msg[80] msg[80] nameT *pNow; pNext pNext int k; . . . for(k=0; k<3; k++) list[k].pNext = &(list[(k+1)%3]); pNow = &(list[0]); // start here . . . pNow = pNow->pNext; // move on. pNow = pNow->pNext; // move on. pNow = pNow->pNext; // move on. list[2] msg[80] pNext 8 Traverse a Chain of Pointers Let pointer `pNow' set current object, Copy address `pNext' to `pNow': pNow list[0] list[1] nameT list[3]; msg[80] msg[80] nameT *pNow; pNext pNext int k; . . . for(k=0; k<3; k++) list[k].pNext = &(list[(k+1)%3]); pNow = &(list[0]); // start here . . . pNow = pNow->pNext; // move on. pNow = pNow->pNext; // move on. pNow = pNow->pNext; // move on. list[2] msg[80] pNext 9 Going Backwards? Easy: use TWO pointers per object; pNext, pPrev: typedef struct nameT{ char msg[80]; struct nameT *pNext; struct nameT *pPrev; } namedT; . . . // name of person // next person on list // previous list entry msg[80] pNext pPrev 10 Result: Doubly-linked List Link them together into a loop: typedef struct nameT{ char msg[80]; // name of person struct nameT *pNext; // next person on list struct nameT *pPrev; // previous list entry } nameT; . . . namedT List[3]; for(k=0; k<3; k++) { list[k].pNext = &(list[(k+1 )%3]); list[k].pPrev = &(list[(k-1+3)%3]); // wrap backwards } msg[80] msg[80] msg[80] pNext pNext pNext pPrev pPrev pPrev list[0] list[1] list[2] 11 Back-Track? Doubly-linked List Again, let pointer `pNow' set current object, Copy address `pPrev' to `pNow': namedT list[3]; namedT *pNow; . . . pNow = &(list[0]); pNow msg[80] pNext pPrev list[0] msg[80] pNext pPrev list[1] msg[80] pNext pPrev list[2] 12 Back-Track? Doubly-linked List Again, let pointer `pNow' set current object, Copy address `pPrev' to `pNow': namedT namedT . . . pNow = pNow = list[3]; *pNow; &(list[0]); pNow->pPrev; pNow msg[80] pNext pPrev list[0] msg[80] pNext pPrev list[1] msg[80] pNext pPrev list[2] 13 Back-Track? Doubly-linked List Again, let pointer `pNow' set current object, Copy address `pPrev' to `pNow': namedT namedT . . . pNow = pNow = list[3]; *pNow; &(list[0]); pNow->pPrev; pNow msg[80] pNext pPrev list[0] msg[80] pNext pPrev list[1] msg[80] pNext pPrev list[2] 14 Back-Track? Doubly-linked List Again, let pointer `pNow' set current object, Copy address `pPrev' to `pNow': namedT namedT . . . pNow = pNow = pNow = list[3]; *pNow; &(list[0]); pNow->pPrev; pNow->pPrev; pNow msg[80] pNext pPrev list[0] msg[80] pNext pPrev list[1] msg[80] pNext pPrev list[2] 15 Back-Track? Doubly-linked List Again, let pointer `pNow' set current object, Copy address `pPrev' to `pNow': namedT namedT . . . pNow = pNow = pNow = pNow = list[3]; *pNow; &(list[0]); pNow->pPrev; pNow->pPrev; pNow->pPrev; pNow msg[80] pNext pPrev list[0] msg[80] pNext pPrev list[1] msg[80] pNext pPrev list[2] 16 Doubly-Linked List: Delete? How would you delete the pNow item from a doubly-linked list? pNow msg[80] pNext pPrev list[0] msg[80] pNext pPrev list[1] msg[80] pNext pPrev list[2] 17 Doubly-Linked List: Delete? How would you delete the pNow item from a doubly-linked list? 1) Remove pNow from pNext loop and pPrev loop pNow->pPrev->pNext = pNow->pNext; pNow msg[80] pNext pPrev list[0] msg[80] pNext pPrev list[1] msg[80] pNext pPrev list[2] 18 Doubly-Linked List: Delete? How would you delete the pNow item from a doubly-linked list? 1) Remove pNow from pNext loop and pPrev loop pNow->pPrev->pNext = pNow->pNext; pNow msg[80] pNext pPrev list[0] msg[80] pNext pPrev list[1] msg[80] pNext pPrev list[2] 19 Doubly-Linked List: Delete? How would you delete the pNow item from a doubly-linked list? 1) Remove pNow from pNext loop and pPrev loop: pNow->pPrev->pNext pNow->pNext->pPrev = = pNow->pNext; pNow->pPrev; pNow msg[80] pNext pPrev list[0] msg[80] pNext pPrev list[1] msg[80] pNext pPrev list[2] 20 Doubly-Linked List: Delete? How would you delete the pNow item from a doubly-linked list? 1) Delete pNow from pNext loop and pPrev loop: pNow->pPrev->pNext pNow->pNext->pPrev = = pNow->pNext; pNow->pPrev; pNow msg[80] pNext pPrev list[0] msg[80] pNext pPrev list[1] msg[80] pNext pPrev list[2] 21 Doubly-Linked List: Delete? How would you delete the pNow item from a doubly-linked list? 1) Delete pNow from pNext loop and pPrev loop: pNow->pPrev->pNext pNow->pNext->pPrev pNow->pNext pNow->pPrev = = = = pNow->pNext; pNow->pPrev; 2) And kill pNow pointers: NULL; NULL; msg[80] pNext pPrev list[0] pNow msg[80] pNext pPrev list[1] msg[80] pNext pPrev list[2] 22 Other Things You Can Build Stack Binary Trees K-ary Trees Arbitrary Trees Graphs Queues ... 23 ...
View Full Document

This note was uploaded on 10/05/2011 for the course COMPUTER S 110-1 taught by Professor Tumblin during the Spring '11 term at Northwestern.

Ask a homework question - tutors are online