1
00:00:02,030 --> 00:00:04,760
To find out the position of the selected value.

2
00:00:04,790 --> 00:00:07,070
We need to traverse the list elements.

3
00:00:07,070 --> 00:00:13,670
So once the selected value is matched with the value of the current position, the method just needs

4
00:00:13,670 --> 00:00:15,620
to return its current position index.

5
00:00:15,620 --> 00:00:23,480
So the implementation of the search operation here, we're going to develop our search operation here.

6
00:00:23,480 --> 00:00:31,490
So we are developed the insert, hit the insert tail and insert and get here.

7
00:00:31,490 --> 00:00:35,150
So now we're going to develop our search operation.

8
00:00:35,150 --> 00:00:36,920
So my name is Stephan.

9
00:00:37,160 --> 00:00:40,550
Welcome to another lecture of our Cplusplus course.

10
00:00:40,550 --> 00:00:42,680
And let's get started.

11
00:00:56,720 --> 00:01:00,720
Now we're going to develop our search operation.

12
00:01:00,740 --> 00:01:01,240
Oops.

13
00:01:01,370 --> 00:01:02,170
Not like this.

14
00:01:02,180 --> 00:01:02,810
Here.

15
00:01:03,080 --> 00:01:04,150
Redo here.

16
00:01:04,160 --> 00:01:08,390
Yes, redo and here.

17
00:01:11,000 --> 00:01:13,010
Let's get our delete this.

18
00:01:13,010 --> 00:01:16,280
And after that we're going to develop our search operation.

19
00:01:16,280 --> 00:01:26,930
So under the insert here, we're going to first we will create our template template type name, type,

20
00:01:26,930 --> 00:01:31,910
name T, and after that, here we will.

21
00:01:32,270 --> 00:01:35,450
So this search will return integer type.

22
00:01:35,450 --> 00:01:37,730
So index of our node here.

23
00:01:37,730 --> 00:01:41,570
So now integer linked list T here.

24
00:01:41,570 --> 00:01:46,280
And this is going to be search here, which is going to take t val here.

25
00:01:46,280 --> 00:01:53,420
And as you can see, Android studio is just automatically write it code for us and here.

26
00:01:55,210 --> 00:01:57,070
So firstly, create the condition.

27
00:01:57,070 --> 00:02:01,050
So if our linked list is empty, just return not found here.

28
00:02:01,060 --> 00:02:04,840
So in this case, not found here, the minus one here.

29
00:02:04,840 --> 00:02:08,650
In this case, it's going to be the not.

30
00:02:10,500 --> 00:02:11,040
Font.

31
00:02:14,460 --> 00:02:15,090
Here.

32
00:02:15,240 --> 00:02:23,220
So let's actually make it if my count is equal to zero.

33
00:02:23,820 --> 00:02:24,630
My I.

34
00:02:24,690 --> 00:02:30,240
If I have zero items in list, then return minus one.

35
00:02:30,240 --> 00:02:33,960
So if there is a no items, how can I search it right here?

36
00:02:34,590 --> 00:02:37,530
And after that we will need to count the index.

37
00:02:37,530 --> 00:02:39,000
So we will create an index here.

38
00:02:39,000 --> 00:02:44,010
So integer index, in this case, it's going to firstly start with the zero and after that we're going

39
00:02:44,010 --> 00:02:45,630
to traverse from the head node.

40
00:02:45,630 --> 00:02:54,690
So node T and node hit here and we will create a while loop here.

41
00:02:55,420 --> 00:03:02,420
To traverse until the selected value is matched with the value of the current position.

42
00:03:02,440 --> 00:03:04,720
Here it will look like this.

43
00:03:04,750 --> 00:03:05,470
Here, for example.

44
00:03:05,470 --> 00:03:06,130
This is one.

45
00:03:06,400 --> 00:03:07,030
Let's actually.

46
00:03:07,030 --> 00:03:07,390
Yeah.

47
00:03:07,420 --> 00:03:10,000
For example, this is one five.

48
00:03:11,200 --> 00:03:12,010
Five.

49
00:03:12,010 --> 00:03:14,620
Six, four.

50
00:03:15,850 --> 00:03:17,800
Three nine.

51
00:03:19,420 --> 00:03:21,490
Or, for example, 20.

52
00:03:23,300 --> 00:03:26,690
2029.

53
00:03:27,530 --> 00:03:37,520
Or and to and here, for example, if we are searching for the or for if we are searching for for here

54
00:03:37,790 --> 00:03:39,170
searching.

55
00:03:43,000 --> 00:03:43,990
Search.

56
00:03:44,980 --> 00:03:45,730
For here.

57
00:03:45,730 --> 00:03:48,130
So this while loop will continue here.

58
00:03:48,130 --> 00:03:56,200
If one is is one is equals to four, you know, then we'll continue to five, six, four.

59
00:03:56,200 --> 00:04:01,360
And here we found the for the loop will break here.

60
00:04:02,720 --> 00:04:06,590
Here, so the loop will not continue.

61
00:04:07,510 --> 00:04:11,260
And not look at here since we found the four here.

62
00:04:11,530 --> 00:04:15,910
So let's get started by writing our while loop now.

63
00:04:16,030 --> 00:04:17,020
So.

64
00:04:19,110 --> 00:04:22,170
While our not.

65
00:04:23,120 --> 00:04:23,980
Here.

66
00:04:24,170 --> 00:04:30,170
Node value value is not equals to our this value here.

67
00:04:30,170 --> 00:04:36,140
So is not equals to our value that we're going to get with the we're going to get from the parameter

68
00:04:36,140 --> 00:04:41,420
of our search then here, then until here.

69
00:04:41,420 --> 00:04:43,730
So until this value is not equals to.

70
00:04:45,080 --> 00:04:45,830
Value.

71
00:04:46,250 --> 00:04:48,080
Then keep looping here.

72
00:04:48,080 --> 00:04:48,410
So.

73
00:04:48,410 --> 00:04:50,270
Well, then keep looping.

74
00:04:50,270 --> 00:04:53,390
So we will increase the index by one.

75
00:04:53,390 --> 00:04:54,080
So we will.

76
00:04:54,080 --> 00:04:57,590
So we will look at other next indexes here.

77
00:04:57,590 --> 00:05:01,230
And after that we're going to note here.

78
00:05:01,280 --> 00:05:04,730
Node Node next.

79
00:05:05,270 --> 00:05:06,650
And here.

80
00:05:08,180 --> 00:05:17,600
If our node is null, if our node is null here, then it will return return minus one again.

81
00:05:17,600 --> 00:05:25,580
So if our count is we have nothing in our list, then it will return minus one which is not found.

82
00:05:25,580 --> 00:05:31,340
And if our node is null, it will also return minus one here.

83
00:05:35,630 --> 00:05:42,890
And after that we will just outside the while loop here, we will just return the index.

84
00:05:43,160 --> 00:05:45,740
Return the index here.

85
00:05:46,790 --> 00:05:47,930
So that's it.

86
00:05:48,080 --> 00:05:55,280
So again, since we need to traverse list element, the complexity of this operation here will be here.

87
00:05:55,280 --> 00:05:58,190
The complexity of this operation is going to be zero.

88
00:05:59,740 --> 00:06:02,530
And multiply by n here.

89
00:06:03,350 --> 00:06:06,590
Science and is our number of items that we're going to search.

90
00:06:06,590 --> 00:06:15,050
But however the selected value here, for example, let's create another example again here, for example.

91
00:06:16,200 --> 00:06:20,190
The two of us here, for example.

92
00:06:20,220 --> 00:06:22,110
Two, five.

93
00:06:23,120 --> 00:06:24,170
32.

94
00:06:25,340 --> 00:06:26,330
15.

95
00:06:28,840 --> 00:06:30,190
80 here.

96
00:06:31,900 --> 00:06:35,530
Three of three and nine.

97
00:06:35,560 --> 00:06:44,230
So, for example, for example, if we are searching for two, then we will first find this.

98
00:06:44,230 --> 00:06:50,800
So that is going to be our best case scenarios because it's going to be zero one here.

99
00:06:50,800 --> 00:06:57,610
But in the worst case scenario, for example, we have one, two, three, four, five, six, seven

100
00:06:57,610 --> 00:07:03,340
elements and here we are, select this here, oops, Here, let's select different color.

101
00:07:03,340 --> 00:07:04,420
So we will check this.

102
00:07:04,420 --> 00:07:10,900
Yeah, it's not equal to for example, if we are searching for nine, it's not nine naught nine naught

103
00:07:10,900 --> 00:07:12,940
nine naught nine here and not nine.

104
00:07:12,940 --> 00:07:19,300
And as you can see here, we finally found nine, which is it's going to be our worst case.

105
00:07:19,300 --> 00:07:22,030
So worst case.

106
00:07:23,620 --> 00:07:28,510
This is our worst case scenario in our application.
