Dynamic allocation (continued)

Slides:



Advertisements
Similar presentations
DATA STRUCTURES USING C++ Chapter 5
Advertisements

Lists: An internal look
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.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
Summary of lectures (1 to 11)
5 Linked Structures. 2 Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
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.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Stack and Heap Memory Stack resident variables include:
Chapter 5 – Dynamic Data Structure Par1: Abstract Data Type DATA STRUCTURES & ALGORITHMS Teacher: Nguyen Do Thai Nguyen
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
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.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
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.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.
Introduction toData structures and Algorithms
C++ Programming:. Program Design Including
ECE Application Programming
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Copy Constructor / Destructors Stacks and Queues
Lectures linked lists Chapter 6 of textbook
Linked Lists Chapter 6 Section 6.4 – 6.6
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Data Structures and Algorithms
Dr. Bernard Chen Ph.D. University of Central Arkansas
CISC181 Introduction to Computer Science Dr
Stack and Queue APURBO DATTA.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
CMSC 341 Lecture 5 Stacks, Queues
Array Lists Chapter 6 Section 6.1 to 6.3
Chapter 16-2 Linked Structures
An Introduction to Pointers
C++ Plus Data Structures
Linked List (Part I) Data structure.
Indirection.
Data Structures and Algorithms
Chapter 16 Linked Structures
CSI 1340 Introduction to Computer Science II
Stacks and Queues.
Introduction to Data Structure
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2019 Lecture 29: Linked queues
Data Structures and Algorithms Memory allocation and Dynamic Array
(1 - 2) Introduction to C Data Structures & Abstract Data Types
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Getting queues right … finally (?)
Instructor: Dr. Michael Geiger Spring 2019 Lecture 34: Exam 3 Preview
Presentation transcript:

Dynamic allocation (continued) EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019 Lecture 19: Dynamic allocation (continued) Stacks

Announcements/reminders Program 2 due 3/21 Exam 2: Monday, 4/1, 3-5 PM, Ball 214 Use same poll as before to request alt. exam (I’ll repost link) Today’s lecture Review: dynamic allocation Linked data structures Stacks 4/28/2019 Data Structures: Lecture 19

Review: Dynamic allocation with new Use new to dynamically allocate space new int allocates space for 1 integer new int[20] allocates an array of 20 integers new “returns” pointer to first byte Directly initialize with value in parentheses e.g. new int(3) Pointer must hold address generated by new int *p1 = new int(3); // p1  single int = 3 int *p2 = new int[20]; // p2  array of 20 // ints 4/28/2019 Data Structures: Lecture 19

Review: Dynamically allocated objects May want to dynamically allocate objects Ex. array of Points where size is unknown at compile time Point *pointArray; int numPoints; cin >> numPoints; pointArray = new Point[numPoints]; Ex. linked list data structure—to add an element to the list, must allocate new element class linkedList { private: int elem; linkedList *next; public: linkedList(); linkedList(int val); void addElement(int i); ... } void linkedList::addElement(int i) { next = new linkedList(i); 4/28/2019 Data Structures: Lecture 19

Review: Referencing objects thru pointers Recall: use dot operator (.) to reference members of object, e.g: Point p1; p1.setX(2); With pointers, use -> linkedList *list1 = new linkedList; list1->addElement(2); 4/28/2019 Data Structures: Lecture 19

Deallocation with delete Space allocated using new should be freed In C, we used free() In C++, we use delete You should only use delete to free memory allocated by new Any other use will result in an error 4/28/2019 Data Structures: Lecture 19

Data Structures: Lecture 19 delete example int *ptr; ptr = new int (100); ptr 100 delete ptr; //free the memory ptr ? delete frees space on heap ... ... but ptr still points to same address! Solution: assign freed pointers to NULL: ptr = NULL; 4/28/2019 Data Structures: Lecture 19

Example: delete with arrays double *dptr; const int SIZE = 10; dptr = new double[SIZE]; //80 bytes for(int i=0; i<SIZE; ++i) cin >> dptr[i]; fun1(dptr, SIZE); // pass array to fun1 delete [] dptr; //free all 10 elements dptr = NULL; 4/28/2019 Data Structures: Lecture 19

Data Structures: Lecture 19 List ADT Common problem in program: store collection—or list—of things Grocery list, grade list, list of students, etc. Common properties Homogeneous (elements all have same type) Finite length (number of elements) Length could be 1 or even 0 Elements arranged sequentially Well-defined first and last element All except last have unique successor All except first have unique predecessor 4/28/2019 Data Structures: Lecture 19

Data Structures: Lecture 19 List ADT (continued) A sequence of a finite number of data items, all of the same type Basic operations Construction: create empty list Empty: check if the list is empty Insert: add an item to the list Delete: remove an item from the list Traverse: go through part or all of list, accessing and processing elements in order Types of traversal include search, output (to screen or file), copy, rearrange (usually sort) 4/28/2019 Data Structures: Lecture 19

List-based data structures List ADT used for several data structures Differences between them Where can data be inserted? Where can data be removed? What determines ordering of items? List-based data structures Stacks: insert/remove at “top” (LIFO) Queue: insert at back, remove at front (FIFO) Linked list: insert/remove anywhere, can order as needed 4/28/2019 Data Structures: Lecture 19

Data Structures: Lecture 19 Array-based lists Implementing ADT Define necessary data members Define methods described in ADT design Common list implementation: array Built-in type in most languages Sequential memory storage Straightforward algorithms For (some) flexibility, dynamically allocate array Define max size of list at construction Dynamic allocation does cause some issues with construction (to be seen) 4/28/2019 Data Structures: Lecture 19

Data Structures: Lecture 19 Stacks Consider the following applications Run-time memory management On function call, create space for local data (stack frame or activation record) On return, clear frame for current function Traversing maze Follow path until dead end Backtrack to last intersection and choose new path In all cases Need ability to access only most recent data If necessary, will only remove most recent data Appropriate data structure: stack 4/28/2019 Data Structures: Lecture 19

Data Structures: Lecture 19 Stack ADT Stack is last-in, first-out (LIFO) data structure Definition Ordered collection of data items Can only be accessed at one end (top of stack) Operations Construction (start with empty stack) Check if stack is empty Push: add data to the top of the stack Pop: remove data from the top of the stack Read item at top of stack 4/28/2019 Data Structures: Lecture 19

Data Structures: Lecture 19 Final notes Next time: more on stacks Details of array-based implementation Dynamic allocation, constructors, and destructors Operator overloading Reminders: Program 2 due 3/21 Exam 2: Monday, 4/1, 3-5 PM, Ball 214 Use same poll as before to request alt. exam (I’ll repost link) 4/28/2019 Data Structures: Lecture 19