WEBVTT

00:00.850 --> 00:09.580
Let's add a simple main function without parameters here and we're going to print provide a width.

00:10.330 --> 00:19.360
Basic input output using the include input output stream here and we're going to use the string in this

00:19.360 --> 00:22.060
practical practical example here.

00:22.060 --> 00:24.910
So include string here.

00:26.030 --> 00:37.270
So here above the main function, add a definition for the structure that represents a task in the list

00:37.280 --> 00:37.790
here.

00:38.870 --> 00:42.330
Task and task pointer here.

00:42.370 --> 00:46.670
P next and string description.

00:49.020 --> 00:49.350
Here.

00:50.370 --> 00:53.090
So this has two members.

00:53.100 --> 00:55.920
The guts of the object is the description.

00:56.880 --> 00:57.570
Item here.

00:57.570 --> 00:59.190
So in our example.

01:00.270 --> 01:07.260
Executing a task will involve the print printing, the description item to the console.

01:07.260 --> 01:14.100
So in an actual project, we most likely will have many items associated with the tasks and you may

01:14.100 --> 01:17.490
even have member functions to execute the task.

01:17.490 --> 01:26.010
But we have not covered the member functions in this course, which is the topic of another next lecture

01:26.010 --> 01:26.550
here.

01:26.550 --> 01:32.790
So the plumbing of the linked list is the other member P next here.

01:32.790 --> 01:40.560
So the task structure has not been completely defined at the point that the P next member is declared.

01:40.560 --> 01:45.060
So this is not a problem because the P next here is a pointer.

01:45.600 --> 01:53.970
So you cannot have a data member of any undefined or a partially defined type because the compiler will

01:53.970 --> 01:56.730
not know how much memory to allocate for it.

01:56.730 --> 02:03.130
So you can have a pointer member to a partial defined type because a pointer member is the same size

02:03.130 --> 02:06.430
irrespective of what it points to.

02:06.430 --> 02:12.190
So if we know the first link in the list, then we can access the whole list.

02:12.190 --> 02:16.510
And in our example this will be a global variable.

02:16.510 --> 02:19.570
So when constructing the list here.

02:20.540 --> 02:28.990
The construction functions need to know the end of the list so they can attach a new link to the list.

02:29.000 --> 02:30.470
Again, for convenience.

02:30.470 --> 02:37.640
We will make this global variable here and the following pointers after the definition here.

02:39.750 --> 02:40.110
Here.

02:41.250 --> 02:46.650
The following pointers after the definition of the task pointer like that.

02:46.650 --> 02:49.700
Here it and nullptr.

02:50.930 --> 02:53.840
VTR and task here.

02:57.530 --> 02:58.970
He current?

02:59.870 --> 03:01.080
No, Peter.

03:01.730 --> 03:06.500
So as it stands here, if we execute this.

03:07.280 --> 03:11.060
This code does nothing, but it's a good opportunity to compile the file.

03:11.090 --> 03:15.980
To test that, there are no errors in our code here.

03:16.100 --> 03:19.520
So let's add the task object to the list here.

03:19.520 --> 03:30.920
So in this project, the next thing to do is to provide the code, is to add, uh, add a new task to

03:30.920 --> 03:31.610
the task list.

03:31.610 --> 03:39.680
So this needs to create a new task object and initialize it appropriately and then add it to the list

03:39.680 --> 03:43.790
by adding the last link in the list to point to a new link.

03:43.790 --> 03:51.740
So above the main function here, add this new task here void queue.

03:53.270 --> 03:55.400
Task and const.

03:56.390 --> 03:58.070
Spring name.

04:00.380 --> 04:00.650
Here.

04:01.890 --> 04:11.160
So the parameter here is const this const the parameter is const because we will not change the parameter

04:11.160 --> 04:15.720
and we do not want to overheat of a copy being made.

04:15.990 --> 04:22.620
So by the way, I want to mention that this is not the not just a const variable, this is const reference.

04:22.620 --> 04:29.310
As you know, we make this ampersand before the variables to make them references.

04:29.310 --> 04:37.380
So the first thing in this function, the function this function must do is create a new link.

04:37.380 --> 04:44.010
So we're going to add the task pointer task P task.

04:45.070 --> 04:47.050
Task T task.

04:48.730 --> 04:49.180
Here.

04:49.840 --> 04:50.800
New task.

04:53.170 --> 04:55.090
And task.

04:56.010 --> 04:57.090
This operator.

04:57.090 --> 04:59.700
You know it from the previous lectures.

05:00.600 --> 05:02.880
Description and name.

05:05.010 --> 05:06.330
P task.

05:07.200 --> 05:11.250
P next year and we can assign it to nullptr.

05:11.550 --> 05:19.680
So the first line creates a new link on the free store and following lines to initialize it.

05:19.680 --> 05:24.000
So this is not necessarily the best way of initializing such an object.

05:24.000 --> 05:31.410
And a better mechanism is a constructor, so the constructors will be covered in next lectures here.

05:31.410 --> 05:37.700
So notice that the P next year P next item is initialized to nullptr.

05:37.710 --> 05:42.420
So this indicates that the link will be at the end of the list.

05:42.420 --> 05:46.950
So the final part of this function adds the link to the list.

05:46.950 --> 05:50.520
So this is it makes the link the last in the list.

05:50.520 --> 05:57.900
So however, if the list is empty, it means that the this link is also the first link in the list.

05:57.900 --> 06:03.360
So the code must perform both actions here and we're going to add.

06:04.820 --> 06:05.570
Here.

06:09.350 --> 06:10.220
If.

06:12.460 --> 06:12.910
Here.

06:17.920 --> 06:18.760
If.

06:19.030 --> 06:20.640
Nullptr.

06:21.890 --> 06:22.760
He had.

06:24.540 --> 06:27.060
Then he had.

06:28.110 --> 06:29.010
He task.

06:30.680 --> 06:31.820
He task.

06:33.970 --> 06:34.270
Oops.

06:38.610 --> 06:39.090
Okay.

06:42.880 --> 06:49.570
T hat is p task and then p current.

06:50.310 --> 06:51.210
He test?

06:53.600 --> 06:54.350
Else.

06:56.700 --> 06:59.700
Here the current.

07:02.500 --> 07:06.640
He next and it equals to p task.

07:08.190 --> 07:09.360
The current.

07:11.920 --> 07:13.780
The task here.

07:13.780 --> 07:20.250
So the first line of the code checks to see if the list is empty.

07:20.260 --> 07:28.270
So if the if the P hit is null, this it means that the there are no other links.

07:28.840 --> 07:31.990
And so the current link is the first link.

07:31.990 --> 07:39.550
So and so both P hit and the P current are initialized to the new link pointer.

07:39.550 --> 07:44.230
So if there are existing links in the string, the links has to be added to the last link.

07:44.230 --> 07:53.710
So in the s class here, the first line, the first line makes the last link point to the new link and

07:53.710 --> 07:54.850
the second line.

07:54.850 --> 07:58.810
Initialize the p current with the new link pointer.

07:58.810 --> 08:06.760
Making the new link to the last link for any new insertions to the list so the items are added to the

08:06.760 --> 08:11.720
list by calling this in the main function.

08:11.720 --> 08:16.160
So in this example we will queue the tasks to a wallpaper room.

08:16.160 --> 08:23.570
So this involves removing the old wallpaper, filling any holes in the wall, sizing the wall and then

08:23.600 --> 08:25.850
hanging the passage wallpapers to the wall.

08:25.850 --> 08:31.760
So you have to do these tasks in this order so you cannot change the order.

08:31.940 --> 08:36.050
So these tasks are ideal for a link list.

08:36.050 --> 08:39.890
So in the main function we're going to use a queue task here.

08:40.490 --> 08:44.930
As you know, we made this void function in this here.

08:45.740 --> 08:54.560
We're going to use the queue task and remove old wallpaper for example, and then we're going to use

08:54.560 --> 08:55.340
it again.

08:55.610 --> 08:57.500
Fill holes.

08:59.010 --> 09:02.330
Here and size walls.

09:03.700 --> 09:10.270
And to task the hang new hang new wallpapers.

09:11.230 --> 09:16.270
So after the last line, the list has been created.

09:17.150 --> 09:23.570
So the heat variable points to the first item in the list and you can access any other item in the list

09:23.570 --> 09:29.930
simply by following the next member from one list to the next so you can compile the code.

09:29.930 --> 09:32.600
But there is no output as you see here.

09:32.630 --> 09:39.500
Worse, as the code center is a memory leak, so the program has no code to delete the memory occupied

09:39.500 --> 09:40.910
by the task object.

09:40.910 --> 09:45.110
So on the free store by the new operator here.

09:45.110 --> 09:53.390
So in next lecture we're going to create that delete the task list command in C plus plus.

09:53.390 --> 09:55.670
So I'm waiting you in next lecture.
