SlideServe
Menu
Préférences
© SlideServe · v2026

CS261 Data Structures

mendel avatar
17 diapositives · 346 Views · Jul 29, 2014

CS261 Data Structures. Dynamic Array Queue and Deque. Queues. int isEmpty (); void addBack (TYPE val ); // Add value at end of queue. TYPE front(); // Get value at front of queue. void removeFront (); / / Remove value at front. remove from the front.

CS261 Data Structures - slide 1
01 / 17CS261 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 Array Queue and Deque

  2. Queues intisEmpty(); void addBack(TYPE val); // Add value at end of queue. TYPE front(); // Get value at front of queue. void removeFront(); // Remove value at front. remove from the front add to the back

  3. Queue Applications Printer Queue What nodes are reachable from A?

  4. Queue with Dynamic Array Removal from front is expensive! front back intisEmpty(); void addBack(TYPE val); // Add value at end of queue. TYPE front(); // Get value at front of queue. void removeFront(); // Remove value at front.

  5. Deque (Double Ended Queue) ADT void addFront(TYPE val); void removeFront(); TYPE front(); void addBack(TYPE val); voidremoveBack() TYPEback(); removeBack addFront front back 5 22 8 removeFront addBack

  6. Deque Application • Finite Length Undo • Palindrome Checker ( e.g. radar)

  7. Let the partially filled block “float” & “wrap” Allow the starting index, beg, to “float” around the underlying array! It’s no longer confined to index 0 Logicalindex beg=3 beg=5 size size 3 4 0 1 2 3 0 1 2 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 Absolute index How?? Keep track of the data, arranged circularly and convert all indices into proper indices for the actual array! Circular Buffer

  8. ArrayDeque Structure structArrDeque { TYPE *data; /* Pointer to data array. */ int size; /* Number of elements in collection. */ int beg; /* Index of first element. */ int cap; /* Capacity of array. */ }; void initArrDeque(structArrDeque *d, int cap) { d->data = malloc(cap * sizeof(TYPE))); assert(d->data != 0); d->size = d->beg = 0; d->cap = cap; }

  9. Adding/Removing from Back Add Remove beg beg size size beg beg size size

  10. Adding/Removing from Front Add Remove beg beg size size beg beg size size

  11. Wrapping Around – Circular Buffer beg size Elements can wrap around from beg to end

  12. size 0 1 2 3 4 5 6 7 Wrapping Around • Calculate offset: add logical (element) index to start (beg) • offset = beg + logicalIndex; /* logIndex = 3, offset = 9 */ • If larger than or eq to capacity, subtract capacity • if (offset >= cap) • absoluteIndex = offset – cap; • Alternatively, use mod: • /* Convert logical index to absolute index. */ • absIdx = (logicalIdx + beg) % cap; beg = 6 cap = 8 beg beg = 0 cap = 8 beg size 0 1 2 3 4 5 6 7

  13. Resizing? • Can we simply copy the elements to a larger array? • Have to be careful because the wrapping is dependent on the ‘capacity’

  14. Key Changes from Dynamic Array • Keep track of floating data with beg & size • Wrap beg as necessary • Whenever accessing a logical index, convert to absolute first • On resize, copy to start of the new array and reset beg

  15. Let’s look at some code… void addFrontDynArr(DynArr *v, TYPE val){ if (v->size >= v->capacity) _dynArrSetCapacity(v, 2*v->capacity); v->beg = v->beg - 1; if(v->beg < 0) v->beg = v->capacity-1; v->data[_absoluteIndex(v, 0)] = val; v->size++; } Always make sure you have space Update beg Wrap beg as necessary Don’t forget to increment size front is at logical index = 0 convert to proper absolute index

  16. Let’s look at some code… Full implementation is in dynamicArrayDeque.c void addFrontDynArr(DynArr *v, TYPE val){ if (v->size >= v->capacity) _dynArrSetCapacity(v, 2*v->capacity); v->beg = v->beg - 1; if(v->beg < 0) v->beg = v->capacity-1; v->data[_absoluteIndex(v, 0)] = val; v->size++; } Always make sure you have space Update beg Wrap beg as necessary Don’t forget to increment size front is at logical index = 0 convert to proper absolute index

  17. Operations Analysis