﻿1
00:00:00,440 --> 00:00:07,550
‫So now we're going to do doubly linked lists and we'll start out by creating our constructor.

2
00:00:07,760 --> 00:00:13,550
‫So the constructor is going to be very similar to what we saw with a singly linked list, except we're

3
00:00:13,550 --> 00:00:18,590
‫going to be making nodes that also point the other way.

4
00:00:18,800 --> 00:00:21,860
‫So I'm going to put this back as a singly linked list.

5
00:00:21,860 --> 00:00:27,350
‫So am I going to look at that singly linked list node which looked like this?

6
00:00:27,590 --> 00:00:35,120
‫We said that was very similar to an unordered map like this and the constructor looked like this.

7
00:00:35,630 --> 00:00:42,230
‫So for a doubly linked list, the only change that we need to make is adding a second pointer that will

8
00:00:42,230 --> 00:00:50,330
‫point at the previous node, and then we'll create a constructor, we'll pass that a value, and then

9
00:00:50,330 --> 00:00:52,870
‫we'll say, this value equals value.

10
00:00:52,880 --> 00:00:58,460
‫This is the same as we did for a singly linked list, or we're passing the constructor of value.

11
00:00:58,460 --> 00:01:06,260
‫That's what this value is here, and that we set this value to be equal to value and this value.

12
00:01:07,090 --> 00:01:10,030
‫Is the member variable up here.

13
00:01:10,360 --> 00:01:18,070
‫So that's how we actually assign that value to a node and then we'll initialize that next and previous

14
00:01:18,070 --> 00:01:20,770
‫pointer to be equal to null pointer.

15
00:01:21,190 --> 00:01:27,670
‫So now that we've created our node class, let's start creating our doubly linked list class.

16
00:01:28,090 --> 00:01:31,660
‫So I'm going to start out by creating our member variables.

17
00:01:31,690 --> 00:01:33,340
‫I'm going to mark these as private.

18
00:01:33,340 --> 00:01:39,280
‫But this line here is not necessary because everything is private by default.

19
00:01:39,640 --> 00:01:43,930
‫I just like to do this because I think it makes the code more readable.

20
00:01:44,170 --> 00:01:46,930
‫To explicitly put this in here.

21
00:01:47,320 --> 00:01:53,770
‫So just like with singly linked lists, we're going to have head, we're going to have tail and we're

22
00:01:53,770 --> 00:01:57,290
‫going to have length, and then we'll create our constructor.

23
00:01:57,310 --> 00:02:01,960
‫The constructor needs to be public and we'll start the constructor out like this.

24
00:02:02,260 --> 00:02:05,860
‫And let's just focus in on that constructor.

25
00:02:06,220 --> 00:02:09,580
‫So the first thing we'll do with this is create a node.

26
00:02:09,580 --> 00:02:15,910
‫So notice that this is the exact same line of code that we used in the singly linked list class.

27
00:02:16,240 --> 00:02:20,560
‫It's just that we're calling a different node class.

28
00:02:20,860 --> 00:02:27,490
‫So when we pass this a value, instead of getting a singly linked list node, we're getting a doubly

29
00:02:27,490 --> 00:02:29,800
‫linked list node which has two pointers.

30
00:02:30,490 --> 00:02:36,550
‫Then we'll set head to be equal to the new node like this, we'll do the same thing with tail and that

31
00:02:36,550 --> 00:02:38,770
‫will point tail at that new node.

32
00:02:39,010 --> 00:02:46,450
‫And because that doubly linked list has that one item in it, we'll set the length to be equal to one.

33
00:02:47,020 --> 00:02:49,380
‫So let's bring back the rest of our code.

34
00:02:49,390 --> 00:02:53,740
‫That's the entire doubly linked list class so far.

35
00:02:54,250 --> 00:02:57,270
‫And I'll focus back in on the constructor like this.

36
00:02:57,280 --> 00:03:03,280
‫We'll run this constructor and create a new, doubly linked list with something like this.

37
00:03:03,660 --> 00:03:09,880
‫We're calling our new doubly linked list my doubly linked list and passing it a number seven.

38
00:03:10,150 --> 00:03:14,440
‫And when we run this line of code, we're getting all of this.

39
00:03:14,440 --> 00:03:20,920
‫We're getting a doubly linked list node with a value of seven head and tail are pointing at that node

40
00:03:20,920 --> 00:03:23,770
‫and the length is equal to one.

41
00:03:24,190 --> 00:03:28,180
‫So now let's flip over to this code and take a look at this.

42
00:03:28,930 --> 00:03:34,570
‫So there is our node class there for our doubly linked list and I'll scroll up here.

43
00:03:35,310 --> 00:03:41,820
‫And this is all of the code that we just wrote for our doubly linked list with the three member variables

44
00:03:41,820 --> 00:03:45,240
‫up here at the top and then the constructor down here.

45
00:03:45,970 --> 00:03:52,990
‫And just like I did with single link lists I created for member functions called Print List, get head,

46
00:03:52,990 --> 00:03:55,360
‫get tail and get length.

47
00:03:55,600 --> 00:03:59,920
‫These all work exactly the same way as they did in a singly linked list.

48
00:04:00,370 --> 00:04:06,700
‫And I'll scroll up again and we have our main function and this is the line of code that we just looked

49
00:04:06,700 --> 00:04:14,410
‫at that creates a new doubly linked list called My Linked List, and it will be initialized with one

50
00:04:14,410 --> 00:04:16,330
‫node with a value of seven.

51
00:04:16,660 --> 00:04:21,460
‫And then with this line here, we'll print out that linked list with that one item in it.

52
00:04:21,970 --> 00:04:23,350
‫So I'll run this.

53
00:04:24,110 --> 00:04:30,350
‫And you can see here we have a linked list with one node with a value of seven.

54
00:04:31,310 --> 00:04:35,570
‫And that is our doubly linked list constructor.

