Download presentation

Presentation is loading. Please wait.

1
**CSCE 3110 Data Structures & Algorithm Analysis**

Rada Mihalcea Growable Arrays. Lists. Reading: Chap. 3 Weiss

2
**Linked Lists Avoid the drawbacks of fixed size arrays with**

Growable arrays Linked lists

3
**Growable arrays Avoid the problem of fixed-size arrays**

Increase the size of the array when needed (I.e. when capacity is exceeded) Two strategies: tight strategy (add a constant): f(N) = N + c growth strategy (double up): f(N) = 2N

4
Tight Strategy Add a number k (k = constant) of elements every time the capacity is exceeded 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 C0 + (C0+k) + … (C0+Sk) = S = (N – C0) / k Running time? C0 * S + S*(S+1) / 2 O(N2)

5
**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++; }

6
Growth Strategy Double the size of the array every time is needed (I.e. capacity exceeded) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 C0 + (C0 * 2) + (C0*4) + … + (C0*2i) = i = log (N / C0) Running time? C0 [ … + 2 log(N/C0) ] O(N) How does the previous code change?

7
**Linked Lists Avoid the drawbacks of fixed size arrays with**

Growable arrays Linked lists

8
**Using Dynamically Allocated Memory (review)**

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

9
Linked Lists bat cat sat vat NULL

10
**Insertion bat cat sat vat NULL mat **

Compare this with the insertion in arrays!

11
Deletion bat cat mat sat vat NULL dangling reference

12
**List ADT ADT with position-based methods**

generic 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)

13
**Implementation Declaration**

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

14
**Create one Node e name (*e).name strcpy(ptr data, “bat”);**

ptr link = NULL; address of first node ptr data ptr link b a t \ NULL ptr

15
**Example: Create a two-nodes list**

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; } 20 NULL ptr

16
**Insert (after a specific position)**

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); }

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

18
Deletion node trail = NULL node 20 NULL 20 NULL (a) before deletion (b)after deletion Delete node other than the first node head node head 20 NULL 20 NULL

19
**void removeAfter(pnode node) { /**

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); } } node 20 NULL 20 NULL

20
**Traverse a list Where does ptr point after this function call?**

void traverseList(pnode ptr) { printf(“The list contains: “); for ( ; ptr; ptr = ptr->next) printf(“%4d”, ptr->data); printf(“\n”); } Where does ptr point after this function call?

21
**Other List Operations swapElements insertFirst insertLast deleteBefore**

deleteLast

22
**Running Time Analysis insertAfter O(?) deleteAfter O(?)**

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

Similar presentations

Presentation is loading. Please wait....

OK

DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI

DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI

© 2018 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google

Ppt on hybrid renewable energy systems Lecture ppt on digital image processing Ppt on network security algorithms Gas plasma display ppt online Ppt on different model of atom Ppt on quality education quotes Ppt on our government of india Ppt on event handling in javascript which term Ppt on the seven ages poem by william shakespeare Ppt on earthquake for class 7