Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5 – Dynamic Data Structure Par1: Abstract Data Type DATA STRUCTURES & ALGORITHMS Teacher: Nguyen Do Thai Nguyen

Similar presentations


Presentation on theme: "Chapter 5 – Dynamic Data Structure Par1: Abstract Data Type DATA STRUCTURES & ALGORITHMS Teacher: Nguyen Do Thai Nguyen"— Presentation transcript:

1 Chapter 5 – Dynamic Data Structure Par1: Abstract Data Type DATA STRUCTURES & ALGORITHMS Teacher: Nguyen Do Thai Nguyen E-mail: nguyenndt@hcmup.edu.vnnguyenndt@hcmup.edu.vn Phone: 0906667597 Group: https://www.facebook.com/groups/335118686690998 Ho Chi Minh City University of Pedagogy

2 1 2 3 Abstract Data Type (ADT) Overview object oriented programming Pointer and Dynamic memory allocation Outline

3 1. Abstract Data Type It’s a data type It can hold both data and operation An ADT is a collection of data and associated operations for manipulating that data ADT support abstraction, encapsulation, and information hiding

4 1. Abstract Data Type

5 1. Abatract Data Type ADT  C++: class Examples –the set ADT A set of elements Operations: union, intersection, size and complement –the queue ADT A set of sequences of elements Operations: create empty queue, insert, examine, delete, and destroy queue –Stack ADT –List ADT

6 The List ADT Concept: A sequence of zero or more elements A 1, A 2, A 3, … A N Member data: –Size (N): length of the list –List Contents: A 1 : first element A N : last element

7 The List ADT Operations: –printList: print the list –makeEmpty: create an empty list –find: locate the position of an object in a list list: 34,12, 52, 16, 12 find(52)  3 Two standard implementations for the list ADT –Array-based –Linked list

8 The List ADT Operations: insert: insert an object to a list –insert(x,3)  34, 12, 52, x, 16, 12 remove: delete an element from the list –remove(52)  34, 12, x, 16, 12 findKth: retrieve the element at a certain position

9 Array Elements are stored in contiguous array positions

10 Linked List Ensure that the list is not stored contiguously –use a linked list –a series of structures that are not necessarily adjacent

11 2. Overview object oriented programming Class is a template that include data and functions operating on these data (attribute and method – data member and function member) –A class represents a set of objects that have common properties Object is an instance of a class, an entity created using a class definition.

12 Example object class

13 2. Overview object oriented programming Object-Oriented Design Principles –Abstraction –Encapsulation –Modularity.

14 2. Overview object oriented programming Class declaration in c++: ClassName class ClassName { public: memberFunction1(); memberFunction2(); ………….. private: DataType1 memberdata1; DataType2 memberdata2; ……………. }; private protected public

15 3. Pointer and Dynamic memory allocation

16 Memory Management Static Memory Allocation –Memory is allocated at compilation time Dynamic Memory –Memory is allocated at running time

17 Static vs. Dynamic Objects Static object (variables as declared in function calls) –Memory is acquired automatically –Memory is returned automatically when object goes out of scope Dynamic object –Memory is acquired by program with an allocation request new operation –Dynamic objects can exist beyond the function in which they were allocated –Object memory is returned by a deallocation request delete operation

18 Memory Allocation { int a[200]; … } int* ptr; ptr = new int[200]; … delete [] ptr; new delete

19 Object (variable) creation: New Syntax ptr = new SomeType; where ptr is a pointer of type SomeType p Uninitialized int variable Example int* p = new int;

20 Object (variable) destruction: Delete Syntax delete p; storage pointed to by p is returned to free store and p is now undefined p Example int* p = new int; *p = 10; delete p; 10

21 Array of New: dynamic arrays Syntax P = new SomeType[Expression]; –Where P is a pointer of type SomeType Expression is the number of objects to be constructed -- we are making an array Because of the flexible pointer syntax, P can be considered to be an array

22 Example Dynamic Memory Allocation n Request for “ unnamed ” memory from the Operating System n int *p, n=10; p = new int; p = new int[100]; p new p p = new int[n]; p new

23 Memory Allocation Example Want an array of unknown size main() { cout << “How many students? “; cin >> n; int *grades = new int[n]; for(int i=0; i < n; i++){ int mark; cout << “Input Grade for Student” << (i+1) << “ ? :”; cin >> mark; grades[i] = mark; }... printMean( grades, n ); // call a function with dynamic array... }

24 Freeing (or deleting) Memory

25 –Declaration: * namePtr; –Access: namePtr->memberData; namePtr->memberFunction(); or (*namePtr).memberData; (*namePtr).memberFunction() Object pointer

26


Download ppt "Chapter 5 – Dynamic Data Structure Par1: Abstract Data Type DATA STRUCTURES & ALGORITHMS Teacher: Nguyen Do Thai Nguyen"

Similar presentations


Ads by Google