+ All Categories
Home > Documents > CS 261 Winter 2010 Dynamic Array Introduction (aka Vector, ArrayList)

CS 261 Winter 2010 Dynamic Array Introduction (aka Vector, ArrayList)

Date post: 21-Dec-2015
Category:
View: 221 times
Download: 1 times
Share this document with a friend
25
CS 261 Winter 2010 Dynamic Array Introduction (aka Vector, ArrayList)
Transcript
Page 1: CS 261 Winter 2010 Dynamic Array Introduction (aka Vector, ArrayList)

CS 261 Winter 2010

Dynamic Array Introduction

(aka Vector, ArrayList)

Page 2: CS 261 Winter 2010 Dynamic Array Introduction (aka Vector, ArrayList)

Arrays, Pro and Con

• Simple Arrays have nice feature that they are randomly accessible - can quickly get to any element

• Dark side - size must be fixed when created.

• Often you don’t know much much space you need until you are done

Page 3: CS 261 Winter 2010 Dynamic Array Introduction (aka Vector, ArrayList)

Dynamic Array (Vector, ArrayList)

• Dynamic Array (Java Vector, ArrayList, same thing, different API) get around this by encapsulating a partially filled array.

• Hide memory management details behind a simple API

• Is still randomly accessible, but now it grows as necessary

Page 4: CS 261 Winter 2010 Dynamic Array Introduction (aka Vector, ArrayList)

Partially Filled Array

Page 5: CS 261 Winter 2010 Dynamic Array Introduction (aka Vector, ArrayList)

Size vs Capacity

• The size is the “logical” size - The number of elements. What the programmer thinks. Managed by an internal data value.

• The capacity is the size of the physical array. Number of elements it can hold.

Page 6: CS 261 Winter 2010 Dynamic Array Introduction (aka Vector, ArrayList)

Adding an element

• Adding an element to end is sometimes easy. Just increase the (logical) size, and put new value at end.

• But what happens when the size reaches the capacity?

• Must reallocate new data array - but this detail is hidden from user.

Page 7: CS 261 Winter 2010 Dynamic Array Introduction (aka Vector, ArrayList)

Reallocation and copy

Page 8: CS 261 Winter 2010 Dynamic Array Introduction (aka Vector, ArrayList)

Adding to Middle

• Adding an element to middle can also force reallocation (if the current size is equal to capacity)

• But will ALWAYS require elements to moved up to make space

• Is therefore O(n) worst case

Page 9: CS 261 Winter 2010 Dynamic Array Introduction (aka Vector, ArrayList)

Picture of Adding to Middle

Must use a loop to make space for new value. Careful! Loop from top down

Page 10: CS 261 Winter 2010 Dynamic Array Introduction (aka Vector, ArrayList)

Removing an Element

• Removing an Element will also require “sliding over” to delete the value

• Therefore is O(n) worst case

Page 11: CS 261 Winter 2010 Dynamic Array Introduction (aka Vector, ArrayList)

Picture of Remove Element

Remove also requires loop. This time should it be from top or bottom?

Page 12: CS 261 Winter 2010 Dynamic Array Introduction (aka Vector, ArrayList)

Element Types

• How to make a general purpose container class?

• We define element type as symbolic preprocessor constant. Default double.

• Requires recompiling source for new element types. Not elegant, but workable.

Page 13: CS 261 Winter 2010 Dynamic Array Introduction (aka Vector, ArrayList)

Interface file#ifndef DyArray_H#define DyArray_H

# ifndef EleType# define EleType double# endif

# ifndef LT# define LT(a, b) (a < b)# endif

# ifndef EQ# define EQ(a, b) (a == b)# endif

Page 14: CS 261 Winter 2010 Dynamic Array Introduction (aka Vector, ArrayList)

Interface, continuedstruct dyArray { EleType * data; int size; int capacity;};

/* prototypes */void dyArrayInit (struct dyArray *v, int initCap);void dyArrayFree (struct dyArray *v);void dyArrayAdd (struct dyArray *v, EleType d);EleType dyArrayGet (struct dyArray *v, int index);EleType dyarraySet (struct dyArray *v, int index, EleType

newValue);int dyArraySize (struct dyArray *v);

Page 15: CS 261 Winter 2010 Dynamic Array Introduction (aka Vector, ArrayList)

dyArrayInit - initialization

void dyArrayInit (struct dyArray * v, int initCap){ assert (initCap >= 0); v->capacity = initCap; v->size = 0; v->data = (double *) malloc(v->capacity * sizeof(EleType)); assert (v->data != 0);}

Page 16: CS 261 Winter 2010 Dynamic Array Introduction (aka Vector, ArrayList)

dyArrayFree - clean up

Void dyArrayFree (struct dyArray * v)

{

free (v->data);

v->capacity = 0;

v->size = 0;

}

Page 17: CS 261 Winter 2010 Dynamic Array Introduction (aka Vector, ArrayList)

Size

int dyArraySize (struct dyArray * da)

{ return da->size; }

Page 18: CS 261 Winter 2010 Dynamic Array Introduction (aka Vector, ArrayList)

Add a new Element

void dyArrayAdd (struct dyArray * da, EleType newValue)

{ if (da->size >= da->capacity) _dyArrayDoubleCapacity(da);

da->data[da->size] = newValue; da->size += 1; }

Page 19: CS 261 Winter 2010 Dynamic Array Introduction (aka Vector, ArrayList)

Double the Capacity

void _dyArrayDoubleCapacity (struct dyArray * da) {

EleType * oldbuffer = da->data; int oldsize = da->size; int i; dyArrayInit (da, 2 * da->capacity); for (i = 0; i < oldsize; i++) da->data[i] = oldbuffer[i]; da->size = oldsize; free(oldbuffer);}

Page 20: CS 261 Winter 2010 Dynamic Array Introduction (aka Vector, ArrayList)

Lets build something

• How about building a Stack (worksheet 16)? What do you need to do to:

void dyArrayPush(struct dyArray *d, EleType n)EleType dyArrayTop(struct dyArray *d)void dyArrayPop (struct dyArray *d)int dyArrayIsEmpty (struct dyArray *d)

Page 21: CS 261 Winter 2010 Dynamic Array Introduction (aka Vector, ArrayList)

What about a BAG?

• Already have add. How to do

int dyArrayContains (struct dyArray *d, EleType e)

• What about remove?

Page 22: CS 261 Winter 2010 Dynamic Array Introduction (aka Vector, ArrayList)

What about remove?

• Make more useful tool by writing two routines

Void dyArrayRemove (struct dyArray *d, EleType e)

Void dyArrayRemoveAt (struct dyArray *d, int index)

Think about breaking tasks down into useful pieces

Page 23: CS 261 Winter 2010 Dynamic Array Introduction (aka Vector, ArrayList)

Remove At requires extra work

• RemoveAt requires you to “slide” the elements down. Think: from the top, or from the bottom?

Page 24: CS 261 Winter 2010 Dynamic Array Introduction (aka Vector, ArrayList)

What about addAt ?

• We don’t need dyArrayAddAt for the bag (order doesn’t matter), but it is a nice complement to removeAt

• And we will us it later when we talk about the advantages of keeping a collection in order

Page 25: CS 261 Winter 2010 Dynamic Array Introduction (aka Vector, ArrayList)

Your chance

• Worksheet time. Finish implementation of simple get and set, including reallocation. Next do the stack. Then move on to remove to complete the Bag.

• Note that your implementation will be used in programming assignment 2.

• I’m not going to collect, but you will need this for the programming assignment.


Recommended