CS 261: Data Structures Dynamic...

26
CS 261: Data Structures Dynamic Arrays Introduction

Transcript of CS 261: Data Structures Dynamic...

Page 1: CS 261: Data Structures Dynamic Arraysweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/dynA… · CS 261: Data Structures Dynamic Arrays Introduction. Arrays --Pros and

CS 261: Data Structures

Dynamic Arrays

Introduction

Page 2: CS 261: Data Structures Dynamic Arraysweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/dynA… · CS 261: Data Structures Dynamic Arrays Introduction. Arrays --Pros and

Arrays -- Pros and Cons

• Positives:– Simple – Each element accessible in O(1)

• Negatives:– Size must be fixed when created– What happens when the program later requires

more space?2

# define MAX_SIZE 100struct Bag {

TYPE data[MAX_SIZE];int size;

};

Page 3: CS 261: Data Structures Dynamic Arraysweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/dynA… · CS 261: Data Structures Dynamic Arrays Introduction. Arrays --Pros and

Dynamic Arrays

• Our goal: Hide memory management details

behind an Application Program Interface (API)

• Each element is still accessible in O(1)

• But a dynamic array can change capacity

3

Page 4: CS 261: Data Structures Dynamic Arraysweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/dynA… · CS 261: Data Structures Dynamic Arrays Introduction. Arrays --Pros and

Dynamic Array

Capacity( = cap)

Size( = size)

10size =data =

16cap =

4

“Unused” elements

Page 5: CS 261: Data Structures Dynamic Arraysweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/dynA… · CS 261: Data Structures Dynamic Arrays Introduction. Arrays --Pros and

Size and Capacity• Size:

– Current number of elements– Managed by an internal data value

• Capacity: – Number of elements that a dynamic array

can hold before it must resize

5

Page 6: CS 261: Data Structures Dynamic Arraysweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/dynA… · CS 261: Data Structures Dynamic Arrays Introduction. Arrays --Pros and

Adding an Element

• Increment the size

• Put the new value at the end of the dynamic array

6

Page 7: CS 261: Data Structures Dynamic Arraysweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/dynA… · CS 261: Data Structures Dynamic Arrays Introduction. Arrays --Pros and

Adding an Element• What happens when size == capacity?

• Must:– reallocate new space– copy all data values to the new space– hide these details from the user

7

Page 8: CS 261: Data Structures Dynamic Arraysweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/dynA… · CS 261: Data Structures Dynamic Arrays Introduction. Arrays --Pros and

Reallocate and Copy

8size =data =

8cap =8size =

data =

16cap =

Before reallocation: After reallocation:

Must allocate new (larger) array and copy valid data elements

8

Page 9: CS 261: Data Structures Dynamic Arraysweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/dynA… · CS 261: Data Structures Dynamic Arrays Introduction. Arrays --Pros and

Reallocate and Copy

8size =data =

8cap =8size =

data =

16cap =

Before reallocation: After reallocation:

DO NOT forget to free up thememory of the old array

9

old new

Page 10: CS 261: Data Structures Dynamic Arraysweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/dynA… · CS 261: Data Structures Dynamic Arrays Introduction. Arrays --Pros and

Inserting an Element in the Middle

• May also require reallocation– When?

• Requires that some elements be moved up to make space for the new one

10

Page 11: CS 261: Data Structures Dynamic Arraysweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/dynA… · CS 261: Data Structures Dynamic Arrays Introduction. Arrays --Pros and

Inserting an ElementLoop from THE END backward while copying

Before After

Add at idxà idxà

11

Page 12: CS 261: Data Structures Dynamic Arraysweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/dynA… · CS 261: Data Structures Dynamic Arrays Introduction. Arrays --Pros and

Inserting an Element -- Complexity

O(n) in the worst case

12

Page 13: CS 261: Data Structures Dynamic Arraysweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/dynA… · CS 261: Data Structures Dynamic Arrays Introduction. Arrays --Pros and

• Remove also requires looping. • Loop from idx forward while copying

Before After

Remove idxà

Removing an Element

13

Page 14: CS 261: Data Structures Dynamic Arraysweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/dynA… · CS 261: Data Structures Dynamic Arrays Introduction. Arrays --Pros and

Removing an Element -- Complexity

O(n) worst case

14

Page 15: CS 261: Data Structures Dynamic Arraysweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/dynA… · CS 261: Data Structures Dynamic Arrays Introduction. Arrays --Pros and

Interface View of

Dynamic Arrays

15

Page 16: CS 261: Data Structures Dynamic Arraysweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/dynA… · CS 261: Data Structures Dynamic Arrays Introduction. Arrays --Pros and

Interface file: dynArr.h

struct dyArr {

TYPE * data; /* Pointer to data array */

int size; /* Number of elements */

int capacity; /* Capacity of array */

};

/* Rest of dynarr.h on next slide */ 16

Page 17: CS 261: Data Structures Dynamic Arraysweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/dynA… · CS 261: Data Structures Dynamic Arrays Introduction. Arrays --Pros and

Interface (continued)/* function prototypes */

void initDynArr (struct dyArr *da, int cap);

void freeDynArr (struct dyArr *da);

void addDynArr (struct dyArr *da, TYPE val);

TYPE getDynArr (struct dyArr *da, int idx);

void putDynArr (struct dyArr *da, int idx, TYPE val);

int sizeDynArr (struct dyArr *da);

void _dyArrDoubleCapacity (struct dyArray * da); 17

Page 18: CS 261: Data Structures Dynamic Arraysweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/dynA… · CS 261: Data Structures Dynamic Arrays Introduction. Arrays --Pros and

Implementation View of

Dynamic Arrays

18

Page 19: CS 261: Data Structures Dynamic Arraysweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/dynA… · CS 261: Data Structures Dynamic Arrays Introduction. Arrays --Pros and

initDynArr -- Initialization

void initDynArr (struct dyArr *da, int cap){

assert (cap >= 0);

da->capacity = cap;

da->size = 0;

da->data = (TYPE *)

malloc(da->capacity * sizeof(TYPE));

assert (da->data != 0); /* check the status */

}

19

/* Allocate memory to data array */

Page 20: CS 261: Data Structures Dynamic Arraysweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/dynA… · CS 261: Data Structures Dynamic Arrays Introduction. Arrays --Pros and

freeDynArr -- Clean-up

void freeDynArr (struct dyArr * da)

{

assert (da != 0);

free (da->data); /*free entire array*/

da->capacity = 0;

da->size = 0;

}

20

Page 21: CS 261: Data Structures Dynamic Arraysweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/dynA… · CS 261: Data Structures Dynamic Arrays Introduction. Arrays --Pros and

Size

int sizeDynArr (struct dyArr * da){

return da->size;

}

21

Page 22: CS 261: Data Structures Dynamic Arraysweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/dynA… · CS 261: Data Structures Dynamic Arrays Introduction. Arrays --Pros and

Get the Value at a Given Position

TYPE getDynArr (struct dyArr *da, int idx);

{ /*always make sure the input is meaningful*/

assert((sizeDynArr(da) > idx) && (idx >= 0));

return da->data[idx];

}

22

why?

Page 23: CS 261: Data Structures Dynamic Arraysweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/dynA… · CS 261: Data Structures Dynamic Arrays Introduction. Arrays --Pros and

Add a New Element

void addDynArr (struct dyArr * da, TYPE val){

/*make sure there is enough capacity*/

if (da->size >= da->capacity)

_dyArrDoubleCapacity(da);

da->data[da->size] = val;

da->size++; /*must increase the size*/

}

23

Page 24: CS 261: Data Structures Dynamic Arraysweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/dynA… · CS 261: Data Structures Dynamic Arrays Introduction. Arrays --Pros and

Double the Capacity

8size =data =

8cap =8size =

data =

16cap =

Before reallocation: After reallocation:

1. allocate new space 2. copy data to new space3. free old space 24

MUST:

old new

Page 25: CS 261: Data Structures Dynamic Arraysweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/dynA… · CS 261: Data Structures Dynamic Arrays Introduction. Arrays --Pros and

Double the Capacityvoid _dyArrDoubleCapacity (struct dyArray * da) {

TYPE * oldbuffer = da->data; /*memorize old*/

int oldsize = da->size;

/*allocate new memory*/

initDynArr (da, 2 * da->capacity);

for (int i = 0; i < oldsize; i++) /*copy old*/

da->data[i] = oldbuffer[i];

da->size = oldsize;

free(oldbuffer); /*free old memory*/

}25

Page 26: CS 261: Data Structures Dynamic Arraysweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/dynA… · CS 261: Data Structures Dynamic Arrays Introduction. Arrays --Pros and

Next Class

How to implement

– Stack

– Bag

by using Dynamic Array26