130 likes | 242 Views
Common Complex C Structures. C-minar 6 July 2004. To cover today:. Linked list Doubly linked list Binary Tree N-ary Tree. Linked List. What if you need to store ordered information where finding the ‘next’ element is necessary where insertion should be painless
E N D
Common ComplexC Structures C-minar 6 July 2004
To cover today: • Linked list • Doubly linked list • Binary Tree • N-ary Tree
Linked List • What if you need to store • ordered information • where finding the ‘next’ element is necessary • where insertion should be painless • Then, the linked list is for you
Linked List HEAD* • How to do Linked Lists: • Always keep a pointer to the head • Always point ‘tail’ element’s NEXT to NULL ListElem ListElem ListElem . . . NEXT* NEXT* NEXT* NULL
n:ListElem n:NEXT* hNEXT* Linked List: Adding • Adding to a linked list: • Create new n:ListElem • n:ListElem.NEXT = h:NEXT • h:NEXT =n:ListElem b:ListElem b:NEXT*
h:NEXT* n:ListElem n:NEXT* Linked List: Subtracting • Subtracting • h:NEXT = n:ListElem.NEXT • free n:ListElem b:ListElem b:NEXT*
Linked List • What to watch out for: • Dangling pointers (from adding/subtracting in the wrong order) • Not pointing to NULL at the end • Accidentally losing your HEAD*
Doubly-Linked List • What if you need to store • A list that is traversable in more than one way (i.e., backward and forward) • To be able to arbitrarily reorder the list • To be able to have a linked list, yet see behind you.
Doubly-Linked List HEAD* ListElem ListElem ListElem . . . PREV* NEXT* PREV* NEXT* PREV* NEXT* NULL
Binary Tree • What if you need to • rapidly get to values in a table • sort these values to be easily accessed • drastically reduce time required to access data • be able to leap tall buildings in a single bound • Binary tree is what you nee(d)!
node node node LEFT* LEFT* LEFT* RIGHT* RIGHT* RIGHT* Binary Tree ROOT*
node node node node _a* _a* _a* _a* _b* _b* _b* _b* _c* _c* _c* _c* ... ... ... ... _n* _n* _n* _n* N-ary Tree ROOT*
Homework • See website