When deleting multiple nodes with the same value from a linked list, you must maintain a 'previous' pointer to track the node before the current one, and after deleting a node, update the current pointer to point to the next node (using current = previous->next) to continue traversing the list. This approach handles all cases including deleting the first node (where previous is null), deleting nodes in the middle, and deleting the last node, while ensuring the list remains properly connected throughout the process.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
COMP(1511|1911) Week 8 Lecture 2
Added:Okay. Hello everyone. I won't get started right now. I just really wanted to go live to check that I'd set everything up properly.
So, I'll just wait a couple of minutes till people join.
Okay.
All right. Hi everybody. Um I hope you can still hear me now. I was just waiting for a few more people to join and um yeah, welcome to yeah the week eight lecture two. So we're going to continue with link list. We're going to do a bit of a recap on deletion and we're going to look at a sort of more real world type of application to use link lists with. So yeah, let me know if at any point I'm screeching or you can't hear me or anything like that. Ah, I can see someone in the chat. Hello. Um, all right. A few announcements. They're the same announcements I made yesterday, but just a reminder, you're going to have your mini uh exams as check offs this week. You need to attend in person. You can get more details on the forum. You can sign up for an in-person lab time if you don't normally have an in-person lab time or you can't make your regular one.
Um, and if you can't make it in person at all for the lab this week, please just email the CS1511 account. Um, and please ask questions in the chat or on the forum if you're not sure what I'm talking about or you're not sure what to do. and the mini exam. You'll be spending an hour in your in-person lab.
You'll be going through um in the exam environment that you'll be using in the final exam. You'll be uh doing four questions depending on how much time you have. Uh but you just have to attempt and submit at least one to get checked off for the lab. And then there's the regular questions uh the regular lab exercises that you'll do for the marks for the lab. So that's just a reminder. Some of you might have already done it today. So I hope if you did it was a really good chance to see the exam environment and to attempt some questions. Okay. So yeah, look into that exam info, ask questions about it if you've got any questions.
Uh the other reminder is tomorrow there's a revision session. It will be on things like pointers, strings, link lists. You can sign up and get more information on the forum.
Um so this is just a reminder and of course I was about to say hands up. Well, I hope uh most of you have started the assignment. I hope you've looked at the assignment 2 video which will really help you get started. Um, don't forget if you run into any problems with it. There's plenty of help sessions. And if you go to help sessions now, they're probably not as busy as they might get later um, closer to the deadline. So, now's a really good time to get started.
Go to help sessions, ask all your questions. You can also ask questions to your tutor and your lab assists in the labs as well.
If you feel you need additional help on top of that, then that's okay as well.
You can organize like a 30 minute one-on-one drop-in session with a tutor.
And to do that, just email the admin cs1511 unsw.edu.au and they will do their best to organize a good time for you to go to something like that.
All right.
So, you get started on CS Duck Life.
That's it for the announcements.
So, for today's lecture, we're going to do a bit of a recap on deletion and free.
And we're going to potentially look at extending one of our deletion functions depending on how we're going for time.
But then we're going to look at an email management system. It's just like a a very simplified one. Um but it's sort of a more real world application where we won't just have standalone integer link lists. We'll have link lists that are fields in other strrus and there'll be more complex data.
So hopefully this will be helpful because these are the types of things you'll have to think about and work with in assignment two. Okay. So we'll do that. We'll we'll start off with some deletion recap and yeah please ask questions in the chat of course.
All right.
So I'm just going to start like recapping the deletion part. So we recapped kind of insertion yesterday.
Seems weird that it was only yesterday.
Um anyway, so let's have a look at this deletion situation.
So we've already done this. I'm just recapping it.
So imagine we're just doing the simplest case of trying to delete the first node in a link list.
Now, whenever we write any code for link lists, we've really got to think about, you know, is the code I'm writing or about to write, is that going to work even if we've got an empty link list with no nodes in it?
But to start with, um, let's assume that we we do have some nodes and we are going to delete something.
Well, what do we want to do if we're deleting this? Well, if I'm trying to delete this first node, uh, I'm just going to change my pen color.
If I'm trying to delete the first node, well, the head of the list is going to change. So, if I'm trying to delete the first node, the head of the list should move to here, and this should be the first node in the list. And we're going to want to free the data from this one.
Okay, so we want to change the head of the list. We want to actually free the the d the the memory we maloclocked, I should say. And we should end up with a link list like this.
So that's what we're trying to do. Now on the next page, just to really hurt your brain, I've got two sort of chunks of code that attempt to do this for this list.
So, what are the problems with these solutions? So, there's two solutions here.
Are either of them good? Do either of them work? Are they both bad? What's going on? I'll give you a moment to think about it.
And you can write in the chat what you think, whether solution one would work, whether solution two would, whether none of them do, what's wrong.
Oh, cool. There's a poll.
Okay, now it looks like Oh, things are changing.
Okay. Well, not everyone's voted, but so far we've got Actually, I shouldn't say, should I? I'll just be quiet. I'll give you about two more minutes.
So yeah, we've got a few comments saying exactly what they think is wrong. Okay, so let's go through it. So if you said, yeah, neither are any neither work, that that's the right answer. So what's wrong with this? Um, let's have a look. Well, the first one, and look, both of these look like they're trying to do something, you know, reasonable here. We do want to free the head. So, head's pointing to this chunk of memory, and we want to delete it. Um, so we do want to free that memory.
That's correct. But unfortunately, if we free it first and then say head equals head next, we're actually reusing this piece of memory that we just freed. I mean, head next is going to come from this field in here and we've already deleted it. We've already freed it. I mean, so once we've freed this head, we can't then go accessing this data inside it, right?
We're going into something that's been freed. And we talked about that yesterday, how that's actually it's potentially will crash your program.
Well, it will on dcc, but if you're not using dcc, you're out in the real world, this can create security vulnerabilities.
So, this one isn't good.
All right. Hopefully that makes sense.
Now, the second one, it's doing this first. It's saying, let's move head.
Head's pointing to here. Let's get it to point to head next. And that's correct in that we do want the head to end up pointing to that node.
But now we're actually freeing the head.
And um this is the head now. This is where the head's pointing. So we're freeing this node.
And then we're returning that node that we've just freed.
Uh and that's not great either. But we've also just ended up breaking this list into parts and we've lost all these nodes. So, this one, if I had to choose which is worse, maybe this one's even worse than that one. But they both look at first glance fairly reasonable, like maybe they're doing something that's going to work.
Hopefully, that makes sense as well. I can't see puzzled faces or smiling faces, so it's hard to know.
All right, what about these two solutions? I'll give you a few minutes to think about this. Do either of these work? Do both of them work?
I'll tell you after these. I won't give you 50 million solutions. These are the last two.
Did I just accidentally end the poll?
I'm sorry if I did. Um, I clicked on something. Maybe I ended it. But what are we saying? I think the the winning vote was solution three.
Okay, let's have a look. So, solution three, uh, they've worked out, hang on, we can't do this with just one pointer to one node. We need, uh, a pointer, you know, a second pointer.
So, it's saying temp equals head, and that's good. That's a very good step.
Then, it's saying, well, we know that we want head to move to the next one. Let's make head equal head next. That's a very good step. And then we're saying let's free temp. Temp is pointing to this node. We don't want it anymore. Oops, that's the wrong one.
So, let's free it.
Free the memory here. Um, and that's great. That does work. We return the head. The head is pointing to something that hasn't been freed. That's all great. And we just make sure if temp is still here, we just never use this. We don't go and access that memory that was just freed.
Uh so that's a good solution that will work.
Now this solution here is on the right track. I guess temp is pointing to here.
But now they free temp. Now, when you free temp, you're freeing the memory that temp is pointing to. So, you're still freeing this node um that head's pointing to. So, then when you say head equals head next, you're trying to access this free node and you're going to crash it with a a free after use. Um, so this one's not as obvious that we're using it after free because when we're calling the free, we're using the variable temp, but it was pointing to that same chunk of memory that head was pointing to. So this is not going to go well.
So this is this is the one.
All right. Hope again hopefully that made sense. Does anyone if you've got questions, please ask. So just to remind you this is our beautiful picture showing it.
We have created a pointer to this node.
We move head then we can free temp. And that's a safe way to delete the first node in a link list.
Okay. And we looked at that the other day and we ran our code and checked that it that it seemed to work. Okay.
All right. the other code we looked at.
Oh, and what I forgot though, gosh, a very important point. Um, when we put this together into a function, so this is deleting the first node in a list and we return the list, the new head of the list. Uh, we have to have a special case for if the list was empty. So, if we have an empty link list, the head will be null. Um, and if we didn't have this code here, we would end up well temp would be null, but we'd end up with this line here where we're trying to say head equals next when head is equal to null. So, if you're ever in a situation where it's going to result in an attempt to do something like this, go and go to null, dreference it, get data out of it.
That's going to crash and you're going to have a null. I keep saying null pointer exception but dreferencering a null pointer.
So like a lot of our link list code, we will have a special case for if the link list is empty. So if head's null, we can't be moving pointers around in the list. Um because there's no nodes, we can't be deleting things. We just return null or return the head. It's the same thing in this case.
All right. So, that was deleting the first node.
And just to remind us of what we were doing, we had this list C where we were writing this, you know, the implementation of the functions and we had a main function over here.
Uh, I inserted a few nodes. We tested it out. Okay. The other thing we looked at was basically freeing the whole list. So whenever we are working with a link list, every time we add something to the list, we're doing a maloc.
Oops.
Now, if we just keep doing malocs and malocs and malocs, eventually we can run out of memory. Uh, maybe I won't suggest that you try it, but imagine you just had a loop that kept maloclocking lots of memory. You'd run out of memory. Um, and look, sometimes that's because maybe you're really trying to use a lot of memory.
Like in a game, you might be trying to do something really fancy and really using up all the memory. But often you might be using memory. You could free it and you haven't. And you you've got memory leaks and you're gradually using more and more memory.
So, what we want to make sure we get used to doing is freeing memory from our linked lists or really anything that we're malocing once we've finished with it. So, if we finished with our list, there's still some nodes in there. We want to go free them all. So, this is what this particular um function is trying to do. And I believe this is the correct one because I did have a incorrect one yesterday to show you. Fingers crossed.
So yeah, we're trying to basically just go from the first node. So if we run through this with a drawing, which I really recommend you do when you're programming, uh we've got current pointing to head.
And we want to keep going because we want to free every single node in the list. Now again, we've got to be careful we don't just free current and say current equals current next because then we're using memory after we've freed it.
Now because we're trying to destroy this whole list, we might as well just keep moving head along because we're just destroying everything as we go. So we're going to say head equals head next, right? Because that's I guess the head of the rest of the list that we haven't freed yet. Then we free current.
And then we've got to again resist typing in current equals current next because that would be using memory after we freed it. So then we say current equals head. And now we've deleted the first node. We just keep repeating it until currents null. So we've got head equals head next. So head will be moved over to there.
Uh we free current and we move current.
I'm changing all my colors to where head is one more time. Head ends up being equal to null. We free current.
Current equals head. Now it equals null.
And this whole loop finishes. So this is how we can free up a whole list.
So you can't just call free on the head and just free the first node. You've got to go through and do it like this.
All right.
So when we ran this the other day, I won't call free list.
When we run it like this, we've added some nodes. We've deleted one, but we we've got memory leaks. Now, unless we were really using a lot of uh memory and doing a lot of malocs, um we're probably not going to realize this without running leak check. So, if I run leak check, I'll see that we did not do all the freeze for all of the malocs. And it tells us, you know, that was line six.
Line six is, yeah, we mlocked nodes when we created them. We haven't freed them all. So, in your assignment stage three, you've got to start thinking about these sorts of things um with memory leaks.
So, if we call the function we just wrote that hopefully works and frees any memory that's left in our list.
And if we um compile it with leak check, we're hoping it doesn't have this thing at the bottom telling us we haven't freed it all, but let's try it.
Yeah, that that's what you're hoping to see if you're testing things with memory leak checking.
All right. Any questions about deleting the first node, freeing the list? How are we feeling about it?
Okay.
Hm.
I might I might skip search and delete for now um and come back to it because I want to get started on the link list application.
Um and when we get up to the deletion part in that, I'll come back to it if that's okay.
All right. So, here's an email management system. Again, it's very simplified. In fact, our emails don't even have any contents. Um but that's okay. Uh so in this this is a little bit similar to the assignment in that you know we've got a number of these files that we've been given. We've got a bunch of prototypes in ah file. We've got to go and implement them in a C file. We've got some files that have mains in them.
And you know we've got to work out what on earth is going on and what we have to do.
And you know like the assignment as well, you can't just launch into coding.
You've really got to think about what what's the code that I've been given, how does it fit together, what do I have to do, etc. So yeah, before you start coding, and this sounds pretty obvious, but you really need to understand the problem.
What is the code doing that you've got?
I've already said this. How does it fit together? How do you compile it and run it? Um, you might want to draw some diagrams if you know you've got some link lists involved. You might want to think about the different test cases, etc. Okay. Now, in this particular problem, uh, I might quit out of here.
Oh, don't worry about what I'm doing right now.
Okay. So yeah, we've got these four files. So I'm just going to open them up.
We've got our email management system.h file.
Uh it's got hash define.
It's defined a few enums um which you would be familiar with enums. There's different types of emails, different priorities, but the main thing is that we've got some strcts that I'll go through in more detail. a strct for an email folder and a strct for an email.
Uh, and then we've got all these prototypes for different functions. So, that's what's in our.h file.
Then over in our C file, this is well, it's hash including theh file. Um, but this is where we've got a whole bunch of stub code or, you know, code that really hasn't been implemented for all of these different functions and they're all just printing out that we haven't done them yet and then exiting.
We're going to go and implement these.
We may not finish them all in this lecture, but we'll see how we go.
Um, and then we've got a file that we can use to test it.
All right.
Now, let's just talk about the the strrus that we've got here. So, in this email system, these are the two important strus. Now, we're not calling things strruct node. We're not calling things, you know, strct node star head. So if we're looking at these strrus, do we see anything that might be the equivalent of a node? And do we see anything that might be the equivalent of like the head of a link list?
How do we work out where our link lists even are in these strcts?
or where they potentially are.
Now, obviously in an assignment, you'd have um more instructions that I've given you so far and some text explaining things, but yeah. So, we've got some good answers in the chat. So, I guess this keyword next might give things away a little bit. Um, so our strruct email has a pointer to another strruct email.
So, the key is if we've got like to have a link list node, it has to have a pointer to another strct of the same type. And the fact we've called it next makes it a little bit easier to work out. Okay, these are our nodes. So, we're going to be having link lists of emails in this.
Now, in our emails or in our nodes, we don't just have an int for our data.
We've got a whole bunch of different data. some strings like sender, subject, size, email type, email priority, everything but the actual email contents, it would seem.
Uh, but that would be easy to put in later.
All right, the strruct folder. Well, this couldn't be a link list. There's no pointer to a strruct folder inside it, but this is basically like a container and emails is basically like the head of the link list.
So this is like the head of the link list and it's pointing to a link list of emails.
Does that make sense? So instead of just having a standalone linked list, we've got a folder and that has one of the fields in the folder is a link list and this is the head of it. Now we need to understand that if we're going to be able to program with this and not just be kind of guessing and triing and erroring, which we don't want to do.
Any questions about that?
Now, having all this extra stuff uh can make it a little bit more confusing, but all the underlying link list logic is really going to be the same as what you've seen when we did just have the list of integers.
All right, so let's keep going.
Now again, like here's some diagrams to help us understand what's going on.
And I forgot I always forget there's no numbum emails. Just ignore this drawing. I've commented out that field in here. So our folder, our strruct folder will have a name and this uh head of an email list. Right? These are the two fields or members it has.
Uh and if we create an empty folder, an empty folder will start off having no emails in it. And you know it'll look something like this. We'll initialize the head of the email list to be null and gradually we'll add emails to it.
So this is what it might look like if we had three emails in our link list.
Again, pretend that's not happening. So, these are of typestruct email. This is of typestruct folder. These are all the same types. They're all linked together.
This is a different type.
All right. Any questions about how this fits together.
Uh we can also have more than one folder. And to start with, these folders are just independent folders. So you can have one folder that has a list of emails in it and another folder that has a different list of emails in it. Um, we'll we'll look at that later. Um, and the other thing is we'll start with this test main. Test main just has some code in it to help us run um, and test some of our functions.
Okay, if we got all those functions working, we could then try and run this other main which sort of lets you add and delete emails. But we're going to start with this test main just to check, can I make a folder? Can I add an email to this email list, etc. So, if I go here, how do I compile this?
Well, I'm going to work with test main.
So, I've got to use that code. I've also got to compile the code from my email management system that has all the different functions in it.
Now, if I do that and I try and run it, it's not going to do much now because test main tries to create a folder and my create folder function just prints out something. It exits the whole program.
So the first thing we're going to do is actually implement create folder.
I'll just move this across a bit.
All right. So all we're trying to do really is, you know, create a folder that's a bit like this.
So, what's the first step going to be when I want to create my folder?
Can I just do something like this and then set the data inside it and then try and return that at the end?
Oh, someone's asking does exit instantly like finish the program. Yes. So, you can call exit from any function in your program and it'll just yeah, exit the whole program. I'm not sure whether you're advised to be doing that in your code all the time, but we've just done it. I've just done it for this.
Uh, yes. So, is this a good idea or a bad idea if it's on the stack?
And where where might we want it instead?
Yeah. So if we put this if we have a local variable like this on the stack and we try to return it one it's not going to compile but even if we return the address of it this will be destroyed when the function finishes. So we're really going to have to maloclock our folder and return a pointer to it which shouldn't really be a surprise.
So we're going to maloclock our folder.
We want a chunk of memory big enough I can't type for a strruct folder.
Okay. Uh so now I've created just this chunk of memory for new folder.
It's beautiful. Looks beautiful. Now in it I might want to go and refer back to myh file to remind myself of what all the fields are. So this one's been commented out. We just have to make sure we set and initialize all the fields in this strct. So when you know whatever part of the code calls this function, it's going to give it a name. So maybe the name is travel or whatever, we've got to go and put that name inside this strct.
Oh, I just wrote yes, thank you. I left out the size of Thank you very much. And I didn't even notice. Um, yes, we've got to find out the size of the strct.
Okay, it's good. I've It's good. I've got people helping me. Um, now we want to set data inside this strct we've just made and it's called folder.
Now the field we're trying to initialize is the name. We've got to make sure we're setting the data for the name because we've got a pointer. We use this uh stab notation.
So we're trying to set this name field that's inside our strct to be the name that we were passed in. So if if you know we travel was passed in we're trying to put this in the strct.
Now, what's wrong with doing this?
What's wrong with saying new folder arrow name equals name?
And it's got nothing to do with link lists.
Yeah, it's a string. So, this field's a string.
Strings are really arrays. You can't just say this array equals this array.
You either have to write a loop or you have to write use stir copy. Yeah, exactly. People screaming in the chat saying string copy.
Okay, so we copy this in. So, it's going to have the name of it, you know, and let's pretend it said travel, not that you can read my writing.
And then the other field we have to worry about is emails. So, we don't want to forget about it just because there's no um argument setting it to something.
If we've got a link list, we often initialize it or we usually initialize it to be null.
So this is like saying the head is null.
It's just that the head of the list is buried in this strct. Then at the end we return the new folder.
Okay. Now, if we've done this correctly, um first of all, it should compile if we've done it correctly.
Second of all, when we run it, we should get the right output. What's the right output going to be?
We don't I haven't set up wonderful auto tests like you have in the assignment where it tells you whether it works or not. We're gonna have to look at the output and work out, okay, so is this what we were supposed to get or not? So, we're trying to create a folder called folder one. We're printing out how many emails are inside it. There's already code written that can go and count how many emails are in our folders for us.
We've already done that. Um, and then it's going to go ahead and print the emails. Again, there's provided code that can print all the emails.
So given that our folder is empty right now, it should really tell us there's no emails in this folder.
Okay, so printing.
Oops.
This looks weird. Folder one.
Yeah, print. I can't even see what's happening. Ah, here we go. I didn't scroll up high enough. Creating folder one. Well, that's just a print. Anyway, number of emails in the empty folder.
Zero.
Printing empty folder one. The folder one names been set. There's no emails in this folder. Okay, it seems to work. Um, the next thing we're going to do is we're actually going to try and add some emails to this. So, we're inserting the emails at the head of the list. So, we're given the sender subject size type priority. We've got to actually create an email node.
We want it to end up at the beginning.
So, we're going to have to connect this new node to the first node in the list.
And then we're going to have to make sure we update this uh which is showing us the first node in the list. It's going to be this new node we've just inserted. Okay. So, we're just adding it to the front of the list.
um adding this to the back of the list, the front of the list. Um yeah, it it and this is really the same logic you would have used with your integer link list, but we've just got all these other things now to think about.
So let's go and try and do that.
Now the first thing I probably want to do is actually create another function called create email maybe. So I might um that does the maloc a bit like create node. So, unfortunately, this is the tedious part when you've got sort of more real world like data.
Instead of just having to worry about an int, uh, we've got all of these different inputs that we're storing.
Okay.
and then gets too wide.
All right. So I'm going to do strct email new email and again I'm going to maloc this. This is like a node. I'm going to remember size of this time.
Now when we do a maloc, I think I can see discussion about how much space do we have on the heap. Yeah, in this course we will assume that we won't run out of anything on the heap. Um unless we've made a mistake.
We're not doing anything that uses that much memory. But theoretically, Maloc could return null to say, "No, I can't give you that much memory." And generally in real life, you want to check for that. I'm not going to check for it in this example. I'm not sure whether you need to check for that in the assignment, but you should always make sure you're reading instructions and understanding whether you need to check for it or not in a course like this.
All right. So, I'm creating my new email. Again, I've done my maloc for my email, but now I've got the tedious thing of copying all these fields into it.
And again, I'm dealing with strings. So when I say new email, okay, new email. Uh let's go to this sender.
I I can't just say sender. I've got to use string copy.
Let me know if I'm doing anything wrong.
I've also got to use string copy for the subject.
I've also Oh, no. That's it.
The next piece of data I'm copying into this node is the size and it is a double. Oh, writing double.
So I can just use equals the enums. So I can just use equals and then I return this new email.
Okay.
Again, this isn't going to do anything because I haven't used this function yet, but I'm just checking I don't have typos.
So, I've got something that can create one of these email nodes.
So, I'm going to actually use it down here.
Oh, now I've got to type all this in again. Might have been easier not to do it.
Now, it's good to have a create node function type priority.
Hm. What would you say about my style here?
I'm going over the line like this line. I don't know if you've got it set up in your editors as well. This is telling me these lines of code are too wide.
Yeah, I might leave that for now. Um, so 1511 style and your tutor might complain. All right, once I do this, I actually want to connect it to the list. So, imagine I've got my email folder.
It's got a list to some emails. Maybe there's some emails in there already.
Maybe there's not.
I've created this new email.
This is my new email.
I need to link it up. So, the first thing I want to do is create this link here that joins it up before the first email we already have.
So, to do that, I'm going to take my new email and I'm basically trying to say, well, the next one after this is the one that's the head of the list. Now, we don't have anything called head. We've got our email folder and inside there we've got our field called emails.
So this field here is called emails.
So if we say new email next equals email folder emails, it's going to connect this up, I think.
Then what do we have to do?
Well, we've got to make sure our folder.
Sorry, I'm getting distracted by the chat. Um, we've got to make sure our folder knows which is the first email, right? So, right now our folder thinks the one that's now the second email is the first email. We've got to fix that.
We've got to say email folder emails.
Well, the first email in this email list is this new email.
Okay. So now that is being updated like this.
So I think this will work. Now a quick question before we go for a break. I mean should I be returning the new head of this list?
If not, why not?
So often if I'm changing the first node in the list or I'm basically in all of our insert and delete functions that we did with link list of integers, we returned the new head of the list. Do I have to do that now? No, that's right.
Well, yeah, the return type is void, so we can't. But is that wrong? Should I have changed it to return the head of this email list?
Yeah, that's right. We don't need to.
It's stored um we've actually stored it in this folder.
So if we've changed the head of the list, this strct folder has that information already. So it's already taken care of. We don't need to return it. Yeah, that's right. All right, let's try and make this or compile it. Does it work?
It seems to. Is it going to work if there's nothing in the email list already? Let's just quickly think through that.
Well, if there's nothing in the email list, emails will be null.
Our new node will say email next equals email folder emails.
It'll also be pointing to null. That's okay. So, If the list is empty, this will be null, but we're assigning it to something.
That's okay.
Um, then we're updating this to point to here. I think it it should be all good.
All right, let's quickly run it, then go for a break. So, in my test again, we're in a situation where we might have to think about what should be in our list.
And when it gets printed out, check whether that is what we're looking for.
So into our folder, we're inserting 1 2 3 4 five emails.
We know we're inserting them all at the head of the folder, folder one. So, we would expecting we'd be expecting the first thing in the folder to be Sophia's email, then Tammy's, then Brianna's, then Tim, then John's. So, we'll have a quick look.
Yep, we've got Sofia and and Tammy, Brianna, Tim, and John. And there's five. So, that looks all good.
Now, we're going to have about I don't know, we'll come back at about 5.
So, when we come back, we'll do we'll search for an email and then we'll start trying to do some trickier stuff with deletion of emails and stuff like that.
So, let's come back at about 5 and we're going to continue with this and we might do a cahoot. We'll do a poll uh as to whether we do a cahoot about halfway Sorry.
Okay, we're just about to come back. Yeah, sorry I didn't explain why there was suddenly a cat on the screen. Um, hey, I do like cats and I thought it was cute.
But yeah, that's my thing for a break. I probably should have explained that first.
Uh so let's get back to it. Um is there any way we can do a quick poll to see if people are understanding how to work with these strcts? Like are they getting confused about having a folder and then having the strruct emails or are people sort of okay on that?
Okay, while that's that's happening.
Okay. Right. There's only 12 votes. Um 14 votes. Most people are yes or somewhat. Okay. Um All right. The next one that we're doing is I'll try and just to make sure we I solidify what's going on when I explain explain the next one. So the next one what we're doing is we're searching for a particular subject. So you know this is like the subject when you send an email.
Um it might be I don't know meeting piano lesson I don't know what it might be. Now to make them easy our subjects are just all like a a bbb ccc but again what we're doing is we're just looking through a folder for a particular subject or particular subjects. So if we're looking for any email that has BBB as the entire subject, we're not just going to do a partial search or anything like that. If there's a entire subject in an email called BBB, we're finding it. So really, we're just doing a search, but the search that we're the the data we're searching on is the subject field. So if we're doing a search, all we're really doing is a traversal.
So I think yeah, I think we can end it.
I might end that. Thank you.
Oh, there's a few not at alls. Okay.
Okay.
So I'll just I'll just go through it again. So we've got um we've got two different kinds of strcts. We've got a strct for a folder and ignore this field here. Every folder has a name uh like you know travel or folder one or whatever we've called it. And every folder has a linked list field. So this field called email is really like the head of a link list but it's in this sort of strruct.
Um then the actual link list itself are these strruct emails. So these strruct emails are like nodes and instead of just storing an int node, we've got all of this data.
So there's lots and bits and pieces of data, senders, subject, size, etc. But that's all the data and they've all got a connection to another node. So these are just like nodes. This emails field inside this other strct is like the head of the link list.
So it's buried inside this folder. So often in our code in link list, we might say head equals null or something like that. Well, we'll have some kind of folder. So imagine this folder is called folder. We would be saying folder arrow cuz we need this field which is called emails equals null. That would be the way we would do it in this circumstance.
So where you'd usually see head is equal to null is initializing the link list to have nothing in it. In this scenario, we would be saying folder arrow emails and making that equal to null. For example, now what we're trying to do here is we're trying to basically do a traversal.
And we've seen traversals before. We often make current point to the first note in the list and we just keep going, you know, maybe until we reach the end.
In this case, if we're searching for something, we're going to stop if we find it. So, imagine we're searching for the subject BBB.
Uh, we want to start looking from the first node. We're going to look at the subject. Is this BBB?
And imagine the subject here is AAA. No.
So, we go to the next one. And to do that, we'd say current equals current.
Next, just like we would in any link list. Uh, then we check, is this the subject we're looking for?
Imagine we're looking Oh, this is just like a like a subject in an email. So when you send an email to somebody, you might your subject might be um party on the weekend or surfing or I don't know what people write in their emails.
Um so maybe I'll use realistic ones. So imagine we're looking for the subject holiday or not holiday subject I don't know uh golf this subject is babysitting.
This subject is piano.
Right? These are email subjects. So imagine you're looking for all of the emails that have the subject golf.
So we want to set current to the first email and we're going to check. Is this one about golf? No, it's about babysitting. We move to the next one. Is this one about golf? Yes, it is. So we're going to stop traversing and we're going to return and say yes, we found the email about golf or whatever it is.
We might get to the end. We might be looking for an email about surfing. We get to the end of the list. It's null.
There was no emails with a subject surfing in it. Okay. So, we're just doing a search.
All right.
So, when we go down here to our code, but in ours, I wasn't very um imaginative. instead of having surfing and piano and golf, my uh subjects were a a bb ccc d e. So I apologize for that.
Uh but we're going to go and write our search function.
Okay.
So, keep in mind we've got our email folder that possibly has a bunch of emails in it.
Okay? And that's what we're searching through.
So the first thing we want to do is basically yeah make a pointer to the first node in the list.
And to do that in this scenario our nodes are called strruct email and we're looking for the first node or the first email I should say in this particular folder.
So in this particular folder that we're given, there's going to be an emails list and this is going to give us the head of it. And we're going to make current point to the head.
And that's how we're doing it in this scenario because this emails is the head of the list. It's inside this email folder.
Okay. So now we're just going to do a traversal. Now reg usually a traversal we just start at the beginning and we go right to the end.
Okay. So we're going to be checking every single node until we get to null potentially.
But for every node, we're just checking well in this current node that I'm looking at.
If I go to the subject field, so remember every single email has a sender and it has a subject.
So in this example, we're looking inside that node to see what subject uh was in the email and we're just searching for the one that uh has been given to this particular function to find. All right. So basically we're checking gosh there's planes and sirens everywhere and stuff over here where I am. We're trying to check whether the subject in the current email in the current node that we're pointing to is equal to the subject we're looking for. Now again, what's wrong with this? If we remember that our subjects are strings, how do we compare two strings?
Yes, string compare. So, we've got to use string compare.
We can't just use equals equals. And we're going to have to make sure that we know what string compare returns. String compare returns zero if the strings are equal because there's no differences. So if we get to a particular node and it matches the subject we're looking for, we we found it and we're going to stop and we're going to return it. We're just going to return the first one that we find.
So I would return that whole node and by returning that whole node the code that's using it can go and access all the other fields. They'll know oh I was looking for that uh email about golf.
I found it and it was sent by Sofia and it was this size and that size etc. Now, if we haven't found it yet, we have to keep going. And we do our old favorite current equals current next.
Oops.
To move our pointer along.
So, current will move to the next node.
And oh, I've just destroyed something.
At the end, if we haven't found it, we'll just return null to indicate no, we never found any node with that particular email subject in it. Okay, so eventually if current gets to the end, we know we didn't find it. We return null.
Yeah, instead of putting the if statement inside the while loop, we could have it as part of the actual condition. So I could say we want to keep searching while current is not equal to null and while this while string compared does not return zero. So while we haven't reached the end and while we haven't found the one we're looking for, keep looking. Keep looking. When we get to the end, we just return current. Now either current will have stopped because it found the particular email it was looking for and you'll return it or current found nothing and will be null.
So either way it'll return the right thing. So this is another way you could do it. Um yeah, exactly. Either way is fine. It depends how your brain works and how many conditions you like for your brain. Anyway, either is fine.
Okay. Now, what wouldn't be fine? Yeah, we definitely need to use string compare for strings.
What wouldn't be fine is if we only went to current next.
What would happen if we had this in our loop instead?
If we had current condition while current next is not equal to null, what would this not work for?
So yeah, if we're traversing and we stop when current next is equal to null, that means we're stopping when when we get to the last one.
Yeah. And that means we wouldn't actually go in and check the last one.
So, if we were searching for party and the last one had party, well, we would stop before we actually went um and checked it. We'd be stopping and stopping the loop right there. As soon as we got there, we wouldn't even get to this line to check if um actually that would still work, but it would always return the last node. Yeah, it's it's going to break.
So, we want to make sure we Yeah, this time we want to go right to the end. We want to check inside that node to know whether it matched or not and keep going if it didn't.
All right.
Now, there's a bunch of tests for that.
I'm just going to assume that that search worked, which might come back and um cause problems, but we'll see cuz I want to get on to the next part where we're deleting stuff and clearing stuff.
So, let's just do clear folders. Now, we looked at this today already. Um, okay. We looked at the code that we wrote to clear just a link list. Sorry for my scrolling. Ah, free list. So, this was just freeing a regular list of integers. Could we use this code to free our list of emails?
And if so, how would we use it?
I'm actually going to copy it over.
So, we already kind of looked at this today.
I'm just going to shove it in here. This is not a good idea.
If you create your own helper functions, make sure you place them somewhere appropriate. Um, I'm just wanting to have it all on the the same screen.
Okay. So we know that this can take a list and free it all. In our application, we don't have nodes. We've got emails instead of nodes.
But other than that, if I passed in, so if I had a folder and I passed in like the head of this link list of emails, would this code be able to go through and free them all for me.
So, I've got my email folder.
We know it's got a field called emails.
And I want to free all of the memory associated with those emails.
Could I just call free list like this and say okay I've got my email folder and emails gives me the head and then that free list is just going to free all of these emails for me. Is this going to work?
Let's try it.
It should potentially work unless I've done the wrong like, you know, made typos. Do do a million things. Now, how am I even going to know if I haven't compiled it correctly? Let's do leak check.
And just to show you what's happening in the test code, we've created just so we know what's happening. Uh we've created one folder.
I'm going to get it rid of this for now with a bunch of emails in it. We've printed them out. We've gone and searched. So, we should be able to find the email with a aaa ddd e. We shouldn't find one with holiday.
Then we're making a second folder and inserting a bunch of emails into that and printing them out. And then we're trying to free it. So, basically at the end we're trying to free folder one and folder two. And honestly, between them all, there should be I don't know, quite a few emails. So, we'll soon find out whether this freed things properly or not.
Oh, it doesn't look like it did.
So, what's it complaining about though?
It says actually the things that it looks like it is complaining about the fact we didn't clear the folder.
So or free the folder. So on line 32 when we wrote this create folder we did a meal.
I've gone and I've tried to free all the emails but I haven't freed the actual folder. So that's all a bit messy.
So So I've freed freed the emails I hope but I haven't freed this folder. I should do that next.
So in clear folder I go call my free list function and then I say okay let's actually free the folder itself because that too is a strct that was maloclocked.
Okay.
And of course, we don't want to free the folder first and then try and traverse the list that you know was inside it. That's going to cause problems.
Generally speaking, you you free the inner contents of things and then the containers afterwards.
So hopefully this will free our folder and hopefully all of the emails are freed as well.
Okay, so it looks like this freed everything properly.
Um, so a lot of the code that we've already written for integers will really directly apply. We've just got to, you know, change it slightly to to fit in in this scenario.
Okay. Does anyone have any questions?
And maybe let's do a poll to see if we want to do the next function in this or do a cahoot.
The next function in this is to search for and delete emails of a particular priority or we can do a cahoot.
Okay.
Okay. We've only got 11 votes, but right now the next function is winning over the Cahoot.
So, I'm going to go with that.
Yeah, it's still winning.
All right. So, let's get on to the next function.
Yeah.
Okay. I'm going to end it and just do the next function.
All right. So, the next function is um delete email of priority. Now, this one's tough. Now, we did something like this before, so let me scroll. Shut your eyes if you don't like watching a lot of scrolling and it's safe to open your eyes now.
Now, we did something like this yesterday. We were searching for integers in our integer link list and we looked at all these cases. If we're searching for something in a list, we may not find it because it might not be in the list. The list might be empty. So that might be why we not might not find it. We could find it at the beginning.
We could find it in the middle. We could find it at the end.
Um, and we're deleting it. So all of these cases are really going to need to make sure they're handled properly. And the thing that we didn't look at was what if there are multiple occurrences we want to delete. So we're going to look at that today.
So, just to remind you of the way I did it. I showed you a different approach, but I didn't code it up. Um, so we use this previous pointer.
Yeah. And somebody's suggesting we just build on what we've got and make sure we keep iterating and move the pointers on to the next one. So, that's what we're going to do. We're going to modify what we did yesterday. Um, and just make sure we're not breaking anything as we do it.
So, the way we did it yesterday, we had this previous pointer and the current pointer because to delete something, we need to make sure we've got a pointer to the previous node. So, the previous always starts at null. And I was saying the other day, you know, the previous node is kind of creeping up behind current who's running away from it. I mean, not really, but that's what I think of. And we're going to keep going. We're searching for 42 in this example. And in this example, we just were searching and deleting one thing, but I'll still explain it. Um, so we're checking is the current node null? No. Is it equal to 42? Cuz imagine we're searching for 42 in this example. No. So we move along.
And to move along, we're going to move previous to where current is. And then we're going to move current along. Then we do it again. Is current equal to null? No. Is current equal to the one we're looking for? No. Let's move previous along. Let's move current along. So, is current equal to null? No.
Is current equal to the one we're looking for? Yes. So, we've now found the one that we want to delete.
And then what we then worked out that we needed to do was connect previous to the one after current.
And to do that, we said, okay, well, previous should equal current next.
All right. And that connects it up. Then basically, we were trying to free this.
So we just call free on current. We're not using current anymore, so that's all fine. And basically, we've deleted it.
And really, we could just keep going because we were only trying to delete one thing. But now, we've got to make sure we change things up a bit. Um, and we keep going in case we're looking for more things.
So, first of all, we're definitely going to have to change our loop.
We don't want to stop when we find the value. we're going to have to have an if statement to check if we found the value because we might we're going to maybe find multiple values that we're going to delete. So, we'll have to change that up.
Um, and we've got to think about what we're going to do. Like, we can't keep saying previous current equals current next and all that sort of stuff if we've just deleted current.
So yeah, imagine our loop is just current is just going to keep going till it gets to null. Now as we go along, we find one we want to delete. We're still going to say previous next equals current next and hook it up like this. We're still going to free current.
But now we have to keep going.
If we want to keep going and keep searching, what do we want to do with previous?
If we've deleted this node here, what do we want to do with previous? And what do we want to do with current? If we say current equals current next, we'll be using this memory after we've freed it.
So, what would we have to do? Cuz we probably want to move current along to here so we can keep searching. How do we do that? Yeah, we've got to say current equals previous next. We've put this link in. We say current equals previous next. And we just leave previous where it is.
And then we can keep going. Current equals previous next. We keep going. And look, we need to delete it again.
Oh, but we would do the same thing. We would say previous next equals current next.
We would delete or free current. And then we'd say current equals previous next again, etc. Okay, so this is the kind of thing we have to do. So before we try and implement it in the email thing, which is a little bit more confusing, maybe we'll just try to extend this version in our integer version right now if that's all right with everyone. Um, and just check that first.
So I'll just get rid of this for now.
So, this was our search and delete.
We're going to try and modify it like we just saw to search and delete all.
Okay. So yeah, we don't want to have this as part of our condition because we need to get all the way through the whole list and check every value cuz we might need to delete lots of things. So this is going to have to be an if statement. We're going to have to check it in here and say basically if this is the value we're looking for, then we've got to do something to delete it.
Otherwise, if it's not, then we just move on as normal.
Okay, so we're traversing through. If this is a value we need to delete, delete it. Otherwise, just move preven current along.
So, how do we delete it? Well, we already had code that we'd thought through that we put down here.
Um, and in this case, we had to check before we did it that we hadn't finished the loop and found nothing. So, we had to check whether current was null or not before we deleted it.
If we're doing this inside the loop, we don't have to do that check for whether current's null.
cuz inside the loop current can't be null. So we can basically steal the code we already wrote and just put it inside this loop.
And this is the code that deletes stuff.
Now this is the main case that we just looked at in the slide where where we've got pre pointing to some node. We've got current pointing to the one we want to delete and we're saying let's make previous next equal current next. So we've done that step and we're saying let's free current.
And then in the slide when we're thinking about it, we decided that after that we say well current equals previous next.
And that's going to move current along to here.
And then we just keep going through the loop.
So that's what we'd worked out for that situation.
Now, what on earth was this thing about?
When would we ever be trying to delete a node and previous is null?
When would that case be? We found a null a node to delete. Previous is null.
Yeah. So we've got we had a special case for if we were deleting the first node.
So if we were trying to delete 42 pre would be equal to null.
And what we did before uh when we were just going to delete one thing, we just said okay well we don't want to say previous next equals anything cuz previous is null. It's the first node so we need to move head along. This is like deleting the first node. We say head equals head next.
And then we do our free So, we freed this, but now we need to keep going.
So, what are we going to do? We can't say current equals current next. We can't say current equals pre next cuz pre's null. Where's current going to go?
Well, we just need to move current to where head is. That's going to be the next note in the list because we just deleted the first one.
So we can say current equals head and we're back to the situation where we're looking through the list and we're at the first node and pre is still null.
So does that look right? If pres null we move headto head next free current and now current is going to keep going from the new head onwards.
If we did this again um cuz we'd go through the loop again we would have the same situation. We would say previous null head equals head next we'd be freeing car and we'd be saying current equals head and pre wouldn't have done anything.
Why wouldn't it be current equals head next? Because we've already moved the head along. So imagine we're trying to delete this node and it's the first node.
We're saying head equals head next. So we're actually moving the head to this node here.
Then we're freeing this node.
So we want current to continue. It hasn't checked this node. Oops. To see what's in it. We want it to continue from where the head is now.
Okay. So yeah, the problem was I'll just go back again. I'll go back again to when we've got Yeah, this is better. So the first thing we do is preview is null. We can't say current. We can't use pre at all. But when we delete this, we want the new head of the list to be this second node. So we've changed head. It's pointing here. Then we free current.
But now we want current to keep going from here on in. And that's now called head.
Yeah.
To not skip a node cuz yeah, we might want to delete that. We're just going to keep going from there. So it's really I don't know how people would write this kind of code without drawing it. And even though I'm a terrible terrible drawer and I'm very messy. Sorry everyone. That's the thing I think that helps me work this stuff out. Having said that, let's see if it actually works. Right now, have I forgotten anything?
I haven't purposely forgotten anything.
So, if it doesn't work, it's just cuz I've made a mistake. Um, and we'll have to try and fix it. I say we you'll have to help me. Now, this is what I've been doing a lot lately.
not saving my files.
Okay. And I've gone for gold. I'm not even in the right directory.
Okay.
Split. Right.
Okay. Okay, so I've got my main um and now the most diabolical case to test this would be just having a whole list with everything have the same value in it and try and delete it. But I won't do the diabolical case first. Let's just add everything.
I'm just going to get rid of some of this extra stuff. Let's just add everything twice.
And instead of calling search and delete, I'll call search and delete all. Is that what I called it?
All right, let me scroll down and find it.
And yeah, this code is starting to get quite tricky, these functions.
Uh, delete main.
Well, let's just see if it works and then we'll do the leak check on it.
Well, it have I just destroyed the whole list somehow.
Okay. All right.
So, it looks like something's gone wrong.
Terribly wrong.
So, I might get rid of some of these tests to find out where it is.
Oh, that was control D where I kept selecting the code. Now, what have I done wrong? I really don't know.
So, just the main case didn't work.
So, I tried to delete one and I wrecked everything.
Oh, does anyone know what it is? Yes.
I copied the condition from the while loop and it should be the opposite condition. So, that's why it was destroying everything in the whole list.
So, it wasn't even the tricky part. It was like, well, we don't know. There could be more wrong with it. But whoever found that, that was good. I was just looking at the same thing. Um, okay. So, we've deleted all of the ones. So, we've got this list. We've deleted all of the ones.
That looks okay. Let's test it more carefully.
Um, what about if we're trying to find something and it's not in there?
Yep, that works fine.
What if we're trying to delete all the threes, which should be the first node and also, you know, repeated.
That works.
Um, if you've got bugs, always work from the first case, like one case at a time that you know has failed. It's too overwhelming to see all the cases that have failed. Just look at the first one that's failed and try and work out why that failed. All right, we've deleted all of the zeros.
We should end up with just a list that has four in it because at the beginning we did just a normal delete of four. It didn't delete all the copies.
All right. So, that seems to have worked.
Let's just check.
Oh, well, the time's nearly up.
if it worked from with with leaks. It seems to have worked with leaks. Now, we don't have much time now. We've got about two seconds. No, a couple of minutes.
Now that we've got the code we want cuz we want to do basically exactly the same thing over here.
But unfortunately we've got the more complicated uh environment. But can we get this same code to work? We've debugged it with simple integer list. Can we get it to work with our folder and our everything else?
Well, we've got about 3 minutes. What could possibly go wrong?
Um, okay.
Pre.
Now, of course, these are all called emails. It's not going to be as simple as just changing the word to email instead of node.
We can still say pre equals null. That's fine.
We can't say current equals head.
There's no head. Hopefully by now we know the head is just in this email folder though. Email folder emails. That's the head. That's going to set our current.
Okay.
Now, we're not searching for a data field in this particular one. Every email has a priority field and we're going to search for that.
Priority is uh an enumerated type. We can use equals equals. So, we're just searching for the correctly named piece of data.
Now, every time we talk about pre or current, that's fine. It's just anytime we talk about head, that's no good.
Anytime we're talking about head in this one, we're talking about the emails list that's in the email folder.
It's a lot harder to read though, isn't it? Um, but other than that, all the logic of what we're doing is identical. We've just got these this extra folder thing that makes it a bit tougher. But because we've got that extra folder, we don't have to return the head at the end. Now, this one's tricky and I've gone through it quite quickly, so don't be beating yourself up if you're confused. That's probably my fault.
Let's try it. We've only got about a minute left.
Uh, test main It compiles.
I think I've got to comment out stuff in the test file.
Sorry again for scrolling.
There we go.
Well, it did something.
Now, it was meant to delete. You can take my word for it. It was meant to take this email list that had Bella, Eby, Holly, Andrew, Grace, Sophie, Liam.
It was meant to delete all the low priority ones. So, we should be left with Sophie, Grace, and Eby.
Again, a lot of scrolling. Sophie, Grace, and I think it worked. I didn't check the leaks though.
It should work. That's what I You know, we always think that and then we do it and it doesn't work. I think that worked. Um yeah, you'll still need still need the mini freeze. So, look, that's all we've got time for, I think, today, right?
What's the time? 56 past. So yeah, we didn't finish everything.
You can go play around and see if you can implement I'm I'm sad we didn't get to merge um merge two folders together and then split two folders apart, but we did the basics of how to work with this extra strruct folder that has the the list inside it. So hopefully that was helpful. Um, yeah. So, we did a recap of deletion. We looked at this sort of more real world application that is more like what you're going to face in assignment two.
And then next week, Henry's back. Um, I'm not sure exactly what he's doing next week, but it'll either be link lists or exam revision or something like that. I'll let let him tell you. All right. Thanks so much everybody. Thanks for participating and asking heaps of good questions, answering heaps of good questions, and enjoy your dinner if you're having some now. I certainly will be.
All right, see you later. Bye-bye.
Thanks. And the code will all be there.
Okay, bye.
Related Videos

TOP 15 Data compression Interview Questions and Answers 2019 Part-2 | Data compression | Wisdom jobs
wisdomjobs
281 views•2019-06-28

CTS 158: 802.11w Management Frame Protection
ClearToSend
4K views•2019-02-04

NDSS 2019 Send Hardest Problems My Way: Probabilistic Path Prioritization for Hybrid Fuzzing
NDSSSymposium
496 views•2019-04-02

How realistic is Cities: Skylines?
CityBeautiful
159K views•2019-02-14

GUIs & TUIs: Choosing a User Interface for Your Python Project | Real Python Podcast
realpython
2K views•2025-04-04

The OSI Model - Explained by Example
hnasr
225K views•2019-05-12

Cloud Computing - Introduction
elithecomputerguy
98K views•2019-10-07

From Traveler's Dilemma to Dynamic Routing | Demystifying Networking
IITBombayJuly
5K views•2019-08-04
Trending

One Must Imagine Sisyphus Happy
vlogbrothers
61K views•2026-07-21

Future of Taylor Farms
maighstirtarot5385
11K views•2026-07-21

The Downfall of OnePlus!
techwiser
65K views•2026-07-21

My Friend Locked Up The Engine On His K-Swapped Bug...
boostedboiz
128K views•2026-07-21