1 00:00:00,500 --> 00:00:00,800 Hello. 2 00:00:00,800 --> 00:00:01,940 My name is Stephan. 3 00:00:02,180 --> 00:00:04,480 Welcome to another lecture of our course. 4 00:00:04,490 --> 00:00:10,310 As you remember in previous lecture, we created our get and insert methods. 5 00:00:10,310 --> 00:00:20,780 So now in this lecture, we are going to create our, uh, the search of search and remove methods. 6 00:00:20,780 --> 00:00:22,280 So let's get started. 7 00:00:23,210 --> 00:00:24,890 So here in Main.cpp. 8 00:00:24,890 --> 00:00:28,310 So this is our insert get. 9 00:00:28,310 --> 00:00:31,070 And now we're going to create search method. 10 00:00:31,250 --> 00:00:38,690 So the search method will also iterate each items list until it finds the matched value. 11 00:00:38,960 --> 00:00:47,300 So the return value of this method will be index of list and it will return the minus one if no result 12 00:00:47,300 --> 00:00:47,780 is found. 13 00:00:47,780 --> 00:00:55,340 So the implementation let's actually come to implementation part of our search method. 14 00:00:56,470 --> 00:01:00,340 So now we're going to create the integer. 15 00:01:01,240 --> 00:01:04,750 Integer list search here. 16 00:01:05,500 --> 00:01:07,540 After that, we're going to loop through. 17 00:01:07,570 --> 00:01:13,300 The array elements and we will return the array index if value is found. 18 00:01:13,570 --> 00:01:16,090 So firstly for loop. 19 00:01:16,270 --> 00:01:20,980 For loop integer is assigned integer to zero. 20 00:01:21,040 --> 00:01:28,750 While our integer or while our a is less than my count, then increment E by one. 21 00:01:30,030 --> 00:01:30,960 After that. 22 00:01:31,880 --> 00:01:39,610 If our if our my items my items index is equals. 23 00:01:41,040 --> 00:01:41,730 Duval. 24 00:01:43,910 --> 00:01:45,080 Equals to vol. 25 00:01:46,220 --> 00:01:48,350 Then return. 26 00:01:49,380 --> 00:01:50,040 Re. 27 00:01:51,740 --> 00:01:52,760 Turn here. 28 00:01:52,880 --> 00:01:56,420 So this code here actually lets. 29 00:01:57,760 --> 00:01:59,230 Striped is caught here. 30 00:01:59,230 --> 00:02:02,680 So my items e. 31 00:02:04,230 --> 00:02:06,630 Equals wow or yeah. 32 00:02:06,960 --> 00:02:08,940 So my items here, here. 33 00:02:08,940 --> 00:02:10,470 So this E. 34 00:02:12,780 --> 00:02:14,130 Here is iterator. 35 00:02:14,130 --> 00:02:16,440 So in this case our if my item. 36 00:02:16,440 --> 00:02:20,550 So for example, my item items consists of two oops. 37 00:02:20,730 --> 00:02:21,450 Not like this. 38 00:02:21,450 --> 00:02:21,900 Yeah. 39 00:02:21,900 --> 00:02:25,620 For example, my items consist of uh, here. 40 00:02:26,460 --> 00:02:34,140 Two three, seven, 1299, 88, uh, 45 and etcetera. 41 00:02:34,650 --> 00:02:39,360 So if our searched value is. 42 00:02:40,200 --> 00:02:43,740 While, for example, if our search value is, uh. 43 00:02:43,950 --> 00:02:44,330 Oops. 44 00:02:44,370 --> 00:02:46,110 Why is that like that? 45 00:02:46,710 --> 00:02:47,400 Yeah. 46 00:02:49,910 --> 00:02:59,960 If our searched value is, for example, seven if the E here will iterate through to here. 47 00:02:59,960 --> 00:03:00,680 So. 48 00:03:01,490 --> 00:03:05,270 The two is going to be our seventh item here. 49 00:03:05,270 --> 00:03:11,810 So it will find our element and we'll return the E here. 50 00:03:12,320 --> 00:03:17,840 So let's actually write our LS here, so ls actually, we don't need ls here. 51 00:03:17,840 --> 00:03:18,560 We just. 52 00:03:18,560 --> 00:03:24,740 If this method is not executed, then it means we couldn't find our item and we will return. 53 00:03:25,070 --> 00:03:26,810 Return minus one. 54 00:03:26,990 --> 00:03:27,810 That's it. 55 00:03:27,830 --> 00:03:36,770 As you can guess, the complexity of method of this method will be the zero and n multiply by n, so 56 00:03:36,770 --> 00:03:39,260 for the average and worst case scenarios. 57 00:03:39,590 --> 00:03:45,950 So it will for the average and worst scenarios and it will iterate through all these elements. 58 00:03:45,950 --> 00:03:52,820 However, in the best case it can be 001. 59 00:03:54,160 --> 00:03:57,040 So it is the best case. 60 00:03:58,060 --> 00:03:58,840 Best. 61 00:04:00,700 --> 00:04:03,510 But this is the, uh. 62 00:04:03,580 --> 00:04:06,070 This is the average. 63 00:04:07,060 --> 00:04:08,020 Average. 64 00:04:09,330 --> 00:04:11,070 Or worst case. 65 00:04:13,340 --> 00:04:15,010 This is average or best case this. 66 00:04:15,170 --> 00:04:23,360 So, by the way, why this If you're asking why this is the best case here or one here, the or one, 67 00:04:23,360 --> 00:04:28,040 it can be found if the VAL is found at the first position position. 68 00:04:28,040 --> 00:04:33,260 So this means this in this case, it will be best case for us. 69 00:04:33,260 --> 00:04:41,780 So if we are searching for seven and our element of seven in the first item, so first index of R, 70 00:04:41,780 --> 00:04:49,910 so it the index is zero, then our this means we are on the best case scenario here. 71 00:04:49,970 --> 00:04:53,590 So let's come to actually we also related here. 72 00:04:53,600 --> 00:04:59,270 So we developed our get method insert method search method. 73 00:04:59,270 --> 00:05:06,110 We just need to develop the remove method here so we will not just need to develop the remove method 74 00:05:06,110 --> 00:05:06,590 here. 75 00:05:07,100 --> 00:05:08,450 So let's get started. 76 00:05:08,450 --> 00:05:17,110 So in this remove method, obviously as the name implies, the we will remove an item from list ADT 77 00:05:17,210 --> 00:05:17,750 here. 78 00:05:17,900 --> 00:05:27,920 So the removal method will iterate all list elements and then assign a new array to hold new list element. 79 00:05:27,920 --> 00:05:31,850 So it will skip the select index to the removed item. 80 00:05:31,850 --> 00:05:35,780 So the implementation here, let's actually make the implementation part. 81 00:05:35,780 --> 00:05:39,800 So the search insert, get insert search here. 82 00:05:39,800 --> 00:05:47,210 After the search, we're going to write our remove method here, void list remove here. 83 00:05:47,210 --> 00:05:48,920 We're going to get the index here. 84 00:05:48,920 --> 00:05:49,760 Integer. 85 00:05:51,020 --> 00:05:54,410 Integer index index here. 86 00:05:54,950 --> 00:05:55,780 We get an error here. 87 00:05:55,790 --> 00:05:56,930 Why is that? 88 00:05:56,930 --> 00:06:00,050 Because remove doesn't return anything, so remove is void. 89 00:06:00,050 --> 00:06:01,640 We Yes. 90 00:06:01,640 --> 00:06:02,720 Remove is void. 91 00:06:05,510 --> 00:06:08,060 Each member must be definition. 92 00:06:08,060 --> 00:06:09,140 Yes. 93 00:06:10,010 --> 00:06:12,350 Hat In this case, we're going to index here. 94 00:06:12,350 --> 00:06:16,490 So now we have no errors here. 95 00:06:16,490 --> 00:06:18,710 Any n uh, so firstly. 96 00:06:19,620 --> 00:06:22,950 We will check if the index is out of bounds. 97 00:06:22,980 --> 00:06:33,780 So in this case, index, while if our index is less than zero or index is greater than our my count, 98 00:06:33,810 --> 00:06:36,360 then it will return and do nothing. 99 00:06:37,050 --> 00:06:43,590 After that, we will copy the current array as we did in the insert method. 100 00:06:43,590 --> 00:06:51,360 So we will copy an old array integer old array here my items. 101 00:06:51,390 --> 00:06:59,370 After that we will decrease the array length, so my count decrease array length and we will initialize 102 00:06:59,370 --> 00:07:00,000 a new array. 103 00:07:00,030 --> 00:07:04,890 My items equals new integer and my count. 104 00:07:05,460 --> 00:07:12,630 So after that we will need to fill the new array and remove the selected index. 105 00:07:12,630 --> 00:07:15,350 In order to do that, we will use the for loop. 106 00:07:15,360 --> 00:07:26,250 So for integer for we will create the integer e and integer e and g here, integer e assigned integer 107 00:07:26,250 --> 00:07:27,360 e to zero. 108 00:07:27,480 --> 00:07:31,050 Also g to zero here. 109 00:07:31,830 --> 00:07:36,120 While he is less than my count then. 110 00:07:37,990 --> 00:07:41,560 Increment e by one and increment g by one. 111 00:07:41,560 --> 00:07:42,360 And here. 112 00:07:42,370 --> 00:07:43,180 So. 113 00:07:44,870 --> 00:07:46,910 G equals to zero. 114 00:07:46,910 --> 00:07:55,460 And while E is less than my count, then increment E by one increment G by one. 115 00:07:57,350 --> 00:07:58,100 Here. 116 00:07:58,610 --> 00:07:59,150 Oops. 117 00:07:59,570 --> 00:08:00,050 Yeah. 118 00:08:00,080 --> 00:08:04,640 Increment g by one here. 119 00:08:04,970 --> 00:08:07,520 Now we also need the if statement here. 120 00:08:07,520 --> 00:08:12,350 If index is g, then we will increment g by one. 121 00:08:13,650 --> 00:08:23,750 And after that, my items or my items, my items, the index after that all array. 122 00:08:23,760 --> 00:08:33,390 And here and we will also after that outside the for loop, we specify specifically outside the for 123 00:08:33,390 --> 00:08:36,480 loop because we don't want to delete the array on each loop here. 124 00:08:36,480 --> 00:08:39,120 So that would that would be pointless. 125 00:08:39,120 --> 00:08:40,500 So delete. 126 00:08:41,880 --> 00:08:44,620 Year old array. 127 00:08:45,540 --> 00:08:47,580 So similar to the insert method. 128 00:08:47,580 --> 00:08:53,430 The complexity of the remove method is O or zero. 129 00:08:53,850 --> 00:08:54,190 Yeah. 130 00:08:54,360 --> 00:08:55,620 Or zero here. 131 00:08:56,450 --> 00:08:56,840 And. 132 00:08:59,230 --> 00:09:07,330 So this is the complex of the zero remove method, even if the user removes the first position.