﻿1
00:00:00,450 --> 00:00:05,520
‫So in this video, we're going to walk through a function called print list.

2
00:00:05,790 --> 00:00:11,820
‫And what print list does is print out all of the values from a linked list.

3
00:00:12,240 --> 00:00:14,530
‫So we'll start the function out like this.

4
00:00:14,550 --> 00:00:21,200
‫It's called print list, and we'll create a variable called temp that we set equal to head, which will

5
00:00:21,210 --> 00:00:24,870
‫point temp at the first node in the linked list.

6
00:00:25,200 --> 00:00:29,250
‫And then we're going to create a while loop, we'll say while temp.

7
00:00:29,490 --> 00:00:35,730
‫So this conditional in the wild loop is going to be true as long as temp is pointing at a node.

8
00:00:35,970 --> 00:00:42,900
‫This is the equivalent of saying while temp is not equal to null pointer, you could write this.

9
00:00:42,900 --> 00:00:45,780
‫Either way it would mean the same thing.

10
00:00:46,230 --> 00:00:52,830
‫So I'm going to just leave this as wild temp and we'll say C out temp value.

11
00:00:52,890 --> 00:00:57,360
‫So that's going to be the value of the node that temp is pointing to.

12
00:00:57,540 --> 00:01:04,110
‫So if you have a variable that is pointing to a node, you have access to the attributes and that node

13
00:01:04,110 --> 00:01:07,680
‫and the attributes are value and next.

14
00:01:08,130 --> 00:01:12,900
‫So this line in this iteration is going to print out 11.

15
00:01:13,730 --> 00:01:20,600
‫And then we'll say temp equals temp next, that is temp next.

16
00:01:20,900 --> 00:01:23,720
‫And that moves temp to the next node.

17
00:01:24,110 --> 00:01:30,680
‫And then when we run through the while loop again, that will print out the three and then the 23 and

18
00:01:30,680 --> 00:01:31,580
‫the seven.

19
00:01:32,030 --> 00:01:38,540
‫The next iteration will cause temp to be equal to null pointer, which is going to break us out of this

20
00:01:38,540 --> 00:01:39,380
‫while loop.

21
00:01:39,950 --> 00:01:45,320
‫So now let's flip over and take a look at this code in code.

22
00:01:46,080 --> 00:01:51,810
‫So in this file we have all of the code that we have written so far for creating length lists.

23
00:01:52,140 --> 00:01:57,990
‫We're always going to have these two lines at the top of any file that we're going to do in this course.

24
00:01:58,350 --> 00:02:01,500
‫And then this is our node class here.

25
00:02:02,130 --> 00:02:10,830
‫And then I'm going to scroll up and here is our linked list class here with our class variables of head,

26
00:02:10,830 --> 00:02:12,450
‫tail and length.

27
00:02:12,870 --> 00:02:14,880
‫So I've set these to be private.

28
00:02:15,150 --> 00:02:20,190
‫And as I have mentioned before, classes are by default private.

29
00:02:20,400 --> 00:02:22,680
‫So you don't need to have this line here.

30
00:02:22,680 --> 00:02:26,970
‫But I like to do this because I think it makes it more readable.

31
00:02:27,360 --> 00:02:34,680
‫And then down here we have our constructor and the print list function that we just created.

32
00:02:35,190 --> 00:02:40,020
‫But I'm going to scroll up and show three other functions that I've created.

33
00:02:40,700 --> 00:02:45,500
‫Called Get head, get tail and get length.

34
00:02:45,890 --> 00:02:53,720
‫Now, the way people normally do these three functions is they will just in the body say return head

35
00:02:53,720 --> 00:02:56,390
‫or return tail or return length.

36
00:02:56,930 --> 00:03:03,080
‫I like to do it this way with a sea out statement because if I'm going to do a get head to see what's

37
00:03:03,080 --> 00:03:09,440
‫going on with my length list, I'm probably going to put it in a sea out statement anyway.

38
00:03:09,770 --> 00:03:13,940
‫So I like to just create it right here in the function.

39
00:03:14,730 --> 00:03:16,590
‫So I'm going to scroll up again.

40
00:03:17,830 --> 00:03:20,710
‫And this is where we have our main function.

41
00:03:21,040 --> 00:03:27,790
‫This line here is what we saw at the end of the last video where we created a new linked list with a

42
00:03:27,790 --> 00:03:29,770
‫node with a value of four.

43
00:03:30,280 --> 00:03:36,010
‫But now with these four functions, we have a way to take a look at that length list.

44
00:03:36,640 --> 00:03:38,170
‫So I'm going to run this.

45
00:03:38,960 --> 00:03:44,390
‫And you can see that head points to a node with a value of four and tail does as well.

46
00:03:44,420 --> 00:03:49,370
‫The length is equal to one and this is where we ran print list.

47
00:03:49,370 --> 00:03:55,700
‫And since we only have one node in the linked list, it just prints out the value from that one node.

48
00:03:56,330 --> 00:04:01,370
‫So I'm going to create something similar to this for other data structures as well.

49
00:04:01,370 --> 00:04:07,550
‫So for example, I'll have print stack or print queue for when we get to those data structures.

50
00:04:07,880 --> 00:04:12,980
‫But when we get to those data structures, I'm not going to do what I did here and walk through the

51
00:04:12,980 --> 00:04:15,230
‫details of how I created it.

52
00:04:15,560 --> 00:04:17,780
‫But I just want you to understand that.

53
00:04:17,780 --> 00:04:24,290
‫Say, for example, when we get to Stacks and I do a print stack, that is not something that's built

54
00:04:24,290 --> 00:04:31,310
‫into C++, that is something that I built, and then I'll make it available to you in the code, in

55
00:04:31,310 --> 00:04:32,330
‫the resources.

56
00:04:32,450 --> 00:04:37,400
‫But for now, that is our function for print list.

