SlideServe
Menu
Préférences
© SlideServe · v2026

CS261 Data Structures

rendor avatar
12 diapositives · 289 Views · Jul 29, 2014

CS261 Data Structures. Dynamic Arrays Part 2: Implementation. Element Types - TYPE. 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. Interface File: dynarr.h.

CS261 Data Structures - slide 1
01 / 12CS261 Data Structures
1 / 17

Télécharger la présentation

CS261 Data Structures
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. CS261 Data Structures Dynamic Arrays Part 2: Implementation

  2. Element Types - TYPE • 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.

  3. Interface File: dynarr.h #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

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

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

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

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

  8. Better Solution To use a structdynArr, 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

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

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

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

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