1 00:00:00,850 --> 00:00:09,580 Let's add a simple main function without parameters here and we're going to print provide a width. 2 00:00:10,330 --> 00:00:19,360 Basic input output using the include input output stream here and we're going to use the string in this 3 00:00:19,360 --> 00:00:22,060 practical practical example here. 4 00:00:22,060 --> 00:00:24,910 So include string here. 5 00:00:26,030 --> 00:00:37,270 So here above the main function, add a definition for the structure that represents a task in the list 6 00:00:37,280 --> 00:00:37,790 here. 7 00:00:38,870 --> 00:00:42,330 Task and task pointer here. 8 00:00:42,370 --> 00:00:46,670 P next and string description. 9 00:00:49,020 --> 00:00:49,350 Here. 10 00:00:50,370 --> 00:00:53,090 So this has two members. 11 00:00:53,100 --> 00:00:55,920 The guts of the object is the description. 12 00:00:56,880 --> 00:00:57,570 Item here. 13 00:00:57,570 --> 00:00:59,190 So in our example. 14 00:01:00,270 --> 00:01:07,260 Executing a task will involve the print printing, the description item to the console. 15 00:01:07,260 --> 00:01:14,100 So in an actual project, we most likely will have many items associated with the tasks and you may 16 00:01:14,100 --> 00:01:17,490 even have member functions to execute the task. 17 00:01:17,490 --> 00:01:26,010 But we have not covered the member functions in this course, which is the topic of another next lecture 18 00:01:26,010 --> 00:01:26,550 here. 19 00:01:26,550 --> 00:01:32,790 So the plumbing of the linked list is the other member P next here. 20 00:01:32,790 --> 00:01:40,560 So the task structure has not been completely defined at the point that the P next member is declared. 21 00:01:40,560 --> 00:01:45,060 So this is not a problem because the P next here is a pointer. 22 00:01:45,600 --> 00:01:53,970 So you cannot have a data member of any undefined or a partially defined type because the compiler will 23 00:01:53,970 --> 00:01:56,730 not know how much memory to allocate for it. 24 00:01:56,730 --> 00:02:03,130 So you can have a pointer member to a partial defined type because a pointer member is the same size 25 00:02:03,130 --> 00:02:06,430 irrespective of what it points to. 26 00:02:06,430 --> 00:02:12,190 So if we know the first link in the list, then we can access the whole list. 27 00:02:12,190 --> 00:02:16,510 And in our example this will be a global variable. 28 00:02:16,510 --> 00:02:19,570 So when constructing the list here. 29 00:02:20,540 --> 00:02:28,990 The construction functions need to know the end of the list so they can attach a new link to the list. 30 00:02:29,000 --> 00:02:30,470 Again, for convenience. 31 00:02:30,470 --> 00:02:37,640 We will make this global variable here and the following pointers after the definition here. 32 00:02:39,750 --> 00:02:40,110 Here. 33 00:02:41,250 --> 00:02:46,650 The following pointers after the definition of the task pointer like that. 34 00:02:46,650 --> 00:02:49,700 Here it and nullptr. 35 00:02:50,930 --> 00:02:53,840 VTR and task here. 36 00:02:57,530 --> 00:02:58,970 He current? 37 00:02:59,870 --> 00:03:01,080 No, Peter. 38 00:03:01,730 --> 00:03:06,500 So as it stands here, if we execute this. 39 00:03:07,280 --> 00:03:11,060 This code does nothing, but it's a good opportunity to compile the file. 40 00:03:11,090 --> 00:03:15,980 To test that, there are no errors in our code here. 41 00:03:16,100 --> 00:03:19,520 So let's add the task object to the list here. 42 00:03:19,520 --> 00: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 43 00:03:30,920 --> 00:03:31,610 the task list. 44 00:03:31,610 --> 00:03:39,680 So this needs to create a new task object and initialize it appropriately and then add it to the list 45 00:03:39,680 --> 00:03:43,790 by adding the last link in the list to point to a new link. 46 00:03:43,790 --> 00:03:51,740 So above the main function here, add this new task here void queue. 47 00:03:53,270 --> 00:03:55,400 Task and const. 48 00:03:56,390 --> 00:03:58,070 Spring name. 49 00:04:00,380 --> 00:04:00,650 Here. 50 00:04:01,890 --> 00:04:11,160 So the parameter here is const this const the parameter is const because we will not change the parameter 51 00:04:11,160 --> 00:04:15,720 and we do not want to overheat of a copy being made. 52 00:04:15,990 --> 00: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. 53 00:04:22,620 --> 00:04:29,310 As you know, we make this ampersand before the variables to make them references. 54 00:04:29,310 --> 00:04:37,380 So the first thing in this function, the function this function must do is create a new link. 55 00:04:37,380 --> 00:04:44,010 So we're going to add the task pointer task P task. 56 00:04:45,070 --> 00:04:47,050 Task T task. 57 00:04:48,730 --> 00:04:49,180 Here. 58 00:04:49,840 --> 00:04:50,800 New task. 59 00:04:53,170 --> 00:04:55,090 And task. 60 00:04:56,010 --> 00:04:57,090 This operator. 61 00:04:57,090 --> 00:04:59,700 You know it from the previous lectures. 62 00:05:00,600 --> 00:05:02,880 Description and name. 63 00:05:05,010 --> 00:05:06,330 P task. 64 00:05:07,200 --> 00:05:11,250 P next year and we can assign it to nullptr. 65 00:05:11,550 --> 00:05:19,680 So the first line creates a new link on the free store and following lines to initialize it. 66 00:05:19,680 --> 00:05:24,000 So this is not necessarily the best way of initializing such an object. 67 00:05:24,000 --> 00:05:31,410 And a better mechanism is a constructor, so the constructors will be covered in next lectures here. 68 00:05:31,410 --> 00:05:37,700 So notice that the P next year P next item is initialized to nullptr. 69 00:05:37,710 --> 00:05:42,420 So this indicates that the link will be at the end of the list. 70 00:05:42,420 --> 00:05:46,950 So the final part of this function adds the link to the list. 71 00:05:46,950 --> 00:05:50,520 So this is it makes the link the last in the list. 72 00:05:50,520 --> 00:05:57,900 So however, if the list is empty, it means that the this link is also the first link in the list. 73 00:05:57,900 --> 00:06:03,360 So the code must perform both actions here and we're going to add. 74 00:06:04,820 --> 00:06:05,570 Here. 75 00:06:09,350 --> 00:06:10,220 If. 76 00:06:12,460 --> 00:06:12,910 Here. 77 00:06:17,920 --> 00:06:18,760 If. 78 00:06:19,030 --> 00:06:20,640 Nullptr. 79 00:06:21,890 --> 00:06:22,760 He had. 80 00:06:24,540 --> 00:06:27,060 Then he had. 81 00:06:28,110 --> 00:06:29,010 He task. 82 00:06:30,680 --> 00:06:31,820 He task. 83 00:06:33,970 --> 00:06:34,270 Oops. 84 00:06:38,610 --> 00:06:39,090 Okay. 85 00:06:42,880 --> 00:06:49,570 T hat is p task and then p current. 86 00:06:50,310 --> 00:06:51,210 He test? 87 00:06:53,600 --> 00:06:54,350 Else. 88 00:06:56,700 --> 00:06:59,700 Here the current. 89 00:07:02,500 --> 00:07:06,640 He next and it equals to p task. 90 00:07:08,190 --> 00:07:09,360 The current. 91 00:07:11,920 --> 00:07:13,780 The task here. 92 00:07:13,780 --> 00:07:20,250 So the first line of the code checks to see if the list is empty. 93 00:07:20,260 --> 00:07:28,270 So if the if the P hit is null, this it means that the there are no other links. 94 00:07:28,840 --> 00:07:31,990 And so the current link is the first link. 95 00:07:31,990 --> 00:07:39,550 So and so both P hit and the P current are initialized to the new link pointer. 96 00:07:39,550 --> 00:07:44,230 So if there are existing links in the string, the links has to be added to the last link. 97 00:07:44,230 --> 00: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 98 00:07:53,710 --> 00:07:54,850 the second line. 99 00:07:54,850 --> 00:07:58,810 Initialize the p current with the new link pointer. 100 00:07:58,810 --> 00: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 101 00:08:06,760 --> 00:08:11,720 list by calling this in the main function. 102 00:08:11,720 --> 00:08:16,160 So in this example we will queue the tasks to a wallpaper room. 103 00:08:16,160 --> 00:08:23,570 So this involves removing the old wallpaper, filling any holes in the wall, sizing the wall and then 104 00:08:23,600 --> 00:08:25,850 hanging the passage wallpapers to the wall. 105 00:08:25,850 --> 00:08:31,760 So you have to do these tasks in this order so you cannot change the order. 106 00:08:31,940 --> 00:08:36,050 So these tasks are ideal for a link list. 107 00:08:36,050 --> 00:08:39,890 So in the main function we're going to use a queue task here. 108 00:08:40,490 --> 00:08:44,930 As you know, we made this void function in this here. 109 00:08:45,740 --> 00:08:54,560 We're going to use the queue task and remove old wallpaper for example, and then we're going to use 110 00:08:54,560 --> 00:08:55,340 it again. 111 00:08:55,610 --> 00:08:57,500 Fill holes. 112 00:08:59,010 --> 00:09:02,330 Here and size walls. 113 00:09:03,700 --> 00:09:10,270 And to task the hang new hang new wallpapers. 114 00:09:11,230 --> 00:09:16,270 So after the last line, the list has been created. 115 00:09:17,150 --> 00: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 116 00:09:23,570 --> 00:09:29,930 simply by following the next member from one list to the next so you can compile the code. 117 00:09:29,930 --> 00:09:32,600 But there is no output as you see here. 118 00:09:32,630 --> 00:09:39,500 Worse, as the code center is a memory leak, so the program has no code to delete the memory occupied 119 00:09:39,500 --> 00:09:40,910 by the task object. 120 00:09:40,910 --> 00:09:45,110 So on the free store by the new operator here. 121 00:09:45,110 --> 00:09:53,390 So in next lecture we're going to create that delete the task list command in C plus plus. 122 00:09:53,390 --> 00:09:55,670 So I'm waiting you in next lecture.