CS 261 Winter 2011
-
Upload
valterra-watson -
Category
Documents
-
view
25 -
download
3
description
Transcript of CS 261 Winter 2011
![Page 1: CS 261 Winter 2011](https://reader035.fdocuments.net/reader035/viewer/2022062422/56813336550346895d9a2fcf/html5/thumbnails/1.jpg)
CS 261 Winter 2011
Dynamic Array Worksheet Review
(aka Vector, ArrayList)
![Page 2: CS 261 Winter 2011](https://reader035.fdocuments.net/reader035/viewer/2022062422/56813336550346895d9a2fcf/html5/thumbnails/2.jpg)
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 2011](https://reader035.fdocuments.net/reader035/viewer/2022062422/56813336550346895d9a2fcf/html5/thumbnails/3.jpg)
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 2011](https://reader035.fdocuments.net/reader035/viewer/2022062422/56813336550346895d9a2fcf/html5/thumbnails/4.jpg)
Partially Filled Array
![Page 5: CS 261 Winter 2011](https://reader035.fdocuments.net/reader035/viewer/2022062422/56813336550346895d9a2fcf/html5/thumbnails/5.jpg)
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 2011](https://reader035.fdocuments.net/reader035/viewer/2022062422/56813336550346895d9a2fcf/html5/thumbnails/6.jpg)
Interfacestruct 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 7: CS 261 Winter 2011](https://reader035.fdocuments.net/reader035/viewer/2022062422/56813336550346895d9a2fcf/html5/thumbnails/7.jpg)
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 8: CS 261 Winter 2011](https://reader035.fdocuments.net/reader035/viewer/2022062422/56813336550346895d9a2fcf/html5/thumbnails/8.jpg)
dyArrayFree - clean up
Void dyArrayFree (struct dyArray * v)
{
free (v->data);
v->capacity = 0;
v->size = 0;
}
![Page 9: CS 261 Winter 2011](https://reader035.fdocuments.net/reader035/viewer/2022062422/56813336550346895d9a2fcf/html5/thumbnails/9.jpg)
Size
int dyArraySize (struct dyArray * da)
{
return da->size;
}
![Page 10: CS 261 Winter 2011](https://reader035.fdocuments.net/reader035/viewer/2022062422/56813336550346895d9a2fcf/html5/thumbnails/10.jpg)
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 11: CS 261 Winter 2011](https://reader035.fdocuments.net/reader035/viewer/2022062422/56813336550346895d9a2fcf/html5/thumbnails/11.jpg)
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 12: CS 261 Winter 2011](https://reader035.fdocuments.net/reader035/viewer/2022062422/56813336550346895d9a2fcf/html5/thumbnails/12.jpg)
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 13: CS 261 Winter 2011](https://reader035.fdocuments.net/reader035/viewer/2022062422/56813336550346895d9a2fcf/html5/thumbnails/13.jpg)
Push
void dyArrayPush(struct dyArray *d, EleType n)
{
/* why reinvent the wheel? */
dyArrayAdd(d, n);
}
![Page 14: CS 261 Winter 2011](https://reader035.fdocuments.net/reader035/viewer/2022062422/56813336550346895d9a2fcf/html5/thumbnails/14.jpg)
Top
EleType dyArrayTop(struct dyArray *d)
{
/* make sure there are enough places */
assert(dyArraySize(d) > 0);
return dyArrayGet(d, 0);
}
![Page 15: CS 261 Winter 2011](https://reader035.fdocuments.net/reader035/viewer/2022062422/56813336550346895d9a2fcf/html5/thumbnails/15.jpg)
Pop
void dyArrayPop(struct dyArray *d)
{
/* make sure there are enough places */
assert(dyArraySize(d) > 0);
d->size--; /* just reduce size by one*/
}
![Page 16: CS 261 Winter 2011](https://reader035.fdocuments.net/reader035/viewer/2022062422/56813336550346895d9a2fcf/html5/thumbnails/16.jpg)
isEmpty
int dyArrayIsEmpty (struct dyArray * d)
{
return dyArraySize(d) == 0;
}
![Page 17: CS 261 Winter 2011](https://reader035.fdocuments.net/reader035/viewer/2022062422/56813336550346895d9a2fcf/html5/thumbnails/17.jpg)
What about a BAG?
• Already have add and size. How to do
int dyArrayContains (struct dyArray *d, EleType e)
• Just like previous version
• What about remove?
![Page 18: CS 261 Winter 2011](https://reader035.fdocuments.net/reader035/viewer/2022062422/56813336550346895d9a2fcf/html5/thumbnails/18.jpg)
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 19: CS 261 Winter 2011](https://reader035.fdocuments.net/reader035/viewer/2022062422/56813336550346895d9a2fcf/html5/thumbnails/19.jpg)
dyArrayRemove
void dyArrayRemove (struct dyArray *d, EleType e)
{
int i;
for (i = 0; i < d->size; i++)
if (EQ(e, d->data[i])) {
dyArrayRemoveAt(d, i);
return;
}
}
![Page 20: CS 261 Winter 2011](https://reader035.fdocuments.net/reader035/viewer/2022062422/56813336550346895d9a2fcf/html5/thumbnails/20.jpg)
Remove At requires extra work
• RemoveAt requires you to “slide” the elements down. Think: from the top, or from the bottom?
![Page 21: CS 261 Winter 2011](https://reader035.fdocuments.net/reader035/viewer/2022062422/56813336550346895d9a2fcf/html5/thumbnails/21.jpg)
dyArrayRemoveAt
void dyArrayRemoveAt(struct dyArray *d, int index)
{
int i;
assert(index >= 0 && index < d->size);
d->size--;
for (i = index; i < d->size; i++)
dyArrayData[i] = dyArrayData[i+1];
}
![Page 22: CS 261 Winter 2011](https://reader035.fdocuments.net/reader035/viewer/2022062422/56813336550346895d9a2fcf/html5/thumbnails/22.jpg)
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