![Page 1: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/1.jpg)
Linked ListsPart One
![Page 3: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/3.jpg)
Announcements
● Assignment 4 due right now.● Assignment 5 (Priority Queue) goes out
today and is due on Friday, May 24 at 2:15PM.● This is different from the due date in the
syllabus. You will have two extra days to work on the assignment.
● Build a powerful data structure that you'll harness in the remaining assignments!
![Page 4: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/4.jpg)
Array-Based Allocation
● Our current implementation of Stack uses 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 5: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/5.jpg)
Array-Based Allocation
● Our current implementation of Stack uses 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 6: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/6.jpg)
Array-Based Allocation
● Our current implementation of Stack uses 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
1 2 3 4
![Page 7: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/7.jpg)
Array-Based Allocation
● Our current implementation of Stack uses 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
1 2 3 41
![Page 8: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/8.jpg)
Array-Based Allocation
● Our current implementation of Stack uses 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
1 2 3 41 2
![Page 9: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/9.jpg)
Array-Based Allocation
● Our current implementation of Stack uses 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
1 2 3 41 2 3
![Page 10: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/10.jpg)
Array-Based Allocation
● Our current implementation of Stack uses 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
1 2 3 41 2 3 4
![Page 11: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/11.jpg)
Array-Based Allocation
● Our current implementation of Stack uses 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
1 2 3 41 2 3 4
![Page 12: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/12.jpg)
Array-Based Allocation
● Our current implementation of Stack uses 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 13: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/13.jpg)
Array-Based Allocation
● Our current implementation of Stack uses 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 14: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/14.jpg)
Array-Based Allocation
● Our current implementation of Stack uses 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 15: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/15.jpg)
Array-Based Allocation
● Our current implementation of Stack uses 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 16: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/16.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 17: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/17.jpg)
Excuse Me, Coming Through...
1 2 3 41 2 3 4 5 6 7
![Page 18: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/18.jpg)
Excuse Me, Coming Through...
1 2 3 41 2 3 4 5 6 7
137
![Page 19: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/19.jpg)
Excuse Me, Coming Through...
1 2 3 41 2 3 4 5 6 7 7
137
![Page 20: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/20.jpg)
Excuse Me, Coming Through...
1 2 3 41 2 3 4 5 6 6 7
137
![Page 21: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/21.jpg)
Excuse Me, Coming Through...
1 2 3 41 2 3 4 5 5 6 7
137
![Page 22: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/22.jpg)
Excuse Me, Coming Through...
1 2 3 41 2 3 4 4 5 6 7
137
![Page 23: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/23.jpg)
Excuse Me, Coming Through...
1 2 3 41 2 3 3 4 5 6 7
137
![Page 24: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/24.jpg)
Excuse Me, Coming Through...
1 2 3 41 2 2 3 4 5 6 7
137
![Page 25: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/25.jpg)
Excuse Me, Coming Through...
1 2 3 41 1 2 3 4 5 6 7
137
![Page 26: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/26.jpg)
Excuse Me, Coming Through...
1 2 3 4137 1 2 3 4 5 6 7
![Page 27: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/27.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 28: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/28.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 29: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/29.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 30: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/30.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 31: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/31.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 32: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/32.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 33: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/33.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 34: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/34.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 35: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/35.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 36: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/36.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 37: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/37.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; insertion is efficient in the worst-case.
● Has some tradeoffs; we'll see this later.
![Page 38: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/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: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/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: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/40.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 41: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/41.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 42: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/42.jpg)
A Simple Structure
struct Tribute { string name; int districtNumber;};
Tribute t;t.name = "Katniss Everdeen";t.districtNumber = 12;
![Page 43: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/43.jpg)
A Simple Structure
struct Tribute { string name; int districtNumber;};
Tribute t;t.name = "Katniss Everdeen";t.districtNumber = 12;
![Page 44: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/44.jpg)
A Simple Structure
struct Tribute { string name; int districtNumber;};
Tribute t;t.name = "Katniss Everdeen";t.districtNumber = 12;
![Page 45: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/45.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 usually a stripped-down version of a class:● Purely implementation, no interface.● Primarily used to bundle information
together when no interface is needed.
![Page 46: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/46.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 47: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/47.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 48: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/48.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 49: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/49.jpg)
Dynamic Memory Allocation
struct Tribute { string name; int districtNumber;};
Tribute* t = new Tribute;t->name = "Katniss Everdeen";t->districtNumber = 12;
![Page 50: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/50.jpg)
Dynamic Memory Allocation
struct Tribute { string name; int districtNumber;};
Tribute* t = new Tribute;t->name = "Katniss Everdeen";t->districtNumber = 12;
![Page 51: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/51.jpg)
Dynamic Memory Allocation
struct Tribute { string name; int districtNumber;};
Tribute* t = new Tribute;t->name = "Katniss Everdeen";t->districtNumber = 12;
t
![Page 52: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/52.jpg)
Dynamic Memory Allocation
struct Tribute { string name; int districtNumber;};
Tribute* t = new Tribute;t->name = "Katniss Everdeen";t->districtNumber = 12;
????
name
districtNumbert
![Page 53: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/53.jpg)
Dynamic Memory Allocation
struct Tribute { string name; int districtNumber;};
Tribute* t = new Tribute;t->name = "Katniss Everdeen";t->districtNumber = 12;
????
name
districtNumbert
![Page 54: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/54.jpg)
Dynamic Memory Allocation
struct Tribute { string name; int districtNumber;};
Tribute* t = new Tribute;t->name = "Katniss Everdeen";t->districtNumber = 12;
????
name
districtNumbert
![Page 55: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/55.jpg)
Dynamic Memory Allocation
struct Tribute { string name; int districtNumber;};
Tribute* t = new Tribute;t->name = "Katniss Everdeen";t->districtNumber = 12;
????
name
districtNumbert
![Page 56: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/56.jpg)
Dynamic Memory Allocation
struct Tribute { string name; int districtNumber;};
Tribute* t = new Tribute;t->name = "Katniss Everdeen";t->districtNumber = 12;
????
name
districtNumbert
Because t is a pointer to a Tribute, not an actual
Tribute, we have to use the arrow operator to access the
fields pointed at by t.
Because t is a pointer to a Tribute, not an actual
Tribute, we have to use the arrow operator to access the
fields pointed at by t.
![Page 57: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/57.jpg)
Dynamic Memory Allocation
struct Tribute { string name; int districtNumber;};
Tribute* t = new Tribute;t->name = "Katniss Everdeen";t->districtNumber = 12;
????
name
districtNumbert
![Page 58: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/58.jpg)
Dynamic Memory Allocation
struct Tribute { string name; int districtNumber;};
Tribute* t = new Tribute;t->name = "Katniss Everdeen";t->districtNumber = 12;
Katniss Everdeen
????
name
districtNumbert
![Page 59: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/59.jpg)
Dynamic Memory Allocation
struct Tribute { string name; int districtNumber;};
Tribute* t = new Tribute;t->name = "Katniss Everdeen";t->districtNumber = 12;
Katniss Everdeen
????
name
districtNumbert
![Page 60: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/60.jpg)
Dynamic Memory Allocation
struct Tribute { string name; int districtNumber;};
Tribute* t = new Tribute;t->name = "Katniss Everdeen";t->districtNumber = 12;
Katniss Everdeen
12
name
districtNumbert
![Page 61: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/61.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 62: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/62.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 63: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/63.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 64: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/64.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 65: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/65.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 66: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/66.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 67: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/67.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 default to a garbage value.
![Page 68: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/68.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 69: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/69.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 70: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/70.jpg)
And now... linked lists!
![Page 71: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/71.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 72: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/72.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;
/* ? */ next;
};
The structure is defined recursively!
![Page 73: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/73.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 74: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/74.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 75: Linked Lists - Stanford Universityweb.stanford.edu/class/archive/cs/cs106b/cs106b.1136/...Linked Lists at a Glance A linked list is a data structure for storing a sequence of elements](https://reader033.vdocuments.site/reader033/viewer/2022053008/5f0be09d7e708231d432a80c/html5/thumbnails/75.jpg)
Building Linked Lists