This preview shows pages 1–4. Sign up to view the full content.

1 Lecture 11: Linked Lists PIC 10B Todd Wittman Data Structures s For the rest of this course, we'll study different ways to store and organize data. s Data structures b Vectors b Dynamic Arrays b Linked Lists b Stacks b Queues b Binary Search Trees b Hash Tables b Heaps s We learn different data structures, because each has its advantages and disadvantages. s We'll template our data structures, so they can hold different types of data.

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

View Full Document
2 Erasing in a Vector s Dynamic arrays and vectors allocate sequential memory blocks. s This is good for indexing. If the 10 block below is at address 1201, then the 40 block is at address 1204. s But this is bad for inserting and erasing. s If we want to erase the 20 block in the picture below, we have to shift all the elements over to the right. 20 10 30 40 10 30 40 s Inserting and erasing in a vector is an O(N) operation. Sec 12.1 : Linked Lists s Instead of allocating sequential blocks of memory, we can allow the blocks to be anywhere in the memory heap as long as we have pointers from one block to the next. 20 10 30 40 NULL s This data structure is called a linked list . s Each block is called a node . The pointers are called links . s A node has two parts: the data and the link to the next node. s The last node points to NULL, so we know where the list ends.
3 Why Should We Use Linked Lists? s Suppose we want to erase the block indicated by the arrow. A pointer that marks our position in the list is called an iterator . 20 10 30 40 NULL s All we have to do is rearrange the pointers. Then we can safely delete the 20 block, freeing up that block for future use. s

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.