Topic#8 - CS162 - Topic #8 Lecture: Dynamic Data Structures...

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

View Full Document Right Arrow Icon
CS162 - Topic #8 Lecture: Dynamic Data Structures Walk through the examples assigned at the end of last class inserting at the end of a linked list removing at the beginning of a linked list Stacks and Queues Programming Assignment Questions
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 - Inserting at End Add a node at the end of a linked list. What is wrong with the following. Correct it in class: node * current = head; while (current != NULL) { current = current->next; } current= new node; current->data = new video; current->data = data_to_be_stored; } LOOK AT THE BOLD/ITALICS FOR HINTS OF WHAT IS WRONG!
Background image of page 2
CS162 - Inserting at End We need a temporary pointer because if we use the head pointer we will lose the original head of the list and therefore all of our data If our loop’s stopping condition is if current is not null -- then what we are saying is loop until current IS null well, if current is null, then dereferencing current will give us a segmentation fault and, we will NOT be pointing to the last node!
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 - Inserting at End Instead, think about the “before” and “after” pointer diagrams: 1ST NTH ••• head Before 1ST NTH ••• head After new node current
Background image of page 4
CS162 - Inserting at End So, we want to loop until current->next is not NULL! But, to do that, we must make sure current isn’t NULL This is because if the list is empty, current will be null and we’ll get a fault (or should) by dereferencing the pointer if (current) while (current->next != NULL) { current = current->next; }
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 - Inserting at End Next, we need to connect up the nodes having the last node point to this new node current->next = new node; then, traverse to this new node: current = current->next; current->data = new video; and, set the next pointer of this new last node to null: current->next = NULL;
Background image of page 6
CS162 - Inserting at End Lastly, in our first example for today, it was inappropriate to just copy over the pointers to our
Background image of page 7

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

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

Page1 / 26

Topic#8 - CS162 - Topic #8 Lecture: Dynamic Data Structures...

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

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