extensible array c and data structures baojian hua [email protected]

31
Extensible Array C and Data Structures Baojian Hua [email protected]

Upload: zoie-frake

Post on 16-Dec-2015

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Extensible Array C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Extensible Array

C and Data StructuresBaojian Hua

[email protected]

Page 2: Extensible Array C and Data Structures Baojian Hua bjhua@ustc.edu.cn

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 one predecessor

Page 3: Extensible Array C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Linear Data Structures Typical operations on linear list :// create an empty listnewList ();// the length of a list llength (list l); // insert element x at position i in l, 0<=i<ninsert (list l, x, i); // return the i-th elementnth (list l, i); // delete the element at position i in l, 0<=i<ndelete (list l, i);// apply function f to each element in lforeach (list l, f);

Page 4: Extensible Array C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Polymorphic Abstract Data Types in C// in “list.h”

#ifndef LIST_H

#define LIST_H

typedef struct listStruct *list;

list newList ();

int length (list l);

poly nth (list l, int n); // “poly”?

void insert (list l, poly x, int i);

poly delete (list l, int i);

void foreach (list l, void (*f)(poly));

#endif

Page 5: Extensible Array C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Implementations

Two typical implementation techniques: array-based linked structure-based

We next consider the first, and leave the second to the next slide

Page 6: Extensible Array C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Implementation Using Array The straightforward method to

implement this interface (ADT) 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

Page 7: Extensible Array C and Data Structures Baojian Hua bjhua@ustc.edu.cn

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

Page 8: Extensible Array C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Array-based Implementation// Combine these above observations, we have:// in file “arrayList.c”#include <stdlib.h>#include “list.h”

#define INIT_LENGTH 32#define EXT_FACTOR 2

struct listStruct{ poly *array;

int max; int tail;};

0 n-1

array

max

tail

l

Page 9: Extensible Array C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Operation: “newList”list newList (){ list l = (list)malloc (sizeof (*l)); l->array = malloc (INIT_LENTH * sizeof(poly)); l->max = INIT_LENTH; l->tail = 0;

return l;}

Page 10: Extensible Array C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Operation: “newList”list newList (){ list l = (list)malloc (sizeof (*l)); l->array = malloc (INIT_LENTH * sizeof(poly)); l->max = INIT_LENTH; l->tail = 0;

return l;}

$#%&

%$&^

@#%$

l

Page 11: Extensible Array C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Operation: “newList”list newList (){ list l = (list)malloc (sizeof (*l)); l->array = malloc (INIT_LENTH * sizeof(poly)); l->max = INIT_LENTH; l->tail = 0;

return l;}

0 n-1

array

%$&^

@#%$

l

Page 12: Extensible Array C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Operation: “newList”list newList (){ list l = (list)malloc (sizeof (*l)); l->array = malloc (INIT_LENTH * sizeof(poly)); l->max = INIT_LENTH ; l->tail = 0;

return l;}

0 n-1

array

max

@#%$

l

Page 13: Extensible Array C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Operation: “newList”list newList (){ list l = (list)malloc (sizeof (*l)); l->array = malloc (INIT_LENTH * sizeof(poly)); l->max = INIT_LENTH ; l->tail = 0;

return l;}

0 n-1

array

max

tail

l

Page 14: Extensible Array C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Operation: “length”int length (list l){ // note that we omit such checks in the next // for clarity. However, You should always do

// such kind of checks in your code. assert(l); return l->tail;}

0 n-1

array

max

tail

l

Page 15: Extensible Array C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Operation: “nth”poly nth (list l, int i){ if (i<0 || i>=l->tail) error (“invalid index”);

poly temp; temp = *((l->array)+i); return temp;}

0 n-1

array

max

tail

l

Page 16: Extensible Array C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Operation: “nth”poly nth (list l, int i){ if (i<0 || i>=l->tail) error (“invalid index”);

poly temp; temp = *((l->array)+i); return temp;}

0 n-1

array

max

tail

l

i

temp

Page 17: Extensible Array C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Operation: “insert”void insert (list l, poly x, int i){ if (i<0 || i>l->tail) error (“invalid index”);

//move the data …;}

0 n-1

array

max

tail

l

i

Page 18: Extensible Array C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Operation: “insert”void insert (list l, poly x, int i){ if (i<0 || i>l->tail) error (“invalid index”);

for (int j=l->tail; j>i; j--) (l->array)[j] = (l->array)[j-1]; …;}

0 n-1

array

max

tail

l

i

j

Page 19: Extensible Array C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Operation: “insert”void insert (list l, poly x, int i){ if (i<0 || i>l->tail) error (“invalid index”);

for (int j=l->tail; j>i; j--) (l->array)[j] = (l->array)[j-1]; …;}

0 n-1

array

max

tail

l

i

j

Page 20: Extensible Array C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Operation: “insert”void insert (list l, poly x, int i){ if (i<0 || i>l->tail) error (“invalid index”);

for (int j=l->tail; j>i; j--) (l->array)[j] = (l->array)[j-1]; …;}

0 n-1

array

max

tail

l

i

j

Page 21: Extensible Array C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Operation: “insert”void insert (list l, void *x, int i){ if (i<0 || i>l->tail) error (“invalid index”);

for (int j=l->tail; j>i; j--) (l->array)[j] = (l->array)[j-1]; (l->array)[i] = x;}

x

0 n-1

array

max

tail

l

i

j

Page 22: Extensible Array C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Operation: “insert”void insert (list l, void *x, int i){ if (i<0 || i>l->tail) error (“invalid index”);

for (int j=l->tail; j>i; j--) (l->array)[j] = (l->array)[j-1]; (l->array)[i] = x; (l->tail)++;} x

0 n-1

array

max

tail

l

i

j

Page 23: Extensible Array C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Perfect? What if the initial input arguments

look like this one? direct datamovement willincur an out-of-bound error!

0 n-1

array

max

tail

l

i

Page 24: Extensible Array C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Extensible Arrayvoid insert (list l, poly x, int i){ if (i<0 || i>l->tail) error (“invalid index”); // if l is full, extend l->array by a factor… if (l->tail==l->max) { l->array = realloc (l->array, EXT_FACTOR*(l->max)*sizeof(poly)); l->max *= EXT_FACTOR; } // data movement as discussed above…;}

Page 25: Extensible Array C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Extensible Array

0 n-1

array

max

tail

l

i

0 2n-1

i

l->array = realloc (l->array, EXT_FACTOR*(l->max)*sizeof(poly));

n-1

Page 26: Extensible Array C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Extensible Array

0 n-1

array

max

tail

l

i

0 2n-1

i

n-1

l->array = realloc (l->array, EXT_FACTOR*(l->max)*sizeof(poly));

Page 27: Extensible Array C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Extensible Array

0 n-1

array

max

tail

l

i

0 2n-1

i

l->max *= EXT_FACTOR;

l->array = realloc (l->array, EXT_FACTOR*(l->max)*sizeof(poly));

Page 28: Extensible Array C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Extensible Array

array

max

tail

l

0 2n-1

i

n-1

Page 29: Extensible Array C and Data Structures Baojian Hua bjhua@ustc.edu.cn

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

Page 30: Extensible Array C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Operation: “foreach”void foreach (list l, void (*f)(poly)){ for (int i=0; i<l->tail; i++) f (*(l->array + i)); return;}

0 n-1

array

max

tail

l

Page 31: Extensible Array C and Data Structures Baojian Hua bjhua@ustc.edu.cn

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