Linked Lists

Linked Lists - Lecture 11: Linked Lists PIC 10B Todd...

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

View Full Document Right Arrow Icon
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.
Background image of page 1

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

View Full DocumentRight Arrow Icon
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.
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/27/2010 for the course PIC 157-051-20 taught by Professor Wittman during the Winter '10 term at UCLA.

Page1 / 9

Linked Lists - Lecture 11: Linked Lists PIC 10B Todd...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online