So, in the previous video, we started our abstract class challenge,
the solution, and I want to actually finish it up, hopefully, this video.
Before I actually continue on with the code, there is actually a way to shortcut
a little bit of this code, and I'm talking specifically about the add item method,
which is of course in the my linked list class that we created in the last video.
And the reason we can actually shortcut the code,
is relating to the methods set next and set previous.
Of course if I click through to those.
We actually go to the abstract class, and that's our list item.
Set next and set previous.
And of course we implemented that in our nodes.
I'm going to the node.
You can see, set next, we're actually setting the item, and
then we're actually returning the item as well, and same for set previous, so
because we're actually returning that reference in here, what we can do is,
we can actually shortcut some of our actual code.
Cause, at the moment, you can see, every time we actually add a new time,
we've gotta set the the set next and the set previous,
unless of course it's a new item for the first time.
But most times we're gonna be executing both of those.
We can actually shortcut that by actually doing something like this.
We can actually put [SOUND] dot set previous [SOUND],
and then pass the current item as well.
[SOUND].
And that's actually an abbreviation,
instead of having two lines you're doing it all in the one line.
And again, this relies on the fact that, a,
the method is actually returning the right item.
We can actually achieve that, so we're returning that and
then we're setting the set previous and we're calling the set previous method
on the item that's been returned from the set next if that makes sense.
We're actually doing that as well.
That's the first one, we can actually change, but
we can also make a change, also down here.
You can do the same thing for that, we can do set previous.
Like so, and then with this one down here, we can do exactly the same thing again.
Like so, which abbreviates it.
And then the final else down here can actually do the same thing to
that as well.
[SOUND].
Just a way to abbreviate the amount of code.
And again, it's reliant on the fact that we're actually returning the item and
then recalling the method based on the item that's actually returned.
Okay so in order to actually test this code,
we actually need to actually start writing some code for our traverse method.
And it's actually not that hard.
We can start off by putting a while loop.
We start to make sure we've actually got some data.
While root is not equal to nul.
[SOUND].
Then we're gonna do [SOUND] system out dot print line root dot get value.
[SOUND].
Then we'll do root equals root dot next.
So we'll proceed to the next entry.
So that's in the case where there was some data.
But it's probably gonna be wise to actually also test to see whether it's
nul, we can actually put a message to that effect, so we can put something like if
root is equal to nul [SOUND], then we'll change that to the list is empty [SOUND],
and we'll change this here to an else, while, and I'll indent that code, and
what I'll do is I'll just, actually, put another semicolon in there, like so.
And then it automatically gets fixed up as you saw,
by IntelliJ actually indented that for us.
And now we should be after output the actual link list,
but also display if it's actually nul.
I'm just going back and
actually start running some code to test some of this stuff.
I'm going to go down to our main method and
you remember that we wrote that code at the start, the split code and
I'm going to start typing below that.
Actually, I'm going to start typing above that because we're actually going to be
adding to that.
So I'm going to put it here.
I'm going to type linked list, sorry my linked list, my link list,
list equals new [SOUND], link equals new my linked list.
Nul.
So, we haven't actually added anything yet.
And if we do a list dot traverse, list dot get root.
Let's see what actually happens.
We'll actually run that first.
The list is empty,
so it's correctly showing us that there's no data in the list, which is good.
We can leave that code in there for
splitting the various capital cities of Australia.
But what I'm gonna do now is actually put this code in here now, write some code and
actually put it in its place here.
We're actually gonna add the item to our linked list, to my linked list class.
Let's add item, we're gonna call it, new, we gonna create a new node,
which courses our list item.
The class that actually implements the list item, abstract class.
It's the class that is actually, if you go back and have a look, node we're
extending from list item, and of course list item was our abstract class.
I'm going back to that code.
We're going to add a new node [SOUND],
which is the actual capital city in this case.
That should add the title there, as you can see, and just a confirmation.
We talked about that briefly, but the string split method returns a string
arise, as you can see on the screen on line 63 and then it turns a string array
containing all the individual items of the string, that we defined in line 61.
They can specify the character that separates the items.
You can see on line 63 after the dot split, how we actually got space.
The space is effectively the delimiter.
Or the character that sort of separates each key string in that actual big string
itself.
Now we've done that, we'll actually put another list in there, list dot traverse,
traverse and list dot get root, so we'll actually go through now and
actually run it.
The first time we run that,
of course, we passed this list which was effective at that point nul.
But now if we do this, we've actually got some data that we've added so
we should get a different result.
And you can see we've now got the various cities, and we've also got
an error message here, the site Canberra is already present and not added.
But the rest of the cities, Adelaide, Brisbane, Canberra, Darwin Melvin, Perfis,
and Sydney are actually added in the correct order, and
of course the reason that Canberra came up as a duplicate was that we actually had it
two times in that list purely to confirm that that would actually work.
Next we need to go back to our my list link class.
We need to implement the remove item.
Currently, it's not actually implemented.
It's just returning files.
So what we need to do is be able to actually delete items.
Now, removing items is relatively straightforward in a link list.
We traverse the list until we find the item to be removed.
Then we set the previous link to point to the where,
to where our item to be deleted point, if that makes sense.
We're basically just sort of filling in the gaps and making sure that we still got
we're not actually off in creating any off in records in the link list.
Again, what we're trying to do is set the previous link
to point to where our item to be deleted points.
Let's go ahead and actually do that.
First thing, I'm gonna test if it's nul.
If item is not equal to nul, [SOUND] then we're just gonna print out a message.
So, [SOUND] deleting item plus item dot get value.
Like so.
And next we wanna do a list item [SOUND], current item equals this dot root,
so I'll start at the top, basically, the very first entry.
While count item is not equal to nul.
While we've still got some records to basically go through.
And then we're gonna type int comparison equals current item dot compare to item,
item which was the list item that was passed to this method.
if comparison, now remembering what we're trying to do is actually find this item
in the linked list.
So we cannot do anything till we actually find this specific one.
And remember we wrote the compare to function so
we can do exact match to make sure that we're deleting the right record.
If comparison is equal to zero then that means we've actually found the record to
delete or found the item to delete.
But we need to do a test to see whether this is the root.
If it's only one entry and this is the root record,
we need to do some special processing there.
Put current item equals this dot root, so if this is the root record,
the very first, the head, in other words [SOUND], then we're going to type,
this dot root equals current item dot next, so we're basically making
sure that we're setting root what is going to be the new root.
To point to what currently is pointing to the next record if that makes sense.
So we're deleting the first entry, the root entry, and
we're pointing it automatically.
So it's actually pointing now to what is the next list item.
That list root is currently pointing to with dot next.
We're setting that, but we haven't actually deleted it still yet.
We'll do else current item dot previous dot set next current Item dot next.
Again what we're doing is we're looking at the previous entry because this particular
record, so assuming this isn't the root record,
we're looking at what is the previous entry, and we know that there is
a previous entry because I've already tested to see whether there is a root.
And we're setting that to point to what is the current item next.
In other words we're bypassing this record effectively.
This item that we're actually gonna be deleting.
And what we need to do is test for now.
If current item dot next, if that's not equal to now.
[SOUND] We're gonna do current item dot next dot
set previous current item dot previous.
The same thing there.
We're actually doing the other side of the coin.
For the next item, we're setting that to actually point to the current
item's previous, if that makes sense.
Sounds a little bit confusing, but we're making sure that both pointers,
the previous and the next, are actually now still pointing to the right places.
Now that we've done that,
in the case where did have a direct comparison we can return true,
because we did find the record and were able to make those changes.
Otherwise we need to do another check we need to do here.
Cause if comparison is less than zero,
then that just means we haven't reached the point in the linked list yet.
So we'll put current item equals current item dot next.
So we basically want to continue navigating through the linked list to
see if we can find the record.
But otherwise, effectively if comparison is greater than zero.
[SOUND].
If comparison is greater than zero at this point,
it means we've got passed the point where we could have actually deleted it.
I spelled that wrong [SOUND], so basically here, we are at an item [SOUND],
greater for sort purposes than the one to be deleted.
[SOUND].
The item isn't, is not in the list.
I've reached past the point of where it would've actually been in the list,
based on the sort criteria.
We're gonna return false at that point because we can't do anything else.
We know that we can pass that point.
[NOISE] And for a final test and
will return false,
the next defining thing.
[SOUND] Without finding the item to delete.
[NOISE] So that's basically remove item.
The test is actually create a list of numbers in our main method and
actually just run through and actually see whether it actually works so
I'm gonna go back to the main again below the other code we've added.
And actually for simplicity purposes let's just do it this way,
let's just put this in here.
And we're going to put five, seven, three, nine, eight, two, one, O, four, six.
Cause all the other codes actually there to create our link list.
We actually run that just to make sure it's actually working.
We're getting the sorted in the right order, which is good.
Let's actually just, what I'm gonna do is,
I'm gonna paste in a bit of code here just to save us a bit of time.
Let's actually just paste this code in.
And you can see what I'm doing here is, I'm actually removing an item,
printing out the list, removing an item, removing more items.
And actually just running it through.
So let's actually go through and actually see what it does.
So I can see in the first case, at the top,
we're moving item three, so that should leave us.
Remove three, zero, one, to, three, four, five, six, seven, eight, nine.
Removing item five, zero, one, two, four, six, that's three and five we're deleting.
Deleting zero, four, and two, that leaves one, six, seven, eight, nine.
Deleting item nine, one, six, seven, eight.
Deleting item eight, one, six, seven.
Deleting item six.
When the sevens only left and deleting our one leaves only one.
And the last entry, we're actually deleting what is left.
So the last entry, the reason why we haven't actually specified the number
here, we actually called it, just to remove item.
We called it with root entry.
Basically the only one that was reaming, which was the number seven.
Deleted that, and the list is now empty, so it's actually working.
I'm going to actually end the video here.
I was actually going to actually show the search tree in this video, but
I'll actually go ahead and actually do that in the next video.
That's the one where we're actually going to create the optional search tree,
and I need to actually talk about the differences which are gonna be in
the implementation of the add item, remove item, and traverse, and
it really needs its own video.
So, I will see you in that next video.