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

Computer Science Department University of Central Florida Linked Lists: Inserting Nodes COP 3502 – Computer Science I

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

View Full Document
Linked Lists: Inserting Nodes page 2 Review of Linked Lists What is a linked list? Sequence of nodes chained together Data part Link part (points to next node in the chain) Need a head pointer to point to the front of list Called myList or Head or whatever you want It’s goal in life is just to point to the head of the list Need a helper pointer to point to traverse list help_ptr We then save the value stored in myList into help_ptr , thus allowing help_ptr to also point to the front of the list And we can nod use help_ptr to traverse the list
Linked Lists: Inserting Nodes page 3 Linked Lists Accessing Nodes of a Linked List Traversing (moving through) the list We can use help_ptr to traverse the list pointed to by myList Here would be the instruction to walk one node over: Note that the syntax here is correct Why? Cuz both sides of the assignment statement are pointers to struct ll_node Let’s now examine this statement in detail And how it changes our picture help_ptr = help_ptr->next;

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

View Full Document
Linked Lists: Inserting Nodes page 4 Linked Lists Accessing Nodes of a Linked List Traversing (moving through) the list Here’s our before picture: Remember, what is the goal here? We want help_ptr to point to the second node in the list The question is: How do we accomplish this? help_ptr data next data next data next myList NULL A linked list containing many elements ……….
Linked Lists: Inserting Nodes page 5 Linked Lists Accessing Nodes of a Linked List Traversing (moving through) the list Think: That second node is located somewhere in memory It has an address Currently where is that address saved? In other words, locate the pointer that is pointing to the second node help_ptr data next data next data next myList NULL A linked list containing many elements ……….

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

View Full Document
Linked Lists: Inserting Nodes page 6 Linked Lists Accessing Nodes of a Linked List Traversing (moving through) the list Think: The “next” pointer, of the first node, is currently pointing to the second node And what is a pointer? An address! So the address of the second node is currently saved in the “next ” pointer of the first node help_ptr data next data next data next myList NULL A linked list containing many elements ……….
Linked Lists: Inserting Nodes page 7 Linked Lists Accessing Nodes of a Linked List Traversing (moving through) the list Remember: We want help_ptr to point to the second node So we need to take the address that is stored in the “next of the first node and save it into help_ptr This will make help_ptr point to the second node help_ptr data next data next data next myList NULL A linked list containing many elements ……….

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.

## This document was uploaded on 09/21/2011.

### Page1 / 68

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

View Full Document
Ask a homework question - tutors are online