Now that we have a good understanding of what a queue is, we are going to implement our own queue.
For this implementation, we are going to use the linked list collection.
So we need to bring that into scope using standard collections linked list.
So now we have our linked list brought into scope.
We can create our queue struct, which we're going to do over type with type T, and we are going to
have our element of a linked list also of type T.
So now that we have our struct created, we can implement methods on our queue with the first one being
our new method, which is going to return a queue of type T, and inside of here we have our queue struct
which is going to have our element which is going to be a new linked list.
So now that we have our queue created with our link list that is going to store our elements, we can
now start putting elements into our queue.
But remember a queue is FIFO first in, first out, So we need to keep that in mind when we are pushing
and popping values.
The two went out of the queue.
So our first function is going to be called on queue.
And what we're going to have is a mutable self with a parameter of a value of type T, and this is how
we're going to push values into the queue.
So with it being first in, first out, when we push a value in, we need to push it technically to
the back.
So since we are using the linked list from the collections, we have a function or a method and the
link list called push back and push back.
It's going to put it at the back of the line, if you will.
For Deku.
This is going to be our pop method again.
We're going to have to have a mutable self and this time we are going to return our option.
With our value inside of it.
So since we need to get the oldest element in the queue, we need to use the pop front.
Acid that is associated with the leaked list and this will get the value at the.
Front of the queue.
So the oldest value
now we want to implement our peak method and peak.
Since it's just going to return a reference, we can return an option that is returning a reference
to the value and we will say self dot element, dot, front and front is a method with the link list
that allows us to get a.
Reference return to the value at the front of the link list.
So now we're going to implement our length method
and this is going to return a user size.
And then here we just want to get the length of our linked list so we can return our link method.
And then lastly, we want to implement is empty
and this is going to return a boolean
and if our elements.
His empty arc is our link list empty.
If it is, then we will return true.
Otherwise we return false.
So.
It's a very simple way of implementing a queue using a linked list from the collections library.
But now I want you to take some time to create the test cases for this, and this will be our assignment
for this section.
And then in our next lecture, we will go over my test cases to ensure that our queue is functioning
properly.