{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

ch04 - Ch 4 Linked Lists Motivation Pointers in C Dynamic...

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

View Full Document Right Arrow Icon
Ch 4 – Linked Lists Motivation Pointers in C++ Dynamic Size Arrays Linked List Nodes Connecting Nodes Linked List Operations Print List Create List Insert List Multiple Insertions Insert at End Sorted Insert List Empty? List Full? Searching A List Delete From List Delete Code Implementation Example Using Head and Tail Pointers Using Doubly Linked Lists Insertion into Doubly Linked List Deletion from Doubly Linked List
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
Motivation Arrays fast to access but fixed size. Linked list invented for dynamic data of variable length. Code is more complex and often slower. Programmers must make space/time trade-off when selecting data structure.
Image of page 2
Pointers in C++ float x; variable contains float value float *y; variable contains address of a float variable y = &x; initializes y to have address of x *y = 4.2 stores 4.2 in variable x y = new float; allocates space for new variable on heap delete y gives space back to system *y = 1.7; illegal after delete called, can make program crash
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
Dynamic Size Arrays int *data; integer pointer int size = 42 variable contains address of a float variable data = new int[size] allocates array of integers on heap data[7] = 19 use array normal way *(data + 7) = 91 pointer arithmetic used to access array. (faster but ugly) delete []data; gives space back to system (failure to delete causes memory leak)
Image of page 4
Linked List Nodes We create dynamic size data structure by chaining together chunks of memory. A linked list node contains N data elements and 1 or 2 pointers to other data in the list struct Node { int ID; float GPA; Node * next; } data fields pointer to another Node record
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
Linked List Nodes We draw nodes as boxes and connect nodes with arrows from pointer fields. head ID GPA next ID GPA next null pointer
Image of page 6
Connecting Nodes pointer to Node record allocate space on heap head 123456 3.7 next Node * ptr ptr = new Node; ptr->ID = 123456; ptr->GPA = 3.7; ptr->next = NULL; set data fields
Image of page 7

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

View Full Document Right Arrow Icon
Connecting Nodes head 123456 3.7 next Node * ptr2 ptr = new Node; ptr2->ID = 567890; ptr2->GPA = 2.5; ptr2->next = ptr; create another node and add to chain 567890 2.5 next
Image of page 8
Linked List Operation Linked list do not allow random access like arrays so we must write functions to store and retrieve data.
Image of page 9

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

View Full Document Right Arrow Icon
Image of page 10
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