1 00:00:06,220 --> 00:00:11,620 Now that we have a good understanding of what a queue is, we are going to implement our own queue. 2 00:00:12,640 --> 00:00:17,140 For this implementation, we are going to use the linked list collection. 3 00:00:17,380 --> 00:00:24,250 So we need to bring that into scope using standard collections linked list. 4 00:00:25,000 --> 00:00:28,270 So now we have our linked list brought into scope. 5 00:00:28,270 --> 00:00:36,160 We can create our queue struct, which we're going to do over type with type T, and we are going to 6 00:00:36,160 --> 00:00:41,200 have our element of a linked list also of type T. 7 00:00:43,630 --> 00:00:51,100 So now that we have our struct created, we can implement methods on our queue with the first one being 8 00:00:51,100 --> 00:00:59,830 our new method, which is going to return a queue of type T, and inside of here we have our queue struct 9 00:01:00,340 --> 00:01:04,810 which is going to have our element which is going to be a new linked list. 10 00:01:07,600 --> 00:01:16,750 So now that we have our queue created with our link list that is going to store our elements, we can 11 00:01:16,750 --> 00:01:19,930 now start putting elements into our queue. 12 00:01:21,130 --> 00:01:28,570 But remember a queue is FIFO first in, first out, So we need to keep that in mind when we are pushing 13 00:01:28,570 --> 00:01:30,190 and popping values. 14 00:01:31,770 --> 00:01:33,430 The two went out of the queue. 15 00:01:34,120 --> 00:01:38,500 So our first function is going to be called on queue. 16 00:01:39,040 --> 00:01:47,170 And what we're going to have is a mutable self with a parameter of a value of type T, and this is how 17 00:01:47,170 --> 00:01:50,860 we're going to push values into the queue. 18 00:01:51,280 --> 00:01:58,540 So with it being first in, first out, when we push a value in, we need to push it technically to 19 00:01:58,540 --> 00:01:59,380 the back. 20 00:01:59,980 --> 00:02:07,330 So since we are using the linked list from the collections, we have a function or a method and the 21 00:02:07,330 --> 00:02:10,990 link list called push back and push back. 22 00:02:10,990 --> 00:02:14,530 It's going to put it at the back of the line, if you will. 23 00:02:17,210 --> 00:02:18,440 For Deku. 24 00:02:18,440 --> 00:02:22,130 This is going to be our pop method again. 25 00:02:22,130 --> 00:02:30,230 We're going to have to have a mutable self and this time we are going to return our option. 26 00:02:33,650 --> 00:02:35,660 With our value inside of it. 27 00:02:37,130 --> 00:02:46,760 So since we need to get the oldest element in the queue, we need to use the pop front. 28 00:02:47,500 --> 00:02:53,950 Acid that is associated with the leaked list and this will get the value at the. 29 00:02:54,800 --> 00:02:56,930 Front of the queue. 30 00:02:57,350 --> 00:02:58,790 So the oldest value 31 00:03:01,370 --> 00:03:05,840 now we want to implement our peak method and peak. 32 00:03:05,840 --> 00:03:13,730 Since it's just going to return a reference, we can return an option that is returning a reference 33 00:03:14,840 --> 00:03:25,640 to the value and we will say self dot element, dot, front and front is a method with the link list 34 00:03:25,640 --> 00:03:28,010 that allows us to get a. 35 00:03:29,060 --> 00:03:33,200 Reference return to the value at the front of the link list. 36 00:03:34,970 --> 00:03:37,460 So now we're going to implement our length method 37 00:03:41,600 --> 00:03:44,360 and this is going to return a user size. 38 00:03:45,260 --> 00:03:52,880 And then here we just want to get the length of our linked list so we can return our link method. 39 00:03:55,130 --> 00:04:00,380 And then lastly, we want to implement is empty 40 00:04:04,280 --> 00:04:06,800 and this is going to return a boolean 41 00:04:08,960 --> 00:04:11,420 and if our elements. 42 00:04:13,640 --> 00:04:15,950 His empty arc is our link list empty. 43 00:04:15,980 --> 00:04:18,830 If it is, then we will return true. 44 00:04:18,860 --> 00:04:21,050 Otherwise we return false. 45 00:04:21,710 --> 00:04:22,310 So. 46 00:04:23,090 --> 00:04:29,960 It's a very simple way of implementing a queue using a linked list from the collections library. 47 00:04:30,590 --> 00:04:39,950 But now I want you to take some time to create the test cases for this, and this will be our assignment 48 00:04:39,950 --> 00:04:41,990 for this section. 49 00:04:42,410 --> 00:04:49,010 And then in our next lecture, we will go over my test cases to ensure that our queue is functioning 50 00:04:49,010 --> 00:04:49,880 properly.