{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

19_pdfsam_cs2022_lectures

19_pdfsam_cs2022_lectures - Linked lists in C Function to...

Info icon This preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Linked lists in C Function to add a cell to the head of the list returns a pointer to the new head of the list Function to print the contents of a list ListCell *add_front(ListCell *head, int data) { return make_ListCell(data, head); } void print_list(ListCell *head) { for( ; head ; head = head->next) printf(“%d\n”, head->data); }
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
Linked lists in C Function to destroy a ListCell like a destructor for ListCell in C++ Function to destroy an entire list void destroy_ListCell(ListCell *cell) { free(cell); } void destroy_list(ListCell *head) { ListCell *next; for( ; head ; head = next) { next = head->next; destroy_ListCell(head); } }
Image of page 2
Linked lists in C int main() { ListCell *head = NULL; // list initially empty // Add 1, 2, 3, ... 10 to the list for(int j=0; j<10; j++) head = add_front(head, j); // Print the first element of the list printf(“%d\n”, head->data); // Print the fourth element of the list printf(“%d\n”, head->next->next->next->data); // Print the contents of the list print_list(head); // Free memory allocated to list destroy_list(head); return 0; }
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
unions
Image of page 4
Image of page 5
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