Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dynamic Array: Implementation of Stack

Similar presentations


Presentation on theme: "Dynamic Array: Implementation of Stack"— Presentation transcript:

1 Dynamic Array: Implementation of Stack
CS 261 – Data Structures Dynamic Array: Implementation of Stack

2 First: Review of Stacks
Stack abstraction keeps elements ordered by time of insertion: Reverse order from time of insertion Think of a stack of plates

3 Stack Functions: Abstractly
Abstractly, stack characterized by following operations: init() push(newElement) top() pop() isEmpty() ... And, the LIFO property Other APIs have different names for same stack functions

4 Stack Interface /* Stack interface. */
int isEmptyDynArr(struct DynArr *v); void pushDynArr(struct DynArr *v, TYPE val); TYPE topDynArr(struct DynArr *v); void popDynArr(struct DynArr *v); /* push, top, pop are all O(1) */

5 Arrays: Pro’s and Con’s
Only core data structure designed to hold a collection of elements Random access: can quickly get to any element  O(1) Fixed size: Maximum number of elements must be specified when created Often don’t know much much space needed until you are done Homogeneity: cannot hold different types in the same array

6 Dynamic Array (Vector or ArrayList)
The dynamic array (called Vector or ArrayList in Java, same thing, different API) gets 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

7 Size and Capacity Unlike arrays, a dynamic array can change its capacity Size (or count) is logical array size: Current number of elements in the dynamic array What the programmer thinks of as the size of the array Managed by an internal data value Capacity is physical array size: # of elements it can hold before it must resize Size Capacity data “Unused” elements

8 Partially Filled Array
data = cnt = 10 cap = 16 Size ( = cnt) Capacity ( = cap)

9 Adding an element Adding an element to end is usually easy — just increase the (logical) size, and put new value at end What happens when size reaches capacity? Must reallocate new data array - but this detail is hidden from user

10 Set Capacity: Reallocate and Copy
Before reallocation: After reallocation: data = 8 cnt = data = 16 cap = cnt = 8 cap = 8 Must allocate new (larger) array and copy valid data elements Also...don’t forget to free up the old array

11 (We will be doing this later, doesn’t occur in stack)
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 (We will be doing this later, doesn’t occur in stack)

12 Adding to Middle (cont.)
Must make space for new value Careful! Loop from top down while copying data Add at idx  idx  Before After

13 (We will be doing this later, also not part of stack ADT)
Removing an Element Removing an element will also require “sliding over” to delete the value Therefore is O(n) worst case (We will be doing this later, also not part of stack ADT)

14 Picture of Remove Element
Remove also requires loop. This time should it be from top or bottom? Remove idx  Before After

15 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.

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

17 Interface (cont.) struct DynArr {
TYPE *data; /* Pointer to data array. */ int cnt; /* Number of elements in collection. */ int cap; /* Capacity of array. */ }; /* The stack interface. */ void initDynArr(struct DynArr *v, int cap); void pushDynArr(struct DynArr *v, TYPE e); TYPE topDynArr(struct DynArr *v); void popDynArr(struct DynArr *v); int isEmptyDynArr(struct DynArr *v);

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

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

20 Concretely, in C..using the dynArray
Concretely, it’s a bit more complicated: struct DynArr d; initDynArr(&d, 8); pushDynArr(&d, 1.1); if (!isEmptyDynArr(&d)) { TYPE e = topDynArr(&d); popDynArr(&d); }

21 Your Chance In-class lesson time: finish implementation of simple push and pop, including reallocation Homework assignment deals with reallocation (why doubling is good) Next time we look at linked list implementation of stack


Download ppt "Dynamic Array: Implementation of Stack"

Similar presentations


Ads by Google