﻿1
00:00:00,390 --> 00:00:05,700
‫So now we're going to write our append function and append is going to look like this.

2
00:00:05,700 --> 00:00:07,890
‫We're going to create a node.

3
00:00:08,190 --> 00:00:13,290
‫We'll have the last node in the linked list, point to the new node, and then we'll have tail point

4
00:00:13,290 --> 00:00:14,070
‫to the new node.

5
00:00:14,070 --> 00:00:17,220
‫And that adds that into our linked list.

6
00:00:17,580 --> 00:00:21,600
‫That's how we do it if we already have nodes in the linked list.

7
00:00:21,600 --> 00:00:27,540
‫But if we have an empty linked list, we're going to have head and tail point to the new node.

8
00:00:28,050 --> 00:00:35,550
‫So what I recommend that you do here is pause the video and see if you can write the code to do this

9
00:00:35,550 --> 00:00:40,140
‫and then come back and watch the rest of the video where I go over the code.

10
00:00:40,780 --> 00:00:44,200
‫So we're going to start the code out like this.

11
00:00:44,830 --> 00:00:50,130
‫Our return type is void because this function does not return anything.

12
00:00:50,140 --> 00:00:57,880
‫We pass it an integer that we're going to call value, and then we're going to use that value to create

13
00:00:57,880 --> 00:01:00,820
‫a new node like this.

14
00:01:01,300 --> 00:01:06,040
‫So now the first situation will code for is when we have an empty length list.

15
00:01:06,040 --> 00:01:10,090
‫This is where we have head and tail point to the new node.

16
00:01:10,510 --> 00:01:17,080
‫So the first thing we need to do here is test to see is the linked list, in fact empty.

17
00:01:17,590 --> 00:01:23,770
‫So I'm going to say if the length is equal to zero and there are other ways you could test to see if

18
00:01:23,770 --> 00:01:24,790
‫the length list is empty.

19
00:01:24,790 --> 00:01:30,290
‫You could say if head is equal to null pointer or if tail is equal to null pointer.

20
00:01:30,310 --> 00:01:33,370
‫Those are all different ways that you could test to see.

21
00:01:33,370 --> 00:01:35,380
‫Is the linked list empty?

22
00:01:35,800 --> 00:01:43,270
‫And if it is, we'll want to set head equal to new node that will do this and we'll want to set tail

23
00:01:43,270 --> 00:01:46,390
‫equal to new node and that does this.

24
00:01:46,750 --> 00:01:50,740
‫So now let's add this in with the rest of our code like this.

25
00:01:51,070 --> 00:01:57,430
‫Now let's write the code for when we do have items in the length list, we'll say else and let's build

26
00:01:57,430 --> 00:01:59,110
‫out this else statement.

27
00:02:00,000 --> 00:02:03,090
‫We'll say tail next equals new node.

28
00:02:03,090 --> 00:02:04,980
‫That is tail.

29
00:02:05,430 --> 00:02:08,910
‫Next equals new node.

30
00:02:09,510 --> 00:02:17,010
‫And then we'll set tail to be equal to new node that moves this over and adds that node into the length

31
00:02:17,010 --> 00:02:17,700
‫list.

32
00:02:18,060 --> 00:02:21,780
‫So now let's add this code in with the rest of our code.

33
00:02:21,780 --> 00:02:24,760
‫And so far we have created a node.

34
00:02:24,780 --> 00:02:30,360
‫This is for when we have an empty linked list and this is for when we have items in the length list.

35
00:02:30,360 --> 00:02:35,430
‫And now the only thing left to do is to increase the length by one.

36
00:02:35,970 --> 00:02:38,850
‫So that is our entire append method.

37
00:02:38,850 --> 00:02:41,970
‫And we'll look at this in a moment in VZ code.

38
00:02:42,180 --> 00:02:50,070
‫And when we do, we're going to create this linked list with one item in it and we'll print this and

39
00:02:50,070 --> 00:02:56,430
‫then we'll run append and we'll append this second node with the value of two and then we'll print it

40
00:02:56,430 --> 00:02:59,250
‫out again and we should see one and two.

41
00:02:59,670 --> 00:03:02,850
‫So now let's flip over and take a look at this.

42
00:03:03,420 --> 00:03:05,430
‫So there is our append function.

43
00:03:05,430 --> 00:03:10,710
‫They're not going to scroll up and we're going to look at our main function.

44
00:03:11,220 --> 00:03:18,300
‫So this line here is going to create a linked list with one item with a value of one, and then with

45
00:03:18,300 --> 00:03:21,540
‫this line we'll print that out and I'll run this.

46
00:03:22,280 --> 00:03:27,560
‫And you can see up here, our link list has one node with a value of one.

47
00:03:27,980 --> 00:03:31,580
‫So now I'm going to come up here and I'm going to add a line.

48
00:03:31,850 --> 00:03:36,530
‫And this is where we append that second node with a value of two.

49
00:03:36,710 --> 00:03:39,890
‫And then with this line, we'll print this out again.

50
00:03:39,890 --> 00:03:41,420
‫So I'll run this.

51
00:03:42,050 --> 00:03:50,300
‫And now you can see that we have a length list with values of one and two, and that is our function

52
00:03:50,510 --> 00:03:51,710
‫for append.

