Dynamic Data Structures

Slides:



Advertisements
Similar presentations
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Introduction to Programming Lecture 39. Copy Constructor.
Self Referential Structure. A structure may not contain a member of its own type. struct check { int item; struct check n; // Invalid };
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Xiaoyan Li, CSC211 Data Structures Lecture 10 The Bag and Sequence Classes with Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
@ George Wolberg, CSC212 Data Structure Lecture 6 Dynamic Classes and the Law of the Big Three Instructor: George Wolberg Department of Computer.
Memory Management.
Yan Shi CS/SE 2630 Lecture Notes
Pointers and Dynamic Arrays
COMP 53 – Week Eight Linked Lists.
Pointers and Linked Lists
Chapter 12 – Data Structures
Pointers and Linked Lists
5.13 Recursion Recursive functions Functions that call themselves
Copy Constructor / Destructors Stacks and Queues
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Distinguishing logic from data type
UNIT – I Linked Lists.
Linked Lists Chapter 6 Section 6.4 – 6.6
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Programming with ANSI C ++
MIS 215 Module 1 – Unordered Lists
CISC181 Introduction to Computer Science Dr
Chapter 4 Linked Lists.
C++ Object-Oriented Programming
classes and objects review
Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management.
Chapter 4 Linked Lists
Edvin von Otter & Todd Barker
LinkedList Class.
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
The Bag and Sequence Classes with Linked Lists
Programmazione I a.a. 2017/2018.
Dynamically Allocated Memory
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 16-2 Linked Structures
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
A brief look at some of the new features introduced to the language
Object Oriented Programming COP3330 / CGS5409
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Chapter 4 Linked Lists.
understanding memory usage by a c++ program
Foundational Data Structures
Pointers and Linked Lists
Dynamic Memory Allocation
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Doubly Linked List Implementation
Review & Lab assignments
Indirection.
A Robust Data Structure
Chapter 4 Linked Lists.
Chapter 17: Linked Lists.
Chapter 16 Linked Structures
Introduction to C++ Linear Linked Lists
Copyright © – Curt Hill STL List Details Copyright © – Curt Hill.
Destructor CSCE 121 J. Michael Moore.
Class and Objects In a class, all the functions that operate on the data structure are grouped together in one place along with the data Like a struct.
Destructor CSCE 121.
Doubly Linked List Implementation
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Rule of Three Part 1 & 2.
Classes and Objects Object Creation
A dictionary lookup mechanism
Data Structures & Programming
Presentation transcript:

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