Download presentation
Presentation is loading. Please wait.
1
Extensible Array C and Data Structures Baojian Hua bjhua@ustc.edu.cn
2
Linear Data Structures A linear list (list) consists of: a collection of data elements (e1, e2, …, en) elements are ordered: e1 e2 … en ei is called an predecessor of e_{i+1} e_{i+1} is called a successor of ei every element has at most one successor and predecessor
3
Linear Data Structures Typical operations on linear list : // create an empty list newList (); // the length of a list l length (l); // insert element x at position i in l, 0<=i<=n+1 insert (l, x, i); // return the i-th element nth (l, i); // delete the element at position i in l, 0<=i<=n delete (l, i); // apply function f to each element in l foreach (l, f);
4
Abstract Data Types in C // in “list.h” #ifndef LIST_H #define LIST_H typedef struct list *list; list new (); int length (list l); poly nth (list l, int n); // note the type “poly” void insert (list l, poly x, int i); poly delete (list l, int i); void foreach (list l, void (*f)(poly)); #endif
5
Implementation Using Array The straightforward method to implement this interface is to use an array and the array may not be full, so we must keep a “ tail ” tag to record its tail (the position of its last elements) 0 n-1
6
Implementation Using Array The straightforward method to implement this interface is to use an array and the array may not be full, so we must keep a “ tail ” tag to record its tail (the position of its last elements) 0 n-1 tail
7
Array-based Implementation // Combine these above observations, we have: // in file “arrayList.c” #include #include “list.h” #define initLength 32; #define extFactor 2; struct list { poly *array; int max; int tail; }; 0 n-1 array max tail l
8
Operation: “ new ” list new () { list l = malloc (sizeof (*l)); l->array = malloc (initLength * sizeof(poly)); l->length = initLength; l->tail = -1; return l; }
9
Operation: “ new ” list new () { list l = malloc (sizeof (*l)); l->array = malloc (initLength * sizeof(poly)); l->length = initLength; l->tail = -1; return l; } $#%& %$&^ @#%$ l
10
Operation: “ new ” list new () { list l = malloc (sizeof (*l)); l->array = malloc (initLength * sizeof(poly)); l->length = initLength; l->tail = -1; return l; } 0 n-1 array %$&^ @#%$ l
11
Operation: “ new ” list new () { list l = checkedMalloc (sizeof (*l)); l->array = checkedMalloc (initLength * sizeof(void *)); l->max = initLength; l->tail = -1; return l; } 0 n-1 array max @#%$ l
12
Operation: “ new ” list new () { list l = checkedMalloc (sizeof (*l)); l->array = checkedMalloc (initLength * sizeof(void *)); l->max = initLength; l->tail = -1; return l; } 0 n-1 array max tail l
13
Operation: “ length ” int length (list l) { // note that we omit such checks in the next // for clarity. You should always do such kind // of checks in your code. assert(l); return (l->tail)+1; } 0 n-1 array max tail l
14
Operation: “ nth ” poly nth (list l, int i) { if (i =length(l)) error (“invalid index”); poly temp; temp = *((l->array)+i); return temp; } 0 n-1 array max tail l
15
Operation: “ nth ” poly nth (list l, int i) { if (i =length(l)) error (“invalid index”); void *temp; temp = *((l->array)+i); return temp; } 0 n-1 array max tail l i temp
16
Operation: “ insert ” void insert (list l, poly x, int i) { if (i length(l)) error (“invalid index”); //move the data …; } 0 n-1 array max tail l i
17
Operation: “ insert ” void insert (list l, poly x, int i) { if (i length(l)) error (“invalid index”); for (int j=l->tail; j>=i; j--) (l->array)[j+1] = (l->array)[j]; …; } 0 n-1 array max tail l i j
18
Operation: “ insert ” void insert (list l, poly x, int i) { if (i length(l)) error (“invalid index”); for (int j=l->tail; j>=i; j--) (l->array)[j+1] = (l->array)[j]; …; } 0 n-1 array max tail l i j
19
Operation: “ insert ” void insert (list l, poly x, int i) { if (i length(l)) error (“invalid index”); for (int j=l->tail; j>=i; j--) (l->array)[j+1] = (l->array)[j]; …; } 0 n-1 array max tail l i j
20
Operation: “ insert ” void insert (list l, poly x, int i) { if (i length(l)) error (“invalid index”); for (int j=l->tail; j>=i; j--) (l->array)[j+1] = (l->array)[j]; …; } 0 n-1 array max tail l i j
21
Operation: “ insert ” void insert (list l, poly x, int i) { if (i length(l)) error (“invalid index”); for (int j=l->tail; j>=i; j--) (l->array)[j+1] = (l->array)[j]; …; } 0 n-1 array max tail l i j
22
Operation: “ insert ” void insert (list l, void *x, int i) { if (i length(l)+1) error (“invalid index”); for (int j=l->tail; j>=i; j--) (l->array)[j+1] = (l->array)[j]; (l->array)[i]=x; } x 0 n-1 array max tail l i j
23
Operation: “ insert ” void insert (list l, void *x, int i) { if (i length(l)+1) error (“invalid index”); for (int j=l->tail; j>=i; j--) (l->array)[j+1] = (l->array)[j]; (l->array)[i] = x; (l->tail)++; } x 0 n-1 array max tail l i j
24
Perfect? No, It ’ s Wrong! What if the initial input arguments look like this one? direct data movement will incur an out-of-bound error! 0 n-1 array max tail l i
25
Extensible Array void insert (list l, poly x, int i) { if (i length(l)) error (“invalid index”); // if l is full, extend l->array by a factor 2 if (l->tail+1==l->max) { l->array = realloc (l->array, extFacotr* (l->max)*sizeof(poly)); l->max *= extFactor; } // data movement as discussed above…; }
26
Extensible Array 0 n-1 array max tail l i 02n-2 i l->array = realloc (l->array, extFactor*(l->max)); n-1
27
Extensible Array 0 n-1 array max tail l i 02n-1 i l->array = realloc (l->array, extFactor*(l->max)); n-1
28
Extensible Array 0 n-1 array max tail l i 02n-1 i l->array = realloc (l->array, extFactor *(l->max)); l->max *= extFactor;
29
Extensible Array array max tail l 02n-1 i
30
Operation: “ delete ” The “ delete ” operation is reverse operation of the “ insert ” operation also involves data movement should we shrink the extensible array, when there are few elements in it (say ½ data item left)? See the programming assignment
31
Operation: “ foreach ” void foreach (list l, void (*f)(poly)) { for (int i=0; i tail; i++) f (*(l->array + i)); return; } 0 n-1 array max tail l
32
Summary Linear list ADT: a collection of ordered data element each item has no more than one successor or predecessor Extensible array-based implementation maintain internally a dynamically extensible array bad performance with insert or delete space waste
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.