 Hello! So this time we are going to talk about searching our list. So searching is a very kind of straightforward concept. We are just going to look through the list for a particular date of value and if that value is in our list, we return true and if it's not in the list, we return false. Okay? So simple on the surface. Let me switch over to my board here and let's plan this out just a little bit. Okay? So I've got a linked list on the board. I've got Bob at the head. He's index zero. He's the first item. I've got the number 42 is the second item in the list. And why not? You can mix and match the types of data that are in a list just like you can with Python. And the tail of the list is what? Jane? Jane is the tail. Okay? So I've got my list and let's work through some scenarios. Now again, we're searching. We search for an item. We are looking for a value of the data. Okay, so how are we going to do that? Well, what I need to do just conceptually is kind of the there's no other way really to do it. I need to kind of go through each node in the list one by one and ask the question. Is the item I'm looking for equal to the item that is stored in the node? If so, yes, I'm there. I have found the thing I was searching for. I'm done. I can return true. I found it. It's there. However, if I go all the way through the list of and I never found it, then it must not be in the list. Return false. Okay, so that's how I'm going to search. There's really no other way to do it with a linked list. Okay, so but how now? Okay, how are we going to accomplish that? That's a little bit trickier. But it's going to involve traversing or walking the linked list. Okay, so how do we walk the list? Well, let's first make sure that we've kind of got our algorithm down. Okay? Now the item we are searching for the item is the value. We're not looking for nodes necessary. We're looking for data inside a node. Okay, so keep that in mind. Now let's think for a minute about our special cases here. Okay. In the special case, what are the special cases? Well, the special case is that you're trying to search an empty list. Well, if the list is empty, it's the thing there. No, the thing you're looking for can't be in an empty list. The list is empty. So maybe we can use that. You know, we can just say if if the list is empty, we return false. Just right off the bat, right? We don't need to search. It's empty. It can't be there. Return false. Okay, how do you know if the list is empty? There's actually a couple of ways, right? If the head is none, if the tail is none, or if the size is equal to zero. Those all refer to an empty list, right? Okay, but the list is an empty. What do we got to do? Else, and I'll just put else here because if empty, return false. Else, our strategy, as I mentioned, is we got to go to each node and check to see if the data in the node matches what I'm looking for. Else, I'm going to call that, I'm going to say walk the list. Okay, we're going to step down our list. Walk the list. And if the item equals the data in the node, if the item equals the data, I have found it, right? If the item equals the data, I have found it. So I can stop. Return return true at that point. The item is there. No need to keep looking. I'm done. Return true. I have searched successful. Okay? If the item isn't in the data, if the item does not equal the data, then what do I do? Well, I don't stop searching. You know, if I'm here looking for Jane, let's say I'm searching for Jane. Bob, is Bob equal to Jane? No. So I go to the next one, right? Else, I'll just make it explicit. Item is not equal to data. Go to the next node, right? Walk our list. So effectively what you're doing is you're kind of starting here, right? And we're going to name this guy. We're going to have a little variable that's going to do this walk and he's going to start here and then are you it? Yes. No. Oh, you're not. Okay. Get rid of it. Move him down here. Are you it? Yes. No. Oh, you're not. No. Okay. Move you down here. Are you it? Yes. Yes, I am. Okay. Return true. Now the question though is how do you know if the data is not in the list and you know the value is not in the list if you get to the end, you get to the tail and you haven't found it, right? It could be that the tail is the item, but if you've gone all the way through and you didn't find it anywhere, it's not there, right? Okay. So finally, if end of list, you return false. Okay. If you reach the end of the list, you return false because it's not there. Okay. So that's like our strategy. That's going to be our algorithm. Now, how are we going to do this thing? The key here is how do you walk this list? Okay. So let's go into that. Let me turn on my code here. This one. Okay. So here's my search method. It's just what we talked about. All right. Now, all we got, what we got to do is we got to walk this list, but you know what? Let's start out with the easy one. If the list is empty, it's not there, right? Let's just handle that special case right up front. It's usually a good idea to handle your special cases right at the beginning. And it may, again, it may not be optimized code. You might be able to get a little more concise, but get it correct first. This is this assignment more than any other assignment you will have this semester. Get it right first, then optimize. If you optimize prematurely on this, you try and use really snazzy code. You're going to shoot yourself in the foot. I guarantee it. Okay. So just be explicit. If the list is empty, how do I know if it's empty? Well, if its size equals zero, that's a key that's empty. The list, an empty list also has its head value as none. Okay. Either one of those is acceptable, right? Notice that when I'm comparing with none, I use the is operator. You use the is operator to compare a value to none, to true, and to false. That's it. Everything else uses equal equal. And in fact, you could use equal equal here, but it's not good practice. It's not, it tells you not to. We use is. Okay. So that takes care of our special case. Now, let's write general code for dealing with list of size one, list of size 138 million. Okay. We got to walk the list. Okay. So we will use a variable to walk the list. Okay. So this variable traditionally is named Kerr. Okay. Kerr is short for current. The current node that you're looking at. And if you're imagining your linked list as a chain link, it's just like laying out on your desk in front of you or your bed or your table. Current is a little poker chip, a quarter, a penny, that you're just sitting over top of the link you're currently inspecting. Okay. But we need a variable to keep track of where in the linked list we are, because we can't use like index math or things like that because, and this is so important, right, we have to use a variable because what the variables hold, they are references to addresses in memory, places in memory. A linked list lives all throughout memory. It's not in a contiguous block. It's everywhere. Okay. So current is going to point to, it's going to hold a memory address of that current node. Okay. Okay. Where do we want to start our search? Well, the right place to start is at the beginning. It's in fact the only place we can start our search because our linked list only goes in one direction. It goes from the head to the tail. You can't start at the tail and go backwards. Okay. Think about why that is. So we're going to start at the head, self.head. Okay. All right. So after this line of code executes, current and self.head will be pointing to the same node. Okay. On our board, just switch over here real quick. Right. On our board. Right. At this point, Kerr and try and draw just square over here. A little bit better. Let's focus up in here. After that line of code executes, Kerr and head are pointed to the same place. That's all we're saying. Okay. That's all we're saying. Right. But make sure you kind of grasp that. Right. All right. Let's go back to the code for a second. All right. Now I got to walk the list. I got to move down the list. Okay. All right. So, I am going to use a while loop for this. All right. I'm going to use a while loop. We use while loops. When we want to iterate until a condition is met. Okay. So our condition here is going to be while Kerr is not none. Okay. While Kerr is not none. Okay. So that's kind of weird. I got to finish out my loop here and then I'll explain what this is. Okay. So let's do our comparison. Right. Forget about the loop for just a second. Kerr is pointing to the first node. How do I check that node's value? Okay. If Kerr.data equals equals the item, the item I'm looking for, the parameter, then I have found it. Return true. It's there. Okay. Again, remember Kerr is a reference to a node. It starts out by referring to the head. Okay. That's it. It's a node. So it has data. Right. Nodes have data. They have a data attribute or excuse me, they have a data variable and they have a next variable. Kerr.data is currently the head and what's its data value. Okay. That's what it's saying. In this case, it would be Bob. Right. So it's Bob equal to the thing you were searching for. If so, great. I'm done. If not, I need to move Kerr down a slot. I'm going to go back to my document. It's a really critical point. Okay. Kerr is here. Now, how do I get Kerr, this variable, to go here to the node containing 42? Well, there is already a reference to the node 42. Who knows where the node 42 is? Bob's node does. Okay. Because Bob's node has a next. Right. This is the next attribute. Right. These guys over here, these links. I've drawn on the board. Those are all nexts. Bob's next has a pointer to 42's node. Okay. Well, what's also pointing to this node? The current is. So with a little magic, Kerr gets Kerr. Okay. Dot next. Okay. How does Python evaluate it? It evaluates the right hand side of the assignment statement first. Kerr dot next is this pointer. Kerr gets this pointer right here. Says, oh, you want me, Mr. Kerr to point there. That's what Kerr gets Kerr dot next does. Oh, now that hurts your brain. Right. But if you can understand that statement, Kerr gets Kerr dot next. You have made a tremendous leap in your understanding of classes and objects and everything else related to programming. Okay. So it's a big concept. Let me get back to the code and write it in code. Okay. Again, take the time to really sit and think and understand this. Okay. So this line of code right here. What is it doing? Kerr is a node. Kerr gets Kerr dot next is what walks one step. Okay. But now we've got it wrapped in a loop. Okay. While Kerr is not none. Okay. Is not is the same as not equal to. Okay. While Kerr is not equal to none. I'll leave it like that because maybe that's more clear and more used to what you're looking for. While Kerr is not equal none. When will Kerr equal none? Okay. Well, it will equal none when what? Which node up here has a next equal to none? Right? Because we're doing Kerr gets Kerr dot next. That's how we keep walking. When you hit the tail, right? Let's say that we've gotten all the way down here. Let's say that we are looking for the value, the item, what? We're searching for, I don't know, Vanna White. We're searching for Vanna White. I don't know why Vanna White, Bob Barker, Vanna White. Somehow it all works. Right? When we get kind of like to the end of our list, Kerr will be here. Okay. But look at the while loop. While Kerr is not equal to none. Is Kerr equal to none? No. Kerr is equal to this node. This node does have, you know, it's a thing. It's there. It's not none. Okay. So what will happen? Well, according to our code, we will do Kerr gets Kerr dot next. What is the next of the tail? This is the tail node. This next is in fact none. Right? Nothing comes after the tail. By definition, nothing in a length list comes after a tail. So the tail's next value is none. So at this point, when I do Kerr gets Kerr dot next, Kerr will be equal to none. Okay. I've effectively walked off the end of the list. Done. I've gone through the whole thing. It's not there. Okay. So I know that when this loop ends, right, when this loop ends, if I get here, the thing is not in the list. It's the loop and it. Okay. If I had found it, I would have returned out of the statement. I would have returned out of our search method, right? And when you return out of a function or a method, no code after the return statement executes, right? So the only way I get here is when this loop ends without finding the thing. Okay. So if I get this far, I know it's not in the list. If I know, and if it's not in the list, I need to return false. Okay. So there is my search method. And it's a doozy, right? It's a doozy. Let me run my test. I'm going to run my code and scroll up. Okay. And here's my search test. I'm getting some passes. I'm getting a lot of good check marks here, right? So I've done it. Cool beans. That was a doozy. Oh, I forgot to get rid of the not implemented error. All right. So again, wrap your head around this. You can actually shorten this, optimize it, make it a little more concise. I'm not going to go there. I don't need to. Understand what we're doing here. Okay. That's the challenge. Understand walking the list. We start at the head, we go down the list, and then we do something. Okay. If we find the thing, we do something. Okay. In general, these lines here, 87, 88, and then this curve dot, it's curve dot next, that's walking the list. We're going to have to walk the list when we do some other things, removing and popping. Okay. Talk about those next time.