CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 Growable Arrays. Lists....

22
CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea http://www.cs.unt.edu/~rada/CSCE3110 Growable Arrays. Lists. Reading: Chap. 3 Weiss

Transcript of CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 Growable Arrays. Lists....

Page 1: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 Growable Arrays. Lists. Reading: Chap. 3 Weiss.

CSCE 3110Data Structures & Algorithm Analysis

Rada Mihalceahttp://www.cs.unt.edu/~rada/CSCE3110

Growable Arrays. Lists.Reading: Chap. 3 Weiss

Page 2: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 Growable Arrays. Lists. Reading: Chap. 3 Weiss.

Linked Lists

Avoid the drawbacks of fixed size arrays with

Growable arraysLinked lists

Page 3: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 Growable Arrays. Lists. Reading: Chap. 3 Weiss.

Growable arrays

Avoid the problem of fixed-size arraysIncrease the size of the array when needed (I.e. when capacity is exceeded)Two strategies:

tight strategy (add a constant): f(N) = N + cgrowth strategy (double up): f(N) = 2N

Page 4: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 Growable Arrays. Lists. Reading: Chap. 3 Weiss.

Tight Strategy

Add a number k (k = constant) of elements every time the capacity is exceeded

123456789101112131415

C0 + (C0+k) + … (C0+Sk) =

S = (N – C0) / k

Running time?

C0 * S + S*(S+1) / 2 O(N2)

Page 5: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 Growable Arrays. Lists. Reading: Chap. 3 Weiss.

Tight Strategy

void insertLast(int rear, element o) {if ( size == rear) {

capacity += k;element* B = new element[capacity];for(int i=0; i<size; i++) { B[i] = A[i];}

A = B; } A[rear] = o;

rear++; size++; }

Page 6: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 Growable Arrays. Lists. Reading: Chap. 3 Weiss.

Growth Strategy

Double the size of the array every time is needed (I.e. capacity exceeded)1

23456789101112131415

C0 + (C0 * 2) + (C0*4) + … + (C0*2i) =

i = log (N / C0)

Running time?

C0 [1 + 2 + … + 2 log(N/C0) ] O(N)

How does the previous code change?

Page 7: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 Growable Arrays. Lists. Reading: Chap. 3 Weiss.

Linked Lists

Avoid the drawbacks of fixed size arrays with

Growable arraysLinked lists

Page 8: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 Growable Arrays. Lists. Reading: Chap. 3 Weiss.

int i, *pi;float f, *pf;pi = (int *) malloc(sizeof(int));pf = (float *) malloc (sizeof(float));*pi =1024;*pf =3.14;printf(”an integer = %d, a float = %f\n”, *pi, *pf);free(pi);free(pf);

request memory

return memory

Using Dynamically Allocated Memory (review)

Page 9: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 Growable Arrays. Lists. Reading: Chap. 3 Weiss.

bat cat sat vat NULL

Linked Lists

Page 10: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 Growable Arrays. Lists. Reading: Chap. 3 Weiss.

bat cat sat vat NULL

mat

Insertion

Compare this with the insertion in arrays!

Page 11: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 Growable Arrays. Lists. Reading: Chap. 3 Weiss.

bat cat sat vat NULL mat

danglingreference

Deletion

Page 12: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 Growable Arrays. Lists. Reading: Chap. 3 Weiss.

List ADT

ADT with position-based methodsgeneric methods size(), isEmpty()query methods isFirst(p), isLast(p)accessor methods first(), last()

before(p), after(p)update methods swapElements(p,q),

replaceElement(p,e)insertFirst(e), insertLast(e)insertBefore(p,e),

insertAfter(p,e)removeAfter(p)

Page 13: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 Growable Arrays. Lists. Reading: Chap. 3 Weiss.

typedef struct node, *pnode;typedef struct node { char data [4]; pnode next; };Creationpnode ptr =NULL; Testing#define IS_EMPTY(ptr) (!(ptr))Allocationptr=(pnode) malloc (sizeof(node));

Declaration

Implementation

Page 14: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 Growable Arrays. Lists. Reading: Chap. 3 Weiss.

b a t \0 NULL

address offirst node

ptr data ptr link

ptr

e name (*e).namestrcpy(ptr data, “bat”);ptr link = NULL;

Create one Node

Page 15: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 Growable Arrays. Lists. Reading: Chap. 3 Weiss.

pnode create2( ){/* create a linked list with two nodes */ pnode first, second; first = (pnode) malloc(sizeof(node)); second = ( pnode) malloc(sizeof(node)); second -> next= NULL; second -> data = 20; first -> data = 10; first ->next= second; return first;} 10 20 NULL

ptr

Example: Create a two-nodes list

Page 16: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 Growable Arrays. Lists. Reading: Chap. 3 Weiss.

void insertAfter(pnode node, char* data){/* insert a new node with data into the list ptr after node */ pnode temp; temp = (pnode) malloc(sizeof(node)); if (IS_FULL(temp)){ fprintf(stderr, “The memory is full\n”); exit (1); }

Insert (after a specific position)

Page 17: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 Growable Arrays. Lists. Reading: Chap. 3 Weiss.

strcpy(temp->data, data); if (node) { noempty list temp->next=node->next; node->next= temp; } else { empty list temp->next= NULL; node =temp; }}

50

10 20 NULL

temp

node

Page 18: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 Growable Arrays. Lists. Reading: Chap. 3 Weiss.

10 20 NULL 50 20 NULL 50

node trail = NULL node

(a) before deletion (b)after deletion

Deletion

Delete node other than the first node

10 20 NULL 50 20 NULL 10

head node head

Page 19: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 Growable Arrays. Lists. Reading: Chap. 3 Weiss.

void removeAfter(pnode node){/* delete what follows after node in the list */ pnode tmp; if (node) { tmp = node -> next; node->next = node->next->next; free(tmp); }}

10 20 NULL 50

20 NULL 10

node

Page 20: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 Growable Arrays. Lists. Reading: Chap. 3 Weiss.

void traverseList(pnode ptr){ printf(“The list contains: “); for ( ; ptr; ptr = ptr->next) printf(“%4d”, ptr->data); printf(“\n”); }

Traverse a list

Where does ptr point after this function call?

Page 21: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 Growable Arrays. Lists. Reading: Chap. 3 Weiss.

Other List Operations

swapElementsinsertFirstinsertLastdeleteBeforedeleteLast

Page 22: CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea rada/CSCE3110 Growable Arrays. Lists. Reading: Chap. 3 Weiss.

Running Time Analysis

insertAfter O(?)deleteAfter O(?)deleteBefore O(?)deleteLast O(?)insertFirst O(?)insertLast O(?)