{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

linkedLists - Recursive Data Representations We can define...

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

View Full Document Right Arrow Icon
Recursive Data Representations We can define structures with pointer fields that refer to the structure type containing them. struct list { int data; struct list *next; } The pointer variable next is called a link. Each structure is linked to a succeeding structure by way of the field next . The pointer variable next contains an address of either the location in memory of the successor struct list element or the special value NULL . 1 data next
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
Example: struct list a, b, c; a.data = 1; b.data = 2; c.data = 3; a.next = b.next = c.next = NULL; a.next = &b; b.next = &c; a.next->data has value 2 a.next->next->data has value 3 b.next->next->data error !! 2 b a c NULL NULL NULL 3 2 1 b a c NULL 3 2 1 data data data next next next
Image of page 2
Static and Dynamic Variables Static Variables: - They are created during compilation. (Fixed memory is reserved for them.) - They cannot be allocated/deallocated during the execution of the program. - Names are associated with them. int x; char y[10]; int z[100]; Dynamic Variables: - They are created (allocated) and deallocated during the execution of the program. - no names are associated with them. The only way to access them is to use pointers. - They don’t exist during compilation. Once they are created they contain data and must have a type like any other variable. Thus we can talk about creating a new dynamic variable of type x and setting a pointer to point to it, or returning a dynamic variable of type x to the system (deallocation). 3
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
A Conceptual View of Memory 4 PROGRAM MEMORY RAM main called and standard functions global program heap system stack DATA MEMORY
Image of page 4
Dynamic Data For example : We must maintain a list of data At some moments, the list is small, so we want to use only a little memory At other moments, the list is larger, so we need to use more memory Declaring variables in the standard way won’t work here because we don’t know how many variables to declare We need a way to allocate and deallocate data dynamically (i.e., on the fly) 5
Image of page 5

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

View Full Document Right Arrow Icon
Dynamic Memory Allocation Creating and maintaining dynamic data structures requires dynamic memory allocation – the ability for
Image of page 6
Image of page 7
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