9.LinkedList-05 - Linked List Linked List The simplest form...

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

View Full Document Right Arrow Icon
Linked List Linked List: The simplest form of a linked structure. It consists of a chain of data locations called nodes. Each node holds a piece of information AND a link to the next node. We can think of each node as a record. The first part of the record is a field that stores the data, and the second part of the record is a field that stores a pointer to a node. Here is a picture of what a simple linked list that stores Num values looks like: Here is a struct we would use to define a record that stores one of these nodes: struct ll { int data; struct ll *next; } Now, to actually use this record we would have to define a variable of type List_Node as follows: struct ll *my_list;
Background image of page 1

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

View Full Document Right Arrow Icon
How to access nodes of a linked list Let’s assume we already have my_list initialized to look like this: (Don’t worry how this occured, we’ll go through that in a bit.) Now, one of the most common errors dealing with pointers is “moving” the head of the list. Consider if we made my_list point to the second node we have listed. In this case, we would have NO way to access that data value in the first record. Rather than do this, what we need is a temporary List_Node pointer to help us move through the list. We can define one as follows: struct ll *help_ptr; Now our picture looks like this: As we can see, help_ptr is uninitialized. Just as it isn’t a good practice to leave variables uninitialized, it isn’t good to leave
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 8

9.LinkedList-05 - Linked List Linked List The simplest form...

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

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