Full Transcript
https://www.youtube.com/watch?v=N6dOwBde7-M
[00:00] hey what's going on everybody it's you
[00:02] hey what's going on everybody it's you bro hope you're doing well
[00:03] bro hope you're doing well in this video we're going to discuss
[00:05] in this video we're going to discuss linked lists and computer science
[00:06] linked lists and computer science so sit back relax and enjoy the show
[00:11] now before we dive straight into linked
[00:13] now before we dive straight into linked lists we're going to take a closer
[00:14] lists we're going to take a closer examination of arrays and array lists we
[00:16] examination of arrays and array lists we will see what disadvantages that these
[00:19] will see what disadvantages that these data structures have
[00:20] data structures have where linked lists excel at so we'll
[00:22] where linked lists excel at so we'll compare and contrast the differences
[00:24] compare and contrast the differences between the two
[00:25] between the two with what we understand with arrays and
[00:26] with what we understand with arrays and array lists these data structures store
[00:29] array lists these data structures store elements in contiguous memory locations
[00:31] elements in contiguous memory locations in this demonstration i'm storing
[00:33] in this demonstration i'm storing letters of the alphabet suppose that the
[00:35] letters of the alphabet suppose that the first element of my array has a memory
[00:37] first element of my array has a memory address of
[00:38] address of one two three fake street obviously
[00:40] one two three fake street obviously these are not real memory addresses but
[00:41] these are not real memory addresses but this is how i like to think about things
[00:43] this is how i like to think about things if this was a memory address then the
[00:45] if this was a memory address then the next element in my array may have an
[00:47] next element in my array may have an address of
[00:48] address of one two five fake street then one two
[00:51] one two five fake street then one two seven fake street
[00:52] seven fake street one two nine fake street and then you
[00:53] one two nine fake street and then you just continue on in that pattern
[00:55] just continue on in that pattern now arrays are fantastic at randomly
[00:57] now arrays are fantastic at randomly accessing elements because they have an
[00:58] accessing elements because they have an index
[00:59] index but they're not so great at inserting or
[01:01] but they're not so great at inserting or deleting elements especially when those elements are closer to the beginning of the array.
[01:04] Here's an example.
[01:05] Suppose I need to insert a new element at index 3.
[01:09] Since this element is already occupied with a value, I would need to shift my elements to the right in order to accommodate room for this new element.
[01:15] So the process of shifting is cumbersome.
[01:19] But once this element is empty, then I can insert a new value.
[01:21] So it's not that big of a deal if I have a small data set.
[01:24] But imagine if I had 1 million elements, I will need to shift my data up to that many times depending on the location of the insertion.
[01:33] And the same concept applies with deletion as well.
[01:34] We would shift our elements to the left to close the gap.
[01:39] You're probably thinking, dude, why are you talking about arrays in a video about linked lists?
[01:44] Well, where arrays have difficulty inserting and deleting, linked lists actually have the advantage.
[01:49] Here's a representation of a linked list.
[01:51] A linked list is made up of a long chain of nodes.
[01:53] Each node contains two parts: some data that we need to store and an address to the next node in line, also referred to as a pointer.
[02:02] also referred to as a pointer linked lists do not have an index the same way
[02:03] lists do not have an index the same way that arrays do
[02:04] that arrays do but each node contains an address to
[02:06] but each node contains an address to where the next node is located
[02:08] where the next node is located so these nodes are non-contiguous they
[02:10] so these nodes are non-contiguous they can really be anywhere within your
[02:12] can really be anywhere within your computer's memory
[02:13] computer's memory if our initial node has a memory address
[02:15] if our initial node has a memory address of one two three fake street
[02:17] of one two three fake street like our array example then the next
[02:19] like our array example then the next node in our linked list could have a
[02:21] node in our linked list could have a memory address of maybe
[02:23] memory address of maybe 101 help boulevard and another could be
[02:26] 101 help boulevard and another could be 404
[02:26] 404 nowhere lane then 666 crime circle
[02:30] nowhere lane then 666 crime circle each node knows where the next node
[02:31] each node knows where the next node resides i imagine this as if we're
[02:33] resides i imagine this as if we're following a scavenger hunt or
[02:35] following a scavenger hunt or a series of clues to find the end of the
[02:37] a series of clues to find the end of the linked list
[02:38] linked list the tale each node has an address a clue
[02:41] the tale each node has an address a clue as to where the next note is we begin at
[02:43] as to where the next note is we begin at the head and work our way
[02:45] the head and work our way towards the tail following each clue
[02:47] towards the tail following each clue each memory address found in each node
[02:49] each memory address found in each node then we know when we reach the end of
[02:51] then we know when we reach the end of our linked list when we check that
[02:53] our linked list when we check that address
[02:53] address our pointer and it has a value of null
[02:56] our pointer and it has a value of null that means we're at the tail we're at
[02:57] that means we're at the tail we're at the end of our linked list
[02:58] the end of our linked list inserting a node is easy in a linked
[03:00] inserting a node is easy in a linked list there's no shifting of elements
[03:01] list there's no shifting of elements involved wherever we need to place a new
[03:04] involved wherever we need to place a new node we take the address stored in the node.
[03:06] we take the address stored in the previous node.
[03:07] previous node and assign the address of our new node.
[03:09] and assign the address of our new node with the address from the previous node.
[03:12] with the address from the previous node so that our new node is pointing to the next node in line.
[03:14] so that our new node is pointing to the next node in line.
[03:15] next node in line then we can take and replace the address.
[03:17] then we can take and replace the address in the previous node.
[03:19] in the previous node with an address that points to our new node.
[03:20] with an address that points to our new node it's as simple as that and we're completing our chain.
[03:22] node it's as simple as that and we're completing our chain.
[03:23] completing our chain simply by inserting a node at a given location.
[03:25] simply by inserting a node at a given location there's only a few steps involved.
[03:27] location there's only a few steps involved no shifting of elements required.
[03:29] involved no shifting of elements required deleting nodes are easy too.
[03:31] required deleting nodes are easy too wherever we need to delete a node we have the previous node.
[03:32] wherever we need to delete a node we have the previous node point instead to the next node in line.
[03:34] have the previous node point instead to the next node in line.
[03:36] point instead to the next node in line again no shifting of elements is necessary.
[03:37] again no shifting of elements is necessary.
[03:38] necessary now this is where linked lists tend to be inferior to arrays.
[03:40] now this is where linked lists tend to be inferior to arrays.
[03:41] be inferior to arrays they are bad at searching we can randomly access an element of an array.
[03:43] they are bad at searching we can randomly access an element of an array.
[03:45] randomly access an element of an array because we have an index with a linked list that is not the case.
[03:46] because we have an index with a linked list that is not the case.
[03:48] index with a linked list that is not the case to locate an element we need to begin at the head.
[03:50] case to locate an element we need to begin at the head.
[03:51] begin at the head and work our way towards the tail until we find the element that we are looking for.
[03:53] and work our way towards the tail until we find the element that we are looking for.
[03:55] we find the element that we are looking for.
[03:55] for this itself takes time in fact it would take linear time but making the insertion or deletion of a node is constant.
[03:57] this itself takes time in fact it would take linear time but making the insertion or deletion of a node is constant.
[03:59] take linear time but making the insertion or deletion of a node is constant.
[04:00] insertion or deletion of a node is constant this variation of a linked list is a singly linked list.
[04:02] or deletion of a node is constant this variation of a linked list is a singly linked list.
[04:03] variation of a linked list is a singly linked list there are single.
[04:06] is a singly linked list there are single links to each node.
[04:08] links to each node however there's another variation called.
[04:10] however there's another variation called a doubly linked list.
[04:11] a doubly linked list a doubly linked list requires even more.
[04:13] a doubly linked list requires even more memory to store two addresses in each.
[04:16] memory to store two addresses in each node not just one which is the case with.
[04:18] node not just one which is the case with a.
[04:18] a singly linked list one address for the.
[04:20] singly linked list one address for the next node and another for the previous.
[04:22] next node and another for the previous node in our chain.
[04:23] node in our chain the benefit of a doubly linked list is.
[04:25] the benefit of a doubly linked list is that we can traverse our doubly linked.
[04:27] that we can traverse our doubly linked list.
[04:28] list from head to tail or from tail to head.
[04:30] from head to tail or from tail to head in reverse.
[04:31] in reverse each node knows where the next and.
[04:33] each node knows where the next and previous note is.
[04:34] previous note is but the downside is that a doubly linked.
[04:37] but the downside is that a doubly linked list uses even more memory than a singly.
[04:39] list uses even more memory than a singly linked list.
[04:40] linked list so how about we create a linked list in.
[04:42] so how about we create a linked list in real life now let's do it.
[04:43] real life now let's do it all right ladies and gentlemen with all.
[04:45] all right ladies and gentlemen with all that out of the way let's create a.
[04:46] that out of the way let's create a linked list.
[04:47] linked list linked list list the data type of the.
[04:50] linked list list the data type of the objects we'll be storing within this.
[04:51] objects we'll be storing within this linked list.
[04:52] linked list just strings because they're easy and i.
[04:54] just strings because they're easy and i will name this linked list.
[04:55] will name this linked list linked list linked list equals new.
[04:58] linked list linked list equals new linked.
[04:59] linked list and list the data type again we are.
[05:01] list and list the data type again we are storing strings at a constructor boom.
[05:03] storing strings at a constructor boom you got yourself a linked list.
[05:04] you got yourself a linked list now if i was to take my cursor and hover.
[05:06] Now if I was to take my cursor and hover over my linked list declaration.
[05:08] Over my linked list declaration, there's a note here that says this is a doubly linked list.
[05:11] There's a note here that says this is a doubly linked list.
[05:12] Each node knows where the previous and next nodes are.
[05:14] List, each node knows where the previous and next nodes are.
[05:15] And next nodes are.
[05:17] Now if we head to the linked list class itself, there's a few things I need to mention here.
[05:19] List class itself, there's a few things I need to mention here.
[05:20] Need to mention here.
[05:22] Our linked list stores the memory location of our first and last nodes.
[05:25] Location of our first and last nodes.
[05:27] These are effectively the head and the tail of our linked list.
[05:28] Tail of our linked list.
[05:30] And there's also an inner class named node.
[05:31] Node.
[05:34] Each node knows the memory address of the next and previous nodes within this linked list.
[05:36] Previous nodes within this linked list.
[05:38] Now taking a look at our linked list class definition.
[05:40] Class definition.
[05:43] Our linked list class implements the deck interface, and a deck is more or less a double ended queue.
[05:45] Is more or less a double ended queue.
[05:48] So with the deck interface, we implement 12 additional methods.
[05:50] 12 additional methods.
[05:52] So here's just a few of them.
[05:53] So we can add to the head, add to the tail, remove the head, remove the tail.
[05:55] Add to the tail, remove the head, remove the tail.
[05:56] The tail, peek at the head, peek at the tail.
[05:58] Peek at the head, peek at the tail.
[06:00] Some will throw exceptions, some will return a special value.
[06:01] Special value.
[06:03] So you can use any combination of these really.
[06:05] Really.
[06:05] And not only do we have these 12 methods, but we can treat our linked list.
[06:07] methods but we can treat our linked list as either a stack or a queue we can push
[06:09] as either a stack or a queue we can push we can pop we can pull then we can offer
[06:11] we can pop we can pull then we can offer so just to demonstrate let's first treat
[06:13] so just to demonstrate let's first treat our linked list as a stack
[06:15] our linked list as a stack so linked lists do have a push method as
[06:18] so linked lists do have a push method as well if we need to
[06:19] well if we need to push an element onto our linked list as if it were a stack
[06:21] push an element onto our linked list as if it were a stack
[06:23] if it were a stack so let's push the letter a and then i will display my linked list
[06:26] will display my linked list with a print line statement
[06:28] with a print line statement system.out.printline linked list
[06:30] system.out.printline linked list and of course we have the letter a so
[06:32] and of course we have the letter a so let's push another letter onto our stack
[06:34] let's push another letter onto our stack what about b
[06:35] what about b so at the bottom of our linked list we have a and then on top we have b
[06:39] have a and then on top we have b let's add a couple more letters let's represent a typical grading scale we
[06:43] represent a typical grading scale we have c d and f a b c
[06:47] c d and f a b c d f notice that i'm intentionally leaving out e we're going to insert that later
[06:52] later so within our linked list that's behaving as a stack
[06:55] behaving as a stack we have f on top then d c b and a
[06:59] we have f on top then d c b and a so we also have access to a pop method as well
[07:02] as well linked list dot pop and this will pop the top of my linked list so f should no
[07:08] top of my linked list so f should no longer be here it's longer be here it's d c b and a so we can treat a linked list as a stack
[07:13] list as a stack we can also treat it as a queue as well
[07:16] we can also treat it as a queue as well and just to save some time i'm going to copy these lines of code
[07:19] copy these lines of code to add an element to a queue we do not use push we use offer so linkedlist.offer
[07:26] offer so linkedlist.offer and we will keep the order so i'm not going to pop it quite yet
[07:29] and we will keep the order so i'm not going to pop it quite yet so we have a b c d f a is at the head f is at the tail and to remove the head of our q we do not use pop we use pull
[07:40] of our q we do not use pop we use pull and a is no longer in here we have b c f
[07:43] so you can use a linked list to mimic a stack or a queue
[07:47] stack or a queue before we move on to the next section i'm going to get rid of this pull method
[07:49] i'm going to get rid of this pull method so we have a typical grading scale a b c d f where linked lists are really good
[07:57] is the insertion and deletion of nodes let's say for this example i need to add a node between d and f that contains the letter e
[08:02] so that's really easy to do with the linked list
[08:06] list we would type the name of our linked list dot add
[08:09] list dot add list and index like for and our object
[08:13] list and index like for and our object e and then to remove a node we would type the name of our linked list
[08:17] dot remove then list the object e
[08:20] so e should no longer be within my linked list
[08:22] so where linked lists tend to have an advantage over arrays and array lists is the insertion and deletion of nodes
[08:30] however there's one catch to this
[08:32] with a linked list we still need to traverse the entire linked list to find where we need to go
[08:37] unlike with arrays and array lists there's no random access to a linked list
[08:41] searching for an element is fairly straightforward too
[08:43] so within a print line statement i'm going to use the index of method of a linked list
[08:50] linkedlist.index of let's look for f so that would be at index four
[08:55] and before we wrap things up here here's a few methods related to linked lists that you might be interested in
[09:00] we can peek at the head or the tail node of our linked list
[09:06] so within a print line statement i'm going to print linked list
[09:09] going to print linked list dot and then use the peak first method.
[09:13] dot and then use the peak first method so the first node within my linked list.
[09:16] so the first node within my linked list contains the letter.
[09:17] contains the letter a so we can peek last as well linked.
[09:20] a so we can peek last as well linked list.
[09:20] list dot peak last and the last node of my.
[09:24] dot peak last and the last node of my linked list contains the letter f.
[09:26] linked list contains the letter f we can add new nodes at the head or the.
[09:28] we can add new nodes at the head or the tail of our linked list by using the.
[09:30] tail of our linked list by using the add first method for the head so maybe i.
[09:34] add first method for the head so maybe i need to add.
[09:35] need to add maybe zero because i don't really know.
[09:37] maybe zero because i don't really know what comes before a in the alphabet so.
[09:39] what comes before a in the alphabet so zero would be a good bet i guess or we.
[09:41] zero would be a good bet i guess or we could add to the tail by using.
[09:43] could add to the tail by using add last and after.
[09:46] add last and after f comes g and we now have.
[09:50] f comes g and we now have g at the tail of our linked list we can.
[09:53] g at the tail of our linked list we can remove first and remove last.
[09:55] remove first and remove last you can also store them within a.
[09:56] you can also store them within a variable two let's say string.
[09:58] variable two let's say string first equals linked list dot.
[10:01] first equals linked list dot remove first then to remove the last.
[10:04] remove first then to remove the last node.
[10:06] node we could just use remove last then.
[10:10] we could just use remove last then and let's store this within a different
[10:12] and let's store this within a different variable remove
[10:13] variable remove last so yeah those are a few useful
[10:17] last so yeah those are a few useful methods related to linked lists
[10:18] methods related to linked lists in conclusion everybody a linked list is
[10:21] in conclusion everybody a linked list is a data structure that stores a series of
[10:23] a data structure that stores a series of nodes
[10:24] nodes each node contains two parts some data
[10:26] each node contains two parts some data and an address
[10:27] and an address nodes are stored in non-consecutive
[10:30] nodes are stored in non-consecutive memory locations
[10:31] memory locations each node can be really anywhere within
[10:33] each node can be really anywhere within your computer's memory
[10:34] your computer's memory and elements are linked via these
[10:36] and elements are linked via these pointers they contain an address for
[10:38] pointers they contain an address for where the next
[10:39] where the next node is we've discussed two varieties of
[10:42] node is we've discussed two varieties of linked lists a singly linked list
[10:44] linked lists a singly linked list as well as a doubly linked list with a
[10:46] as well as a doubly linked list with a singly linked list
[10:47] singly linked list each node is made up of two parts some
[10:50] each node is made up of two parts some data and an address
[10:51] data and an address to traverse a singly linked list we
[10:53] to traverse a singly linked list we would begin at the head node
[10:55] would begin at the head node and use the address as a sort of clue to
[10:57] and use the address as a sort of clue to find where the next
[10:58] find where the next node is located within our computer's
[11:00] node is located within our computer's memory with a doubly linked list
[11:03] memory with a doubly linked list each node is made up of three parts some
[11:05] each node is made up of three parts some data and two addresses
[11:07] data and two addresses one address for the next node and
[11:09] one address for the next node and another address for the previous node
[11:10] another address for the previous node and it behaves the same way and to
[11:12] and it behaves the same way and to traverse a doubly linked list
[11:14] traverse a doubly linked list we could begin at the head and work our
[11:15] we could begin at the head and work our way towards the tail or
[11:17] way towards the tail or we could begin at the tail and work our
[11:18] we could begin at the tail and work our way towards the head depending on which
[11:20] way towards the head depending on which way is closer to where we need to be
[11:22] way is closer to where we need to be within our linked list
[11:23] within our linked list what are some of the advantages of a
[11:25] what are some of the advantages of a linked list one they're a dynamic data
[11:27] linked list one they're a dynamic data structure
[11:28] structure they can allocate needed memory while
[11:29] they can allocate needed memory while their program is currently running
[11:31] their program is currently running two insertion and deletion of nodes is
[11:33] two insertion and deletion of nodes is really easy
[11:34] really easy if you're familiar with big-o notation
[11:36] if you're familiar with big-o notation this would be in constant time
[11:38] this would be in constant time there's only a few steps regardless of
[11:39] there's only a few steps regardless of the size of our data set
[11:41] the size of our data set and three there is no to low memory
[11:43] and three there is no to low memory waste what are some disadvantages
[11:46] waste what are some disadvantages one there is greater memory usage
[11:48] one there is greater memory usage because we have to store an additional
[11:50] because we have to store an additional pointer
[11:50] pointer each node also stores the address for
[11:53] each node also stores the address for where the next node is located
[11:55] where the next node is located and even more so with a doubly linked
[11:56] and even more so with a doubly linked list this will use a lot more memory
[11:58] list this will use a lot more memory because we need
[11:59] because we need two addresses for each node now two
[12:02] two addresses for each node now two there's no random axis of elements
[12:04] there's no random axis of elements within a linked list
[12:06] within a linked list to find an element we need to begin at
[12:08] to find an element we need to begin at one end and work our way
[12:09] one end and work our way towards the other end and three
[12:12] towards the other end and three accessing and searching of elements is
[12:14] accessing and searching of elements is more time consuming
[12:15] more time consuming this is done in linear time this is
[12:17] this is done in linear time this is where arrays and array lists have an
[12:19] where arrays and array lists have an advantage
[12:20] advantage since they use indexing they can
[12:22] since they use indexing they can randomly access an element of an array
[12:24] randomly access an element of an array or an array list
[12:25] or an array list with a linked list we have to manually
[12:27] with a linked list we have to manually traverse the entire linked list
[12:29] traverse the entire linked list to get to a particular index since we
[12:32] to get to a particular index since we don't have random access
[12:33] don't have random access now what are some uses of linked lists
[12:35] now what are some uses of linked lists one they could implement stacks or
[12:37] one they could implement stacks or queues if you need a stack or queue for
[12:39] queues if you need a stack or queue for anything you could also use a linked
[12:40] anything you could also use a linked list
[12:41] list two maybe gps navigation so let's say
[12:44] two maybe gps navigation so let's say you have a starting position and a final
[12:46] you have a starting position and a final destination
[12:47] destination each step or stop along the way is kind
[12:49] each step or stop along the way is kind of like a node and if you need to take a
[12:50] of like a node and if you need to take a detour
[12:51] detour you can easily change insert or delete a
[12:53] you can easily change insert or delete a node and recalculate how to get to your
[12:55] node and recalculate how to get to your final destination
[12:57] final destination three what about a music playlist so
[12:59] three what about a music playlist so each song within a playlist might not
[13:01] each song within a playlist might not necessarily be next to each other within
[13:02] necessarily be next to each other within your computer's memory
[13:03] your computer's memory you want your playlist to follow a
[13:05] you want your playlist to follow a certain order of songs
[13:07] certain order of songs so that could be another use of a linked
[13:08] so that could be another use of a linked list so those are linked lists if you
[13:11] list so those are linked lists if you would like
[13:11] would like a copy of all my notes here and my code
[13:13] a copy of all my notes here and my code i will post this to the comment section.
[13:15] i will post this to the comment section down below.
[13:16] down below if you can give this video a thumbs up.
[13:18] if you can give this video a thumbs up drop a random comment down below.
[13:19] drop a random comment down below and well yeah those are linked lists in.
[13:22] and well yeah those are linked lists in computer science.