lists - Examples and Exercises on Linked Lists: 1....

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Examples and Exercises on Linked Lists: 1. BuildOneTwoThree() Function Here is simple function which uses pointer operations to build the list {1, 2, 3}. This function demonstrates how calls to malloc() and pointer assignments (=) work to build a pointer structure in the heap. /* Build the list {1, 2, 3} in the heap and store its head pointer in a local stack variable. returns the head pointer to the caller. */ struct node* BuildOneTwoThree() { struct node* head = NULL; struct node* second = NULL; struct node* third = NULL; head = malloc(sizeof(struct node)); // allocate 3 nodes in the heap second = malloc(sizeof(struct node)); third = malloc(sizeof(struct node)); head->data = 1; // setup first node head->next = second; // note: pointer assignment rule second->data = 2; // setup second node second->next = third; third->data = 3; // setup third link third->next = NULL; // At this point, the linked list referenced by "head" // matches the list in the drawing. return head; }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Exercise Q: Write the code with the smallest number of assignments (=) which will build the above memory structure. It requires 3 calls to malloc(). 3 int assignments (=) to setup the ints. 4 pointer assignments to setup head and the 3 next fields. With a little cleverness and knowledge of the C language, this can all be done with 7 assignment operations (=). 2. Length() Function The Length() function takes a linked list and computes the number of elements in the list. Length() is a simple list function. /* Given a linked list head pointer, compute and return the number of nodes in the list. */ int Length(struct node* head) { struct node* current = head; int count = 0; while (current != NULL) { count++; current = current->next; } return count; } Exercise void LengthTest() { struct node* myList = BuildOneTwoThree(); int len = Length(myList); // results in len == 3 }
Background image of page 2
What if we said head = NULL; at the end of Length() — would that mess up the myList variable in the caller? Ans: No. head is a local which was initialized with a copy of the actual parameter, but changes do not automatically trace back to the actual parameter. Changes to the local variables in one function do not affect the locals of another function. Exercise
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 06/12/2011.

Page1 / 8

lists - Examples and Exercises on Linked Lists: 1....

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online