Dynamic Data Structures Putting together variable size pieces Copyright © 2006-2009 Curt Hill
Introduction What are Dynamic Data Structures? Data structures that may vary dynamically at run-time This excludes ordinary arrays Includes pointer based arrays, but this is just the tip of the ice berg Not usually one pointer, but many Copyright © 2006-2009 Curt Hill
Construction We can construct a variety of data structures These come in many shapes and varieties What they usually have in common is: A class (or struct) referred to by a pointer Also containing a pointer of similar or different type Time to look at a picture Copyright © 2006-2009 Curt Hill
Some D.D.S. Heap Stack D1 D3 D5 D2 NULL D4 NULL E9 E8 NULL NULL E7 F2 F3,F4,F5 F12 F13,F14,F15 G6 Copyright © 2006-2009 Curt Hill
Commentary The shape of a dynamic data structure is limited only by: Imagination That a pointer must point at just one thing There were three shapes in previous screen Singly linked list Unbalanced binary tree An ad hoc shape with no classification Each chunk (a class or struct) had data and some pointers Copyright © 2006-2009 Curt Hill
More Commentary Various shapes are well known Programmer may create any shape that makes sense More discussion in next course For the rest of this presentation lets consider the linked list Copyright © 2006-2009 Curt Hill
Linked List Consider the following class: class linked{ int i,j; double d; linked * next; … }; On the stack we have: linked * anchor; What are the characteristics of this? Copyright © 2006-2009 Curt Hill
Linked List Characteristics A linked list is fully as powerful as an array It may be faster or slower depending on how it is used It may grow or shrink without reallocating the whole thing Insertion and deletion are a snap No disturbing adjacent members Copyright © 2006-2009 Curt Hill
Linked List Picture anchor 3 5 1.4 1.1 4 7 2 7 1.7 9 5.3 -2 NULL 4.1 14 8 4.1 Copyright © 2006-2009 Curt Hill
Linked Lists and Arrays The order of things in the list is dependent on how the pointers are organized Not on the order of items in memory Inserting an item in an array forces the movement of everything below it If the array is full, it requires reallocation and copying everything Insertion in a linked list only requires moving some pointers around Copyright © 2006-2009 Curt Hill
Curt’s Pointer Rules for Classes Every class that contains a pointer must have: Default constructor Copy constructor Destructor Operator overload of = All of these will be generated by default And generated wrong Make them private if they should not be used Copyright © 2006-2009 Curt Hill
Why? The default constructor is needed to guarantee that the pointer is properly allocated The copy constructor and assignment operator are used to prevent shallow copies The destructor is needed to guarantee that no memory is lost The whole data structure is deleted Copyright © 2006-2009 Curt Hill
Deep and Shallow Copy The problem with automatically generated copy constructor is that it will invariably do a shallow copy We usually want a deep copy What is the difference? A shallow copy will usually just copy the pointer A deep copy reproduces the entire structure Copyright © 2006-2009 Curt Hill
Shallow list copy anchor1 … anchor1 … anchor2 anchor1 … anchor2 Copyright © 2006-2009 Curt Hill
One More Try The last linked list contained two integers and a pointer in one class Not usually the best practice What is better is three classes: The root of the list One node on the list An iterator Several of these are friends of one another Copyright © 2006-2009 Curt Hill
Three Classes LinkedList LinkedNode LinkedIterator Will be the root of the list It will contain all public methods LinkedNode One node Private but makes others friends LinkedIterator Use to iterate through a list Mutual friends with LinkedList Needs to stop if list changes Copyright © 2006-2009 Curt Hill
LinkedList Lets consider: class LinkedList{ LinkedNode * root; LinkedList(LinkedList&) public: LinkedList(); bool add(int,char*); bool remove(int); char * find(int); }; Copyright © 2006-2009 Curt Hill
LinkedNode Lets consider: class LinkedNode{ friend class LinkedList; friend class LinkedIterator; int key; char * data; LinkedNode * next; LinkedNode(); }; Copyright © 2006-2009 Curt Hill
LinkedIterator Lets consider: class LinkedIterator{ LinkedNode * current; void changed(); public: LinkedIterator(); bool start(LinkedList*); bool next(int &, char *); bool more(); }; Copyright © 2006-2009 Curt Hill