Get a month of TabletWise Pro for free! Click here to redeem

Queue source code

Share the link to this class

 Get access to thousands of classes and millions of flashcards

Sign up or log in to access this lesson
Already have an account? Log in


All right, now it's time to have a look at some source code for Q. So I implemented a queue and you can find the source code at the following link on GitHub comm slash my username slash data dash structures. Also make sure you have watched and understood parts one and two from the Q series before continuing. Alright here we are looking at some source code for a Q. So this source code is in the Java programming language, although you can probably translate it into any programming language that you need. So the first thing to remark is I have an instance variable here of a linked list.

So this is a Java implementation of a doubly linked list. We also use this in the stack implementation. As you'll see the queue and the stack implementations are very, very similar. So here I have two constructors, one create just an empty queue. And another optional one, which will create a queue but with a first element. In fact, I should probably check if this is no, but we we might want to allow no elements.

So let's just leave it like that. So the next method is the size of the size of the linked list. And similarly, this checks if the linked list is empty. Lists are both pretty self explanatory. Okay, next interesting method is the peak method. The peak method returns the element that's at the front of the queue, but it will throw an error if your queue is empty because you can't peek anything when your queue is empty.

Similarly, for poll, this will hold the element at the front of the queue, but unlike peak will actually remove the element from the queue. So next, we scroll down a little bit, I have offer, which adds an element to the back of the queue, I guess I am allowing for no elements. So if you don't want elements just put an if statement and throw an error or something. So the poll are removed from the front. And you can see offer ads to the back. So remove first and last.

And the last method in here is an iterator in case you want to be able to iterate through all the elements in your queue. Very short and very simple implementation just under 50 lines of code, you can create a cube Although there are faster ways of creating queues, especially with arrays, the idea with arrays, especially static arrays, if you know the maximum amount of elements that will be in your queue at any given time, then you can create an array of that maximum size and have pointers to the front and the back positions in your queue. And add and remove elements based on the relative position of those pointers. If you ever get to a position where you're running off the edge of your array, then you can loop around to the front of the array and keep processing elements like that the soul a lot faster than having to maintain references to the next nodes of such as in a linked list.

So I guess I'll say that's it. homework is to create a static array based queue. So guys, thank you so much for watching and I will catch you in the next video.

Sign Up