Topic4 - Introduction to C+ Linear Linked Lists Topic #4...

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

View Full Document Right Arrow Icon
Introduction to C++ Linear Linked Lists Topic #4 1 CS162 Topic #4
Background image of page 1

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

View Full DocumentRight Arrow Icon
CS162 - Topic #4 • Lecture: Dynamic Data Structures – Review of pointers and the new operator – Introduction to Linked Lists – Begin walking thru examples of linked lists – Insert (beginning, end) – Removing (beginning, end) – Remove All – Insert in Sorted Order 2 CS162 Topic #4
Background image of page 2
CS162 - Pointers • What advantage do pointers give us? • How can we use pointers and new to allocating memory dynamically • Why allocating memory dynamically vs. statically? • Why is it necessary to deallocate this memory when we are done with the memory? 3 CS162 Topic #4
Background image of page 3

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

View Full DocumentRight Arrow Icon
CS162 - Pointers and Arrays • Are there any disadvantages to a dynamically allocated array? – The benefit - of course - is that we get to wait until run time to determine how large our array is. – The drawback - however - is that the array is still fixed size .... it is just that we can wait until run time to fix that size. – And, at some point prior to using the array we must determine how large it should be. 4 CS162 Topic #4
Background image of page 4
CS162 - Linked Lists • Our solution to this problem is to use linear linked lists instead of arrays to maintain a “list” • With a linear linked list, we can grow and shrink the size of the list as new data is added or as data is removed • The list is ALWAYS sized exactly appropriately for the size of the list 5 CS162 Topic #4
Background image of page 5

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

View Full DocumentRight Arrow Icon
CS162 - Linked Lists • A linear linked list starts out as empty – An empty list is represented by a null pointer – We commonly call this the head pointer head 6 CS162 Topic #4
Background image of page 6
CS162 - Linked Lists • As we add the first data item, the list gets one node added to it – So, head points to a node instead of being null – And, a node contains the data to be stored in the list and a next pointer (to the next node. ..if there is one) head a dynamically allocated node data next 7 CS162 Topic #4
Background image of page 7

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

View Full DocumentRight Arrow Icon
CS162 - Linked Lists • To add another data item we must first decide in what order – does it get added at the beginning – does it get inserted in sorted order – does it get added at the end • This term, we will learn how to add in each of these positions. 8 CS162 Topic #4
Background image of page 8
CS162 - Linked Lists • Ultimately, our lists could look like: data next data next data next ••• head tail Sometimes we also have a tail pointer. This is another pointer to a node -- but keeps track of the end of the list. This is useful if you are commonly adding data to the end 9 CS162 Topic #4
Background image of page 9

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

View Full DocumentRight Arrow Icon
CS162 - Linked Lists • So, how do linked lists differ than arrays? – An array is direct access; we supply an element number and can go directly to that element (through pointer arithmetic) – With a linked list, we must either start at the head or the tail pointer and sequentially traverse to the desired position in the list 10 CS162 Topic #4
Background image of page 10
CS162 - Linked Lists • In addition, linear linked lists (singly) are connected with just one set of next pointers. – This means you can go from the first to the
Background image of page 11

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

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

This note was uploaded on 09/30/2011 for the course CS 162 taught by Professor Fant during the Spring '08 term at Portland State.

Page1 / 55

Topic4 - Introduction to C+ Linear Linked Lists Topic #4...

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

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