Balanced and Restorative Justice for Juveniles - Youth and Family
Transcript of Balanced and Restorative Justice for Juveniles - Youth and Family
![Page 1: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/1.jpg)
Linked Lists
![Page 3: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/3.jpg)
Array-Based Allocation
● Our current implementation of Vector and Stack use dynamically-allocated arrays.
● To append an element:● If there is free space, put the element into
that space.● Otherwise, get a huge new array and move
everything over.
1 2 3
![Page 4: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/4.jpg)
Array-Based Allocation
● Our current implementation of Vector and Stack use dynamically-allocated arrays.
● To append an element:● If there is free space, put the element into
that space.● Otherwise, get a huge new array and move
everything over.
1 2 3 4
![Page 5: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/5.jpg)
Array-Based Allocation
● Our current implementation of Vector and Stack use dynamically-allocated arrays.
● To append an element:● If there is free space, put the element into
that space.● Otherwise, get a huge new array and move
everything over.
1 2 3 41 2 3 4
![Page 6: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/6.jpg)
Array-Based Allocation
● Our current implementation of Vector and Stack use dynamically-allocated arrays.
● To append an element:● If there is free space, put the element into
that space.● Otherwise, get a huge new array and move
everything over.
1 2 3 41 2 3 4 5
![Page 7: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/7.jpg)
Array-Based Allocation
● Our current implementation of Vector and Stack use dynamically-allocated arrays.
● To append an element:● If there is free space, put the element into
that space.● Otherwise, get a huge new array and move
everything over.
1 2 3 41 2 3 4 5 6
![Page 8: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/8.jpg)
A Different Idea
● Instead of reallocating a huge array to get the space we need, why not just get a tiny amount of extra space for the next element?
● Taking notes – when you run out of space on a page, you just get a new page. You don't copy your entire set of notes onto a longer sheet of paper!
![Page 9: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/9.jpg)
Excuse Me, Coming Through...
1 2 3 41 2 3 4 5 6 7
![Page 10: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/10.jpg)
Excuse Me, Coming Through...
1 2 3 41 2 3 4 5 6 7
137
![Page 11: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/11.jpg)
Excuse Me, Coming Through...
1 2 3 41 2 3 4 5 6 7 7
137
![Page 12: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/12.jpg)
Excuse Me, Coming Through...
1 2 3 41 2 3 4 5 6 6 7
137
![Page 13: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/13.jpg)
Excuse Me, Coming Through...
1 2 3 41 2 3 4 5 5 6 7
137
![Page 14: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/14.jpg)
Excuse Me, Coming Through...
1 2 3 41 2 3 4 4 5 6 7
137
![Page 15: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/15.jpg)
Excuse Me, Coming Through...
1 2 3 41 2 3 3 4 5 6 7
137
![Page 16: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/16.jpg)
Excuse Me, Coming Through...
1 2 3 41 2 2 3 4 5 6 7
137
![Page 17: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/17.jpg)
Excuse Me, Coming Through...
1 2 3 41 1 2 3 4 5 6 7
137
![Page 18: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/18.jpg)
Excuse Me, Coming Through...
1 2 3 4137 1 2 3 4 5 6 7
![Page 19: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/19.jpg)
Shoving Things Over
● Right now, inserting an element into a middle of a Vector can be very costly.
● Couldn't we just do something like this?
1 2 3 41 2 3 4 5 6 7
![Page 20: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/20.jpg)
Shoving Things Over
● Right now, inserting an element into a middle of a Vector can be very costly.
● Couldn't we just do something like this?
1 2 3 41 2 3 4 5 6 7
137
![Page 21: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/21.jpg)
Shoving Things Over
● Right now, inserting an element into a middle of a Vector can be very costly.
● Couldn't we just do something like this?
1 2 3 41 2 3 4 5 6 7
137
![Page 22: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/22.jpg)
Shoving Things Over
● Right now, inserting an element into a middle of a Vector can be very costly.
● Couldn't we just do something like this?
1 2 3 41 2 3 4 5 6 7
137
![Page 23: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/23.jpg)
Linked Lists at a Glance
● A linked list is a data structure for storing a sequence of elements.
● Each element is stored separately from the rest.
● The elements are then chained together into a sequence.
1 2 3
![Page 24: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/24.jpg)
Linked Lists at a Glance
● A linked list is a data structure for storing a sequence of elements.
● Each element is stored separately from the rest.
● The elements are then chained together into a sequence.
1 2 3 4
![Page 25: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/25.jpg)
Linked Lists at a Glance
● A linked list is a data structure for storing a sequence of elements.
● Each element is stored separately from the rest.
● The elements are then chained together into a sequence.
1 2 3 4
![Page 26: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/26.jpg)
Linked Lists at a Glance
● A linked list is a data structure for storing a sequence of elements.
● Each element is stored separately from the rest.
● The elements are then chained together into a sequence.
1 2 3 4
![Page 27: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/27.jpg)
Linked Lists at a Glance
● A linked list is a data structure for storing a sequence of elements.
● Each element is stored separately from the rest.
● The elements are then chained together into a sequence.
1 2 3 4137
![Page 28: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/28.jpg)
Linked Lists at a Glance
● A linked list is a data structure for storing a sequence of elements.
● Each element is stored separately from the rest.
● The elements are then chained together into a sequence.
1 3 4137
![Page 29: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/29.jpg)
Linked Lists at a Glance
● Can efficiently splice new elements into the list or remove existing elements anywhere in the list.
● Never have to do a massive copy step; worst-case insertion is efficient.
● Has some tradeoffs; we'll see this later.
![Page 30: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/30.jpg)
Building our Vocabulary
● In order to use linked lists, we will need to introduce or revisit several new language features:● Structures● Dynamic allocation● Null pointers
![Page 31: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/31.jpg)
Building our Vocabulary
In order to use linked lists, we will need to introduce or revisit several new language features:● Structures
Dynamic allocation
Null pointers
![Page 32: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/32.jpg)
Structures
● In C++, a structure is a type consisting of several individual variables all bundled together.
● To create a structure, we must● Define what fields are in the structure, then● Create a variable of the appropriate type.
● Similar to using classes – need to define and implement the class before we can use it.
![Page 33: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/33.jpg)
Defining Structures
● You can define a structure by using the struct keyword:
struct TypeName {
/* … field declarations … */
};
● For those of you with a C background: in C++, “typedef struct” is not necessary.
![Page 34: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/34.jpg)
A Simple Structure
struct Tribute { string name; int districtNumber;};
Tribute t;t.name = "Katniss Everdeen";t.districtNumber = 12;
![Page 35: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/35.jpg)
A Simple Structure
struct Tribute { string name; int districtNumber;};
Tribute t;t.name = "Katniss Everdeen";t.districtNumber = 12;
![Page 36: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/36.jpg)
A Simple Structure
struct Tribute { string name; int districtNumber;};
Tribute t;t.name = "Katniss Everdeen";t.districtNumber = 12;
![Page 37: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/37.jpg)
structs and classes
● In C++, a class is a pair of an interface and an implementation.● Interface controls how the class is to be
used.● Implementation specifies how it works.
● A struct is a stripped-down version of a class:● Purely implementation, no interface.● Primarily used to bundle information
together when no interface is needed.
![Page 38: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/38.jpg)
Building our Vocabulary
● In order to use linked lists, we will need to introduce or revisit several new language features:● Structures● Dynamic allocation● Null pointers
![Page 39: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/39.jpg)
Building our Vocabulary
In order to use linked lists, we will need to introduce or revisit several new language features:
Structures● Dynamic allocation
Null pointers
![Page 40: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/40.jpg)
Dynamic Memory Allocation
● We have seen the new keyword used to allocate arrays, but it can also be used to allocate single objects.
● The syntax
new T(args)
creates a new object of type T passing the appropriate arguments to the constructor, then returns a pointer to it.
![Page 41: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/41.jpg)
Dynamic Memory Allocation
int* ptr;ptr = new int;
*ptr = 137;
![Page 42: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/42.jpg)
Dynamic Memory Allocation
int* ptr;ptr = new int;
*ptr = 137;
ptr
?
![Page 43: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/43.jpg)
Dynamic Memory Allocation
int* ptr;ptr = new int;
*ptr = 137;
ptr
?
poof!
![Page 44: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/44.jpg)
Dynamic Memory Allocation
int* ptr;ptr = new int;
*ptr = 137;
ptr
![Page 45: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/45.jpg)
Dynamic Memory Allocation
int* ptr;ptr = new int;
*ptr = 137;
ptr
![Page 46: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/46.jpg)
Dynamic Memory Allocation
int* ptr;ptr = new int;
*ptr = 137;
ptr137
![Page 47: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/47.jpg)
Cleaning Up
● As with dynamic arrays, you are responsible for cleaning up memory allocated with new.
● You can deallocate memory with the delete keyword:
delete ptr;
● This destroys the object pointed at by the given pointer, not the pointer itself.
ptr137
![Page 48: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/48.jpg)
Cleaning Up
● As with dynamic arrays, you are responsible for cleaning up memory allocated with new.
● You can deallocate memory with the delete keyword:
delete ptr;
● This destroys the object pointed at by the given pointer, not the pointer itself.
ptr137
![Page 49: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/49.jpg)
Cleaning Up
● As with dynamic arrays, you are responsible for cleaning up memory allocated with new.
● You can deallocate memory with the delete keyword:
delete ptr;
● This destroys the object pointed at by the given pointer, not the pointer itself.
ptr
???
![Page 50: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/50.jpg)
Unfortunately...
● In C++, all of the following result in undefined behavior:● Deleting an object with delete[] that was
allocated with new.● Deleting an object with delete that was
allocated with new[].
● Although it is not always an error, it is usually a Very Bad Idea to treat an array like a single object or vice-versa.
![Page 51: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/51.jpg)
Pointers and Structures
![Page 52: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/52.jpg)
A Tale of Dots and Stars
● If we have a pointer to a structure, like this one:
Tribute* ptr = new Tribute;
● We cannot access the fields by using dot, since ptr is not an actual Tribute.
● The following doesn't work either:
*ptr.districtNumber = 13;
because it's interpreted as
*(ptr.districtNumber) = 13
and not
(*ptr).districtNumber = 13;
![Page 53: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/53.jpg)
Arrow to the Rescue
● To access a field in a structure or class through a pointer, you can write
(*ptr).districtNumber = 13;
● However, it's much easier to use the arrow operator (->)
ptr->districtNumber = 13;
● The arrow operator is so convenient that we almost always use it instead of using the parenthesis/star.
![Page 54: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/54.jpg)
Building our Vocabulary
● In order to use linked lists, we will need to introduce or revisit several new language features:● Structures● Dynamic allocation● Null pointers
![Page 55: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/55.jpg)
Building our Vocabulary
In order to use linked lists, we will need to introduce or revisit several new language features:
Structures
Dynamic allocation● Null pointers
![Page 56: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/56.jpg)
A Pointless Exercise
● When working with pointers, we sometimes wish to indicate that a pointer is not pointing to anything.
● In C++, you can set a pointer to NULL to indicate that it is not pointing to an object:
ptr = NULL;
● This is not the default value for pointers; by default, pointers point to arbitrary locations in memory.
![Page 57: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/57.jpg)
Building our Vocabulary
● In order to use linked lists, we will need to introduce or revisit several new language features:● Structures● Dynamic allocation● Null pointers
![Page 58: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/58.jpg)
Building our Vocabulary
In order to use linked lists, we will need to introduce or revisit several new language features:● Structures● Dynamic allocation● Null pointers
![Page 59: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/59.jpg)
And now... linked lists!
![Page 60: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/60.jpg)
Linked List Cells
● A linked list is a chain of cells.● Each cell contains two pieces of
information:● Some piece of data that is stored in the
sequence, and● A link to the next cell in the list.
● We can traverse the list by starting at the first cell and repeatedly following its link.
![Page 61: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/61.jpg)
Representing a Cell
● For simplicity, let's assume we're building a linked list of strings.
● We can represent a cell in the linked list as a structure:
struct Cell {
string value;
Cell* next;
};
● The structure is defined recursively!
![Page 62: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/62.jpg)
Building Linked Lists
![Page 63: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/63.jpg)
Traversing a Linked List
● Once we have a linked list, we can traverse it by following the links one at a time.for (Cell* ptr = list; ptr != NULL; ptr = ptr->next) {
/* … use ptr … */
}
1 2 4list 3
![Page 64: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/64.jpg)
Traversing a Linked List
● Once we have a linked list, we can traverse it by following the links one at a time.for (Cell* ptr = list; ptr != NULL; ptr = ptr->next) {
/* … use ptr … */
}
1 2 4list 3
ptr
![Page 65: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/65.jpg)
Traversing a Linked List
● Once we have a linked list, we can traverse it by following the links one at a time.for (Cell* ptr = list; ptr != NULL; ptr = ptr->next) {
/* … use ptr … */
}
1 2 4list 3
ptr
![Page 66: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/66.jpg)
Traversing a Linked List
● Once we have a linked list, we can traverse it by following the links one at a time.for (Cell* ptr = list; ptr != NULL; ptr = ptr->next) {
/* … use ptr … */
}
1 2 4list 3
ptr
![Page 67: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/67.jpg)
Traversing a Linked List
● Once we have a linked list, we can traverse it by following the links one at a time.for (Cell* ptr = list; ptr != NULL; ptr = ptr->next) {
/* … use ptr … */
}
1 2 4list 3
ptr
![Page 68: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/68.jpg)
Traversing a Linked List
● Once we have a linked list, we can traverse it by following the links one at a time.for (Cell* ptr = list; ptr != NULL; ptr = ptr->next) {
/* … use ptr … */
}
1 2 4list 3
ptr
![Page 69: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/69.jpg)
Traversing a Linked List
● Once we have a linked list, we can traverse it by following the links one at a time.for (Cell* ptr = list; ptr != NULL; ptr = ptr->next) {
/* … use ptr … */
}
1 2 4list 3
ptr
![Page 70: Balanced and Restorative Justice for Juveniles - Youth and Family](https://reader031.fdocuments.in/reader031/viewer/2022021209/62063e1b8c2f7b173005c822/html5/thumbnails/70.jpg)
Once More With Recursion
● Linked lists are defined recursively, and we can traverse them using recursion!
void recursiveTraverse(Cell* list) {
if (list == NULL) return;
/* … do something with list … */
recursiveTraverse(list->next);
}