Queue source code

4 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
This is a free item
$0.00
د.إ0.00
Kz0.00
ARS$0.00
A$0.00
৳0.00
Лв0.00
Bs0.00
B$0.00
P0.00
CA$0.00
CHF 0.00
CLP$0.00
CN¥0.00
COP$0.00
₡0.00
Kč0.00
DKK kr0.00
RD$0.00
DA0.00
E£0.00
ብር0.00
€0.00
FJ$0.00
£0.00
Q0.00
GY$0.00
HK$0.00
L0.00
Ft0.00
₪0.00
₹0.00
ISK kr0.00
¥0.00
KSh0.00
₩0.00
DH0.00
L0.00
ден0.00
MOP$0.00
MX$0.00
RM0.00
N$0.00
₦0.00
C$0.00
NOK kr0.00
रु0.00
NZ$0.00
S/0.00
K0.00
₱0.00
₨0.00
zł0.00
₲0.00
L0.00
QR0.00
SAR0.00
SEK kr0.00
S$0.00
฿0.00
₺0.00
$U0.00
R0.00
ZK0.00
Already have an account? Log In

Transcript

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

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.