Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dynamic Data Structures

Similar presentations


Presentation on theme: "Dynamic Data Structures"— Presentation transcript:

1 Dynamic Data Structures
Putting together variable size pieces Copyright © Curt Hill

2 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 © Curt Hill

3 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 © Curt Hill

4 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 © Curt Hill

5 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 © Curt Hill

6 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 © Curt Hill

7 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 © Curt Hill

8 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 © Curt Hill

9 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 © Curt Hill

10 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 © Curt Hill

11 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 © Curt Hill

12 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 © Curt Hill

13 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 © Curt Hill

14 Shallow list copy anchor1 … anchor1 … anchor2 anchor1 … anchor2
Copyright © Curt Hill

15 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 © Curt Hill

16 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 © Curt Hill

17 LinkedList Lets consider: class LinkedList{ LinkedNode * root; LinkedList(LinkedList&) public: LinkedList(); bool add(int,char*); bool remove(int); char * find(int); }; Copyright © Curt Hill

18 LinkedNode Lets consider: class LinkedNode{ friend class LinkedList; friend class LinkedIterator; int key; char * data; LinkedNode * next; LinkedNode(); }; Copyright © Curt Hill

19 LinkedIterator Lets consider: class LinkedIterator{ LinkedNode * current; void changed(); public: LinkedIterator(); bool start(LinkedList*); bool next(int &, char *); bool more(); }; Copyright © Curt Hill


Download ppt "Dynamic Data Structures"

Similar presentations


Ads by Google