CSC300: Video [1/2] Previous pageContentsNext page

Open Playlist

the homework this week involves several functions over linked lists you're going to find the homework in the file my linked zero and that is located in the package algs13 in this video i just want to walk through and make sure you understand what each function is meant to do the first of these is going to check the size of a list so if i have a list with two elements the size would be two if i have a list with four elements the size would be four etc and you can see here in the corner case where the list is empty the size is zero we'll also have a function here to test the sum of the positive elements so this is going to compute the sum of the elements but ignore the negative ones so if i have negative 3 negative 4 negative 1 i just ignore those i'm going to add instead 11 21 31 and 41 resulting in 104 okay and you can see so here for some corner cases if there's only one element and it's negative well then the sum is zero um that's the same as the empty list and if i have a single element well i just return that as my my value the next function you're going to do is a mutator so it's changing the contents of the list all of the other functions in this exercise are accessors they're accessing the list without changing it so this function will remove the first element and so you can see the way the test is written i have the initial value of the list that's 11 21 31 and then the result after the function is called should just remove the first element so we have 2131 and you can see here for some corner cases if i have a singleton list i will of course end up with the empty list when i delete it and if i start with the empty list you know there's some choices here of what one would do the choice i'm going to have you take is just to ignore the function so the function will do nothing okay so if you try to delete the first element of the empty list we'll just do nothing succeeds leaves the list alone the next two functions are slightly more interesting the first of these is going to determine the length of a common prefix of two lists so you'll you'll write a function that has two lists as parameters so here's the signature of the function it's got the this list so i'm looking at this list and a parameter that okay i'm checking whether this list is uh what it has in common with that the way the test is written this list is first and then that list is second so we're checking this versus that if you're curious to see you know what exactly is going on you can actually look at the test it's uh the code is sitting down here so it's not that hard to read you can find test length of common prefix i've got the expected result which is an integer i've got two list representative strings and what i'm going to do is turn those things into actual lists and then run the code and the way that's happening here you can see list one dot length of common prefix with the list two passed as the argument okay so the first one is this the second one is that and then what you're being asked to do here is to determine how many values they have in common starting at the beginning so we're just going to check them sort of pairwise we have 11 and 11 so that's good 21 and 21 that's good 31 and 31 that's good then we have 41 and 5. oops not the same okay so the number of common things here is three that's the the length of the common prefix and the test here will show you what happens in various cases so for example if i have uh 11 5 and then 11 21 well the length of the common prefix is 1. for the empty list the length of the common prefix will always be 0 regardless of whether it's this or that that is an empty list the final function is just going to look at the even indices of the list that's the even elements and we're going to look to see if those elements are strictly increasing so if i see 11 something i don't care about then 21 something i don't care about 31 that's increasing strictly increasing uh if i see a five in that position instead that's not strictly increasing and so i'll return false for for the result of this function um and i do mean strictly increasing so if i have a repeated value then that's not strictly increasing and and will return false um and again in the corner cases here the singleton and the empty list will always be considered increasing okay so we'll just say true in those cases so that's the homework problems you're set to do they're all set in terms of a linked list these are member functions of the list so you're working on this list so my size function here node it takes no no parameters and that means that you're going to look at this first and then compute the size from there and likewise for the rest of them so the only one with a non with a parameter is this length of common prefix which i discussed before and as usual there's some reading up here for you to do so take a look at the reading and good luck with the homework

Previous pageContentsNext page