‫So now we're going to do doubly linked lists and we'll start out by creating our constructor.
‫So the constructor is going to be very similar to what we saw with a singly linked list, except we're
‫going to be making nodes that also point the other way.
‫So I'm going to put this back as a singly linked list.
‫So am I going to look at that singly linked list node which looked like this?
‫We said that was very similar to an unordered map like this and the constructor looked like this.
‫So for a doubly linked list, the only change that we need to make is adding a second pointer that will
‫point at the previous node, and then we'll create a constructor, we'll pass that a value, and then
‫we'll say, this value equals value.
‫This is the same as we did for a singly linked list, or we're passing the constructor of value.
‫That's what this value is here, and that we set this value to be equal to value and this value.
‫Is the member variable up here.
‫So that's how we actually assign that value to a node and then we'll initialize that next and previous
‫pointer to be equal to null pointer.
‫So now that we've created our node class, let's start creating our doubly linked list class.
‫So I'm going to start out by creating our member variables.
‫I'm going to mark these as private.
‫But this line here is not necessary because everything is private by default.
‫I just like to do this because I think it makes the code more readable.
‫To explicitly put this in here.
‫So just like with singly linked lists, we're going to have head, we're going to have tail and we're
‫going to have length, and then we'll create our constructor.
‫The constructor needs to be public and we'll start the constructor out like this.
‫And let's just focus in on that constructor.
‫So the first thing we'll do with this is create a node.
‫So notice that this is the exact same line of code that we used in the singly linked list class.
‫It's just that we're calling a different node class.
‫So when we pass this a value, instead of getting a singly linked list node, we're getting a doubly
‫linked list node which has two pointers.
‫Then we'll set head to be equal to the new node like this, we'll do the same thing with tail and that
‫will point tail at that new node.
‫And because that doubly linked list has that one item in it, we'll set the length to be equal to one.
‫So let's bring back the rest of our code.
‫That's the entire doubly linked list class so far.
‫And I'll focus back in on the constructor like this.
‫We'll run this constructor and create a new, doubly linked list with something like this.
‫We're calling our new doubly linked list my doubly linked list and passing it a number seven.
‫And when we run this line of code, we're getting all of this.
‫We're getting a doubly linked list node with a value of seven head and tail are pointing at that node
‫and the length is equal to one.
‫So now let's flip over to this code and take a look at this.
‫So there is our node class there for our doubly linked list and I'll scroll up here.
‫And this is all of the code that we just wrote for our doubly linked list with the three member variables
‫up here at the top and then the constructor down here.
‫And just like I did with single link lists I created for member functions called Print List, get head,
‫get tail and get length.
‫These all work exactly the same way as they did in a singly linked list.
‫And I'll scroll up again and we have our main function and this is the line of code that we just looked
‫at that creates a new doubly linked list called My Linked List, and it will be initialized with one
‫node with a value of seven.
‫And then with this line here, we'll print out that linked list with that one item in it.
‫So I'll run this.
‫And you can see here we have a linked list with one node with a value of seven.
‫And that is our doubly linked list constructor.