Chapter 5 – Dynamic Data Structure Par1: Abstract Data Type DATA STRUCTURES & ALGORITHMS Teacher: Nguyen Do Thai Nguyen Phone: Group: Ho Chi Minh City University of Pedagogy
1 2 3 Abstract Data Type (ADT) Overview object oriented programming Pointer and Dynamic memory allocation Outline
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
1. Abstract Data Type
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
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
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
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
Array Elements are stored in contiguous array positions
Linked List Ensure that the list is not stored contiguously –use a linked list –a series of structures that are not necessarily adjacent
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.
Example object class
2. Overview object oriented programming Object-Oriented Design Principles –Abstraction –Encapsulation –Modularity.
2. Overview object oriented programming Class declaration in c++: ClassName class ClassName { public: memberFunction1(); memberFunction2(); ………….. private: DataType1 memberdata1; DataType2 memberdata2; ……………. }; private protected public
3. Pointer and Dynamic memory allocation
Memory Management Static Memory Allocation –Memory is allocated at compilation time Dynamic Memory –Memory is allocated at running time
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
Memory Allocation { int a[200]; … } int* ptr; ptr = new int[200]; … delete [] ptr; new delete
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;
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
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
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
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... }
Freeing (or deleting) Memory
–Declaration: * namePtr; –Access: namePtr->memberData; namePtr->memberFunction(); or (*namePtr).memberData; (*namePtr).memberFunction() Object pointer