CS261 Data Structures Dynamic Arrays Part 2: Implementation.

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

Collections Chapter Java Collection Frameworks The Java collection framework is a set of utility classes and interfaces. Designed for working with.
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
CSE 303 Lecture 16 Multi-file (larger) programs
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
CS261 Data Structures Dynamic Arrays. Pro: only core data structure designed to hold a collection of elements Pro: random access: can quickly get to any.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
Classes. What is a class? Data and the functions (methods) that operate on that data – collectively called members –Example: bank account class Provide.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
C For Java Programmers Tom Roeder CS sp. Why C? The language of low-level systems programming  Commonly used (legacy code)  Trades off safety.
Specification and Implementation Separating the specification from implementation makes it easier to modify programs. Changes in the class’s implementation.
Classes. What is a class? Data and the functions (methods) that operate on that data – collectively called members –Example: bank account class Provide.
CS 261 Winter 2010 Dynamic Array Introduction (aka Vector, ArrayList)
CS Winter 2011 Introduction to the C programming language.
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
Rossella Lau Lecture 5, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 5: Class construction  Encapsulation 
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
CS-2303 System Programming Concepts
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Linked Structures, Project 1: Linked List Bryce Boe 2013/07/11 CS24, Summer 2013 C.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.
1 Further C  Multiple source code file projects  Structs  The preprocessor  Pointers.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
By – Tanvir Alam.  This tutorial offers several things.  You’ll see some neat features of the language.  You’ll learn the right things to google. 
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
1 Data Structures CSCI 132, Spring 2014 Lecture 10 Dynamic Memory and Pointers.
Recap Visual Perception and Data Visualization Types of Information Display Examples of Diagrams used for Data Display Planning Requirement for Data Visualization.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
CS261 – Data Structures Iterator ADT Dynamic Array and Linked List.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
CS 261 – Recitation 2 Fall 2013 Oregon State University School of Electrical Engineering and Computer Science.
CS 261 – Data Structures Introduction to C Programming.
CS261 Data Structures Binary Search Trees III Generic Container.
CS212: Object Oriented Analysis and Design Lecture 24: Introduction to STL.
Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type.
1 9/6/05CS360 Windows Programming CS360 Windows Programming.
CS 261 Fall 2009 Dynamic Array Introduction (aka Vector, ArrayList)
Separate Compilation Bryce Boe 2013/10/09 CS24, Fall 2013.
CS261 Data Structures Linked Lists - Introduction.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Dynamic Array Queue and Deque
Week 5 - Monday.  What did we talk about last time?  Processes  Lab 4.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
Classes Classes are a major feature of C++. They support – – Abstraction – Data hiding – Encapsulation – Modularity – Re-use through inheritance.
1 CMPT 117 Linked Lists (singly linked). 2 Problems with arrays  When an element is deleted from or inserted into an array, the rest of the array has.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Introduction to the C programming language
Vectors Holds a set of elements, like an array
Basic C++ What’s a declaration? What’s a definition?
Priority Queues & Heaps
Dynamic Array: Implementation of Stack
Week 9 - Monday CS222.
Presentation transcript:

CS261 Data Structures Dynamic Arrays Part 2: Implementation

Pro: only core data structure designed to hold a collection of elements Pro: random access: can quickly get to any element  O(1) Con: fixed size: – Maximum number of elements must be specified when created Arrays: Pros and Cons

How to make a general purpose container class? We define TYPE as symbolic preprocessor constant Requires recompiling source for new element types – Not elegant, but workable. Element Types - TYPE

#ifndef __DYNARR_H #define __DYNARR_H # define TYPE int # define LT(a, b) ((a) < (b) # define EQ(a, b) ((a) == (b))... /* Rest of dynarr.h (on next slide). */ #endif Interface File: dynarr.h

struct DynArr { TYPE *data; /* Pointer to data array. */ int size; /* Number of elements in collection. */ int cap; /* Capacity of array. */ }; /* Dynamic Array Functions */ void initDynArr(struct DynArr *v, int cap); void freeDynArr(struct DynArr *v); int sizeDynArr(struct DynArr *v); void addDynArr(struct DynArr *v, TYPE e) TYPE getDynArr(struct DynArr *v, int pos); void putDynArr(struct DynArr *v, int pos, TYPE val); Interface (cont.)

void initDynArr(struct DynArr *v, int cap) { v->data = malloc(cap * sizeof(TYPE)); assert(v->data != 0); v->size = 0; v->cap = cap; } Initialization: initDynArr

void freeDynArr(struct DynArr *v) { assert(v != 0); assert(v->data != 0); free(v->data); v->data = 0; v->cap = 0; v->size = 0; } Clean Up: freeDynArr

struct DynArr d; initDynArr(&d, 8); addDynArr(&d, 1);... Concretely, in C…using the dynArray

To use a struct dynArr, the user must declare one in main.c (see previous slide). To declare it, the compiler must know its size when compiling that file (ie. it must be in the header!) If it’s in the header, it is ‘exposed’ to the end user and this can be dangerous and violates ‘encapsulation’ Better Solution: Provide create() and delete() functions for your data structure. New returns a ‘pointer’ to allocated space User can always declare pointers and compiler always knows the size of a pointer! Now you can hide your Struct in the.c file or a library Better Solution

struct DynArr* createDynArr(int cap) { struct DynArr *r; assert(cap > 0); r = malloc(sizeof( struct DynArr)); assert(r != 0); _initDynArr(r,cap); return r; } Create/Delete Allocate space for struct DynArr itself!

struct DynArr *d; d = createDynArr(20); addDynArr(d, 1);... Using ‘encapsulated’ DynArray

When to use Dynamic Arrays Need random access Low memory footprint Don’t know size of array at compile time See Examples in Java and C++ STL – Vector (C++ STL) – Vector and ArrayList (Java) When should/should not use a dynamic array! – When O(N) resize is NEVER acceptable

Dynamic Array Stack/Bag First: Worksheet 14 – Dynamic Array Basics – _setCapacity – Get, Put – Swap, RemoveAt Worksheets 16, 21 (start for next assignment) – Implement the stack/bag interfaces – keep and reuse functionality from WS#14 where appropriate.