{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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; }
Image of page 1

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

View Full Document Right 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 }
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.
Image of page 3

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern