














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!















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(N^2 )
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
bat ๏ cat ๏ sat ๏ vat NULL
mat ๏
Compare this with the insertion in arrays!
bat ๏ cat ๏ mat ๏ sat ๏ vat NULL
dangling reference
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));
Declaration
๏ b a t \0 NULL
address of first node
ptr data ptr^ link
ptr
e ๏ name ๏ฐ (*e).name
strcpy(ptr ๏ data, โbatโ);
ptr ๏ link = NULL;
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); }
strcpy(temp->data, data); if (node) { noempty list temp->next=node->next; node->next= temp; } else { empty list temp->next= NULL; node =temp; } }
temp
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
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?