Upload
aaron-barton
View
43
Download
3
Embed Size (px)
DESCRIPTION
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 - PowerPoint PPT Presentation
Citation preview
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
• How to do Linked Lists:– Always keep a pointer to the head– Always point ‘tail’ element’s NEXT
to NULL
HEAD*
ListElem
NEXT*
ListElem
NEXT*
ListElem
NEXT*. . .
NULL
Linked List: Adding
• Adding to a linked list:
1. Create new n:ListElem
2. n:ListElem.NEXT = h:NEXT
3. h:NEXT =n:ListElem
b:ListElem
b:NEXT*hNEXT*
n:ListElem
n:NEXT*
Linked List: Subtracting
• Subtracting
1. h:NEXT = n:ListElem.NEXT
2. free n:ListElem
b:ListElem
b:NEXT*h:NEXT*
n:ListElem
n: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 ListHEAD*
ListElem
NEXT*
ListElem
NEXT*
ListElem
NEXT*. . .
NULL
PREV* PREV* PREV*
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)!
Binary Tree
node
RIGHT*LEFT*
ROOT*
node
RIGHT*LEFT*
node
RIGHT*LEFT*
N-ary Tree
ROOT*
node
_a* _b* _c* _n*...
node
_a* _b* _c* _n*...
node
_a* _b* _c* _n*...
node
_a* _b* _c* _n*...
Homework
• See website