Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structures and Linked Lists in C/C++ ICS 51. Structures Definition of structure A datatype in C A notion of record aggregating a set of objects into a.

Similar presentations


Presentation on theme: "Structures and Linked Lists in C/C++ ICS 51. Structures Definition of structure A datatype in C A notion of record aggregating a set of objects into a."— Presentation transcript:

1 Structures and Linked Lists in C/C++ ICS 51

2 Structures Definition of structure A datatype in C A notion of record aggregating a set of objects into a single object One or more members, possibly different types Example struct grade { intstudentID; char* lastName; char* firstName; intscore; } struct grade my_grade, my_friends_grade; Memory allocation Consecutive locations, may be 4-byte aligned or not (depends on compiler) In order to avoid alignment uncertainty, use all the fields of the same 4-byte size

3 Linked List In one of the assignments, we will use linked list typedef struct list_struc { struct list_struc * next_element; int value; } list; Example of linked list each element is of type list; head = &elem1 elem1 = {&elem2, -2} elem2 = {&elem3, 0} elem3 = {&elem4, 1} elem4 = {&elem5, 3} elem5 = {NULL, 5} ‘&’ means ‘address of’; NULL – reserved address with the value of 0 elem2 -2 elem3 0 pointer to head elem4 1 elem5 3 NULL 5 elem1 elem5

4 Sample program #include typedef struct linked_list { struct linked_list *next_element; int value; } list; int sum_c(struct linked_list *link_ptr) { int sum=0; while (link_ptr != NULL) ////// we go through the list till it reaches NULL { sum=sum+(*link_ptr).value; link_ptr=(*link_ptr).next_element; ////// we move the pointer to the next element } return sum; } __declspec(naked) int sum_asm(struct linked_list *link_ptr) { __asm { push ebx //////// we 'd better push the registers before using them push ecx //////////// same thing mov eax,0 mov ebx, dword ptr[esp+12] /////////// the parameter passed is in esp+12 MAIN_LOOP: cmp ebx, NULL je END mov ecx, dword ptr[ebx+4] /////////// moves the second 4 bytes of the strcuture (.value) into ecx add eax,ecx mov ebx, dword ptr[ebx] ///////// moves the first 4 bytes of the structure (.next_element) into ebx jmp MAIN_LOOP END: pop ecx ////////// pop the pushed registers before returning pop ebx //////////// same thing ret} See next page…

5 Sample program (cont.) void main() { struct linked_list first, second, third, fourth, fifth, sixth; ///// we define some elements struct linked_list *list_ptr; // we define a pointer to an element or a list, // the pointer initially points to a dummy location. first.value=10; /////////// intialization. first.next_element=&second; /////////// the pointer of the second obejct is copied to the first.next_element. second.value=2; second.next_element=&third; third.value=5; third.next_element=&fourth; fourth.value=100; fourth.next_element=&fifth; fifth.value=20; fifth.next_element=&sixth; sixth.value=17; sixth.next_element=NULL;//////////// NULL is defined in list_ptr=&third; printf("the returned value for a single element like sixth is %d\n",sum_c(&sixth)); printf("the returned value for the list starting from the third element is %d\n",sum_asm(list_ptr)); printf("the returned value for the whole list is %d\n",sum_asm(&first)); third.next_element=&sixth; ////////////// deleting elements fourth and fifth printf("the returned value after deleting elements fourth and fifth is %d\n",sum_asm(&first)); }


Download ppt "Structures and Linked Lists in C/C++ ICS 51. Structures Definition of structure A datatype in C A notion of record aggregating a set of objects into a."

Similar presentations


Ads by Google