CS120 Day 8: Linked Lists; testing ------------------------------------- (really working with pointers and dynamic memory allocation) LINKED LIST =========================================== Basic Concept - nodes contain data and links to neighboring nodes - "head" of list is a reference to the first node (or null if empty) - can dynamically increase and decrease as data is added and removed - can add or delete with a few constant operations (no shifting) once the place in the list is found - does not have random access to individual data elements like an array - singly linked list with head pointer to first node is simplest form - self-referential structures: each node points to another node which is the head of a (sub)list head |node1 |node2 |node3 ... |nodex |nodeLast --> | --> | --> | --> ... | --> | null http://en.wikipedia.org/wiki/Linked_list code files: list.h, list.c, listDriver.c - singly linked list - create list (insert at head, or insert at end) - add node to list (insert at head, or insert at end) - print list (forward) - clear list (must free memory!) - search & delete element functions - printReverse - recursion works well, especially in C - when passing to functions, if want pointer to change, must pass address of pointer (pointer to pointer) Implementation Variations - keep "tail" reference to the last node, makes adding to the end fast - keep sentinel empty (dummy) nodes at beginning and end to avoid special cases in code - make doubly linked list with forward and backwards links - any combination of the above - circular list: the last node points to the first Testing - blackbox - make sure all cases you need to handle are included - for linked list: several cases for all functions: - empty list - not found/there - first node - last node - middle node - whitebox - make sure all code statements are executed (gcov) - both are very important!