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

Slides:



Advertisements
Similar presentations
Lists CS 3358.
Advertisements

COMP171 Fall 2005 Lists.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques.
Informática II Prof. Dr. Gustavo Patiño MJ
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Chapter 4 ADT Sorted List.
Dynamic Objects. COMP104 Dynamic Objects / Slide 2 Memory Management * Static Memory Allocation n Memory is allocated at compiling time * Dynamic Memory.
Dynamic Objects. COMP104 Lecture 31 / Slide 2 Static verses Dynamic Objects * Static object n Memory is acquired automatically  int A[10]; n Memory is.
Dynamic Objects. COMP104 Dynamic Objects / Slide 2 Memory Management * Static Memory Allocation n Memory is allocated at compilation time * Dynamic Memory.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Pointers and Dynamic Objects Mechanisms for developing flexible list representations.
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Basic Definitions Data Structures: Data Structures: A data structure is a systematic way of organizing and accessing data. Or, It’s the logical relationship.
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
Pointers. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program execution.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Pointer Data Type and Pointer Variables
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Pointers and Dynamic Objects Mechanisms for developing flexible list representations JPC and JWD © 2002 McGraw-Hill, Inc.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Lists Chapter 6 5/14/15 Adapted from instructor slides Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
The List ADT A sequence of zero or more elements A 1, A 2, A 3, … A N-1 N: length of the list A 1 : first element A N-1 : last element A i : position i.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
UNIT-V ABSTRACT DATA TYPE 1.LIST 2.STACK 3.QUEUE EC6301-II-ECE-C.
Unit – I Lists.
CSCI-255 LinkedList.
Lists CS 3358.
Dynamic Memory Allocation
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Array Lists Chapter 6 Section 6.1 to 6.3
Pointers and dynamic objects
Pointers.
Introduction to Data Structure
Dynamic Objects.
Dynamic allocation (continued)
Data Structures and Algorithms Memory allocation and Dynamic Array
Pointers and dynamic objects
Dynamic Objects.
CECS 130 Final Exam Review Session
Presentation transcript:

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