14_-_Linked_Lists.pdf - COMP1511 Programming Fundamentals Term 3 2019 Lecture 14 What did we learn yesterday Memory ● ● Using memory beyond what's

14_-_Linked_Lists.pdf - COMP1511 Programming Fundamentals...

This preview shows page 1 - 9 out of 36 pages.

*()452356° °° ±56rogramming +,undamentals Term 3, 2019 - Lecture 14
Image of page 1

Subscribe to view the full document.

What did we learn yesterday&$ Memory Ɣ Using memory beyond what's in our functions Ɣ Allocating memory so that it lasts beyond the lifetime of the curly brackets Multiple File Projects Ɣ Using Header (*.h) and Implementation (*.c) files Ɣ Protecting our data by hiding it Ɣ Providing a nice interface with header functions
Image of page 2
What are we learning today&$ Linked Lists Ɣ Like an array, contains multiple of the same type of variable Ɣ More flexible in that it can change length Ɣ Is also able to add and remove elements from partway through the list Ɣ Tying together structs, pointers and memory allocation
Image of page 3

Subscribe to view the full document.

78ecap ±23emory Memory Allocation Ɣ Variables created inside any curly brackets will disappear when they end Ɣ Sometimes we want to create something that persists outside of this Ɣ malloc() allows us to allocate memory within our program Ɣ malloc() returns a pointer to let us know where our allocated memory is Ɣ Then we can use that pointer as if it's pointing at a normal variable Ɣ When we're finished with our allocated memory we must free() it Ɣ We can use dcc --leak-check to tell us if we're leaking memory
Image of page 4
78ecap ±23ulti±file 56rojects Splitting a project into multiple files Ɣ Header files (*.h) contain functions declarations and no running code Ɣ They're used to show what the capabilities of the code are Ɣ Implementation files (*.c) contain full implementations Ɣ This is where the complicated code is hidden Ɣ Other files (like main.c etc) will #include the header files in order to use the functionality Ɣ All C files from the project will be compiled together
Image of page 5

Subscribe to view the full document.

(& new kind of struct Let's make an interesting struct Ɣ This is a node Ɣ It contains some information Ɣ As well as a pointer to another node! struct node { struct node *next; int data; }
Image of page 6
(& *()hain of 34odes ±a 12inked 12ist A program's memory (not to scale) Node Next Data Node Next Data Node Next Data Node Next Data A pointer to the first node NULL
Image of page 7

Subscribe to view the full document.

12inked 12ists A chain of these nodes is called a Linked List As opposed to Arrays . . .
Image of page 8
Image of page 9
  • One '17

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

Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes