COP3502_6_LinkedLists2 - Linked Lists: Inserting Nodes...

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

View Full Document Right Arrow Icon
Computer Science Department University of Central Florida Linked Lists: Inserting Nodes COP 3502 – Computer Science I
Background image of page 1

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

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

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

View Full DocumentRight Arrow Icon
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 ……….
Background image of page 4
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 ……….
Background image of page 5

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

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

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

View Full DocumentRight Arrow Icon
Linked Lists: Inserting Nodes page 8 Linked Lists
Background image of page 8
Image of page 9
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

COP3502_6_LinkedLists2 - Linked Lists: Inserting Nodes...

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

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