what's up guys this is teddy welcome to my youtube channel this video we are going to be talking about the q data structure and the q data structure really isn't that much different from a stack and i highly recommend everybody learn what a queue is it's it's not difficult to learn or it's not difficult to understand it's not difficult to learn and um you're probably going to be asked this somewhere down the line so just stick with me through the through the whole entire video even if you're a self-taught developer just looking for a junior
role this will this will definitely benefit you so here in the south and i always like to start off with the story because this this really helps people remember if you've ever been to the south in the united states the south is a very unique place we have our own unique way of doing things we have our own unique culture and one thing you will notice if you go to the south is that everybody in the south is like seven foot tall or everybody's seven foot wide like there's no in between and like a lot
of people are really like people see me like oh my god you like seem like such like you look like such a small person on video but when i meet you you're just like this huge monster because i live in the south and we're all we're all well fed and just like we're all massive people here so and one of the reasons is because we all love fast food and we love this restaurant called chick-fil-a and chick-fil-a has the biggest lines you'll ever see like you go through the drive-through at chick-fil-a and you almo they
i think that there's places that have to hire like security and they have to have like a traffic person directing because there's literally so many people who are trying to go through chick-fil-a's line that they have to have people you know controlling just how like crazy and rat like just insane people get just to get into chick-fil-a's drive-thru and the best way to understand is uh to understand a cue is a drive-through so let me see here we're going to go through here so just imagine we've got this array and we're going to build a
cue with an array and it's first in first out and let's just kind of think about what that means so the front let's just let's just say this is the front and this is where everybody gets their food this is the front and as people are feeling and this is this doesn't even hold a candle this is like chump change compared to like how many people like just imagine like chick-fil-a literally just opened up in the morning and the first person enters the drive-through and they're about to get their food okay so that would be
an ad item or that would be an in queue and there's different words some sometimes people will call it an ink you sometimes people will call it an ad but basically what's happening is you're adding an item to the end of the data structure when people are going through chick-fil-a's line it does not behave anywhere anything like a stack it's just imagine uh whenever you see one of these data structures like literally just think of like chick-fil-a and somebody going through a drive-through so chick-fil-a just opened and you know like within like the next 30
seconds there's gonna be another person so there's another person and he's might get in a fist fight with you know the person who's directing traffic because he's so hungry and he's from the south and he's got to get his chicken he's got to guess chicken nuggets or he's going to you know have a freak out so the next person same thing we're we're going to town like chick-fil-a's opened up and all to all you know everybody's this thing's flying this thing's ready to go but as people are getting their food and people are satiating themselves
and gorging themselves like filthy the filthy animals that they are what they're going to be doing is they're going to be dequeuing they're going to be removing from the front very rarely think about it very rarely does somebody from the south ever leave a chick-fil-a line without getting food so they're going to be going through and they're going to be dequeuing from the front and whenever you go through chick-fil-a's line so when somebody's dequeued what's going to happen you may think well this this thing is going to be this part's going to be blown off
right here like this whole data structure is going to be gone well no that's not actually the case and what you kind of want to realize about data structures and a lot of these data structures is that the front's just going to be moved like there's not you know you're not going to they're not going to be like deleting places in chick-fil-a's line like the the line is just logically going to move up and people are just going to logically move up because now the front of the line is there and as people are filling
in it's going to be the same exact thing people are being added to the end people are added to the end and this is like the back of the line and people are being added here and when they are removed and when the front of the line is moved up and we always have a front it's just going to it's just going to go to the next person it's not like there's some set place in like chick-fil-a's line where people have to you know fill into that one spit that one space like the front of
the line literally just moves up and that's that's kind of an important distinction to make and um also we we have peak but peak is very similar to how a um stack works you're just getting the top you're just get you're just literally accessing the front of the line so the front of the line is always moving up and if we want to peek we literally just get the front and the front is returned to us it's not um anything that complicated so that is really pretty much it there's not it's not like that crazy
and to be honest with you coding up a queue really isn't that hard so we're just going to go ahead we're going to jump in here and we're actually going to start working on this queue so we're going to go q we've got i've already got it built out and i've already got a nice little folder for it but feel free to put it wherever you want to you can even put it in actually i would not put this in the program file because that is going to look like doo doo but you still could
i always recommend whenever you're implementing an abstract data type like a queue or a stack or a linked list you put it in a class but if you want to work on arrays i always recommend putting it in the program file with c-sharp okay so i'm going to put my little pin down and we're going to go ahead we're going to start working on this thing and i'm just going to delete this is something that's like messed up with right here so i'm just going to delete this in react because like the um parentheses are
off and you always spell q like q i don't know q u [Music] okay i'm being a goofball so we're gonna go in here and we're gonna start uh adding these properties and i'm going to explain these properties one by one and the properties and cues and stacks in linked lists are some of the most important parts like people don't really take the time to explain like just how important it is to understand like how these variables work so this sets the number of elements okay and because this is an array and we have we
have to set this because this is an array if this were a linked list we wouldn't have to do this because this is an array although you still you still probably could but okay so we're going to go here and then we need to actually have an array to store this you could use a linked list and i will make a video um on linked list on how to actually implement a queue with a linked list but it's really not that much different so we're going to call this the q array because this is what
the actual data is going to be stored in then we're gonna go in here we're gonna go we're gonna do another prop and you can make the you feel free to make these private if you want to i think private would be a little bit more official i guess the word would be um because you don't want people to be able to access this from within the program cs file but um it doesn't really matter if we're just coding this if we were in a professional environment and you were building this for like a company
you may want to make these private but if we're just coding in our basements then it doesn't matter so here we've got then we're just going to have a little um pointer to keep track of all the items so this would be sort of like the end link so this is let me see here the actual array like this is what we're actually going to store the values in because we are building this queue off of an array so we're gonna this is the actual array we will store elements in then here because this is
a q remember in the stack we only had to keep track of the we just kept track of the front but because this is a q we have to keep track of both the front and the rear so this will be the index to keep track of front and remember that remember what i said the line is it's not like we are uh let me and i'll just go back to this it's it's not like we are just blowing this off right here and just boom like and that's going to be what you know we're
just going to make that disappear like what's really going to happen is the front is just going to be moved and that is going to be what logically uh you know is going to show where the actual front is it's not like we're going to actually delete that part of the array or delete that part of the data structure because remember data arrays can't be deleted so we're going to have the front here and then the front is going to be what logically keeps track of our array then we'll get and then we have to
have the rear as well too because this is a linked list and people are remember leaving from the back and p are people are leaving from the front and people are being added to the back we actually have to keep track of that rear too we have to we have to keep track of this for when people are added so keep track of adds then this will just sort of be like a length this will keep track of the length okay so those are all of pretty much the variables that we're going to need in
order to uh have this uh queue so now we're going to go down here we're going to have a constructor and very similar because this is an array remember we have to initialize arrays in c sharp and java in python you don't actually have to do this but because this is c sharp and java you actually have to initial you have to initialize these arrays so we're going to have a max size and it's going to be equal to the size and then we're going to have a cube we're going to have a q array
and then this is what's going to initialize um this we're going to initialize the max size or just the size and then here we're going to have the front and we're going to initialize the front as zero and then we're going to initialize the rear as negative one okay and then we initialize the uh the rear to negative one because there is no item in the array to be considered like the last item like at the front there is you know there's a very defined front but when we initialize the rear we don't want to
initialize it at the we don't want to initialize it as a certain point because the rear doesn't even exist yet the front already exists and we already have like the first slot in the array already figured out but we don't want to actually initialize the rear yet because the the rear is going to be uh people are going to be going in through the rear okay so we've got our constructor built out let's start actually working on ways to manipulate our data and the first way probably the best one to start with is we're going
to learn how to enqueue or insert into a queue and before with cues it's very easy to get confused so just close your eyes you don't have to close your eyes if you don't want to but i'm just going to close my eyes and i'm going to think like chick-fil-a chick-fil-a somebody's going into a line at chick-fil-a what is happening they're going into the front and the rear is going to be the the rear is actually because they're coming in through the rear we're not taking anything away from the front we're adding to the back
of it and this person is going to get their chick-fil-a they're going to be satiated and they're or they're not getting it just yet they're they're going into the line and they're they're trying to get into the drive-through so if they're going into the drive-through the rear is going to increase this rear variable is going to increase and then once that rear variable is is increased we're going to go into our q array and then we're going to go to where the rear is and then we are going to add this item and we need
to add because we need to add this number we need to go into here and because we're adding in we're actually adding we need to actually have a actual place to pass in a variable so the rear is going to we're going to increment our and i'll just put a increment so we're going to increment our pointer or our just number that's going to keep track of where the line actually ends and insert you see here and insert into where the rear was incremented great then at the end and i guess we don't even have
to have this length i just decided to add this length increment the length so we can keep track of this length so we're going to go in here and we go in we're going to go to n items we're going to go add and we've got that but technically we don't even need that really what we need right here is we need to increment our pointer and then we need to add where we incremented our pointer and that makes total sense so what's going to happen is the rear let me see i'm just gonna blow
this one away so boom boom so our front is right here and we haven't actually even changed anything and what's gonna happen is we actually have our rear you see i'm going to go in here then what's going to happen is our rear was right here our rear was here but it's going to go over here it's going to go into here now this is our new and then we're going to insert whichever value that we had or whichever value that we chose let's just say we chose to go with one and now that is
the rear of the line because people are being remember people it's a chick-fil-a line it's not a stack it's not like a regular array people are going in through the back just like chick-fil-a okay so now we're going to go and we're going to do our dq and just to kind of reiterate it just be like somebody is going into chick fillet line or whatever restaurant could be starbucks could be dunkin donuts whatever uh type of really popular restaurant that you want to think about okay so now we're going to do our dq and dq
is when the person has finally gotten their chick-fil-a they're not going to fight anybody anymore and they're not going to cut coral teddy off in traffic on his way to work and they're going to be they're going to satiate their bodies okay all right so we're going to go here we're going to go into the queue and we're gonna go q array and we need to hold this we need to hold this in a temporary variable just remember that whenever you're deleting dequeuing removing whatever word that you want to call it you're gonna have to
hold stuff in a temporary variable then we're going to go we're going to add to the front so once we add to the front it's going to increment our front and then we'll see if the front we need to do some kind of check if the front is equal to the max size we're going to go down here and we're going to put the max sizes equal to zero and this will prevent just uh this will prevent so when if it reaches the max size it's going to prevent it otherwise we're going to get an
error and we're going to get an out of bounds value because this is an array so here we have n items and then once that we need to return our temporary variable and that's just going the temporary variable is just there to kind of to have a return a lot of times when you have these algorithm questions i don't really know why i guess you could just have a void but they always whenever you dq or whenever you remove something it seems like they always want you to remove and they always want you to return
the valuable that you delete off of it so logically what's going to happen so we're going to go here and then because we're incrementing it you would think that we're going to decrement it but if we decrement it it's actually going to go that way and it's almost going to be like the the rear is going to go up but when we decrement it this is actually going to go back because it's in our array and it's going to make the um it's going to make the array smaller and kind of like a weird way
a lot of times you look at that you like well if we're like why are we adding to the front if we're taking away from the front and you just kind of have to realize that it's um it's it's going back because it's in the front if that makes sense so next thing let's go ahead and we need to do a peak and the peak is going to be like the easiest of the easy whenever you want to do a peak you're just going to get the front and all that you're going to do is
you're going to return the variable and the intellisense already knows what you want and that's pretty much going to be it so we're going to go in here then we're going to go into our program.cs file and go q we're going to say uh let's just lowercase q then we're going to go equals equal to new q and let me see and we can actually bring in microsoft's version but we want to bring in our own because we just made it you will see a red squiggly line here and we need to initialize the values
that we have in the array and i'll show you maybe you don't understand like what's going on whenever you create this constructor right here if you make it so that and we have to do this because this is a uh um this is an array and i guess we could just hard like if you wanted to you could just hard code that value in there but i i think it's better to uh be able to control the initialization of the array so when we have that that means we have to pass that value in here
because it's going to be passed into here and if you if you have if you have that ability in the constructor you have to put that in there so we're going to go q then we're going to we're just going to enqueue a couple of these things so we're gonna go one two then let's do q we're gonna go q dot dq and we'll just go that and for some reason it did enqueue so we're gonna go dq and we'll just dequeue two of them and then we'll go down here and we'll do a couple
peaks so we'll go q dot peak okay and last but not least i'm going to step through this and kind of show you guys what's going on under the hood so very very key point and i always um try to be clear about whenever you go through and you step through with the debugger i'm going to show you something very important and this will you know this is going to help you a lot so we're going to get rid of all these and we're going to go ahead and we're going to get put the debugger
right here and then we're going to step inside and what's going to happen is that this constructor here is going to execute if you don't know what constructors are i've got a whole video on the topic we're going to go down our max size and this is really important to kind of realize what's going on inside this constructor so this this.max size is representative of this so the size is going to be put in here so watch what happens the size is zero now and then once we execute once that is executed up here you
can see it's got a 10 inside of it so be very aware of that next we're going to go down here we're going to add watch and we're going to just kind of keep track of all these variables as they are incremented and you'll see how this thing actually works under the hood so we're going to go here we're going to go rear and our max size has been but our q array has not been initialized yet and the same thing is going to happen it's going this this dot q array is representative of this
this is like what that is this is what that is okay so the q array is now initialized you notice that before it was zero now we have a nice little array down here we're going to have our front initialized so the front is going to go up here it was zero it it was your before and it's going to be zero now because that's what it initialized but rear rear is zero now and then now rear is negative one so we're going to go in here as of yet we don't even i don't even
think our queue has even been initialized yet so we're going to go in here watch what happens like to pay attention to this part right here watch what happens so we're going to go boom whoa what was that so now there's an object inside of here like think about that we just created our own little toolbox as i mentioned before we created our own little abstract data tag we've created our own little toolbox of data and now we can work on this data think of it as almost like drawers in your house like you keep
you store your integers and you store them under cool little names you store like my dad's toolbox my dad's toolbox analogy i always keep you know my my dad always you know his wrenches in there and then his you know screwdrivers in there and we have nice little labeled places to keep all of our tools keep that in mind so now we enqueue and i already skipped over that one but watch what happens when we step into the enqueue so we're going to go into the enqueue we're going to increment the rear now the rear
is one so now we actually have somebody who's been added to the we've enqueued somebody we've added somebody to the chick-fil-a line they're now you know they're in line they're ready to satiate their bodies and they're you know they could fight somebody at any moment they could just snap and like get out of their car and the police may get involved but until like once they leave the line they're good to go so now we're actually going to so they're in the line and now we have n items we're going to enqueue remember chick-fil-a's line
it's filling they've opened they're you know full full throttle people are feeling this lineup and they are not stopping we've got four people in this line now at chick-fil-a so now the wonderful efficient workers at chick-fil-a have finally got you know finally satiated these people they're not gonna fight anymore they're not going to you know they're going to leave the line so here what's going to happen is we're going to step into the dq the front's going to go up the front is going to be incremented it's not the max size the n items are
going to be um taken away and now our temp is going to be returned so our temporary variable otherwise the reason why we have this is because we don't want to manipulate this q array like we don't want to put this like down there we don't want to like return that we want to house it in a temporary variable so that the front can be incremented so just remember this whenever you do a deletion operation you always want to hold it in a temp variable because you're going to store it in a variable then you're
going to do the actual going down and that's when you're going to return the temp variable because that's always what they just asked for that for some reason this could be void you would you don't even have to return the integer you don't have to return the temp value but that's just what people do like if you really want to go bare bones you could just increment that to the front and it would be good to go but we're not going to do that so we're going to dq again we're going to dq again but
notice something our line like it's it's not actually going down but if you look here the rear is going to be three and then the front is going to be moved up like the front is moving up those values still exist there they're almost like phantom values but logically our front is moved up and those values while they still exist the front has been moved up and it's almost like the va you know it's shrunk and the values are there kind of like as a phantom value but they don't really exist logically so if you
were to iterate over this it wouldn't really even exist and you could totally just null out those values and blow them away or you know i call it blowing off the top or blowing off the back of it it's like you just you know make the the caboose disappear or you make the front of the train disappear but you typically you don't do that and you don't really even have to because people are going to be iterating through this and this is just a small data structure anyway like i see if you had like millions
but that's not really the case so we're going to peak we're going to see which one's at the top and let me go in here and see which one it is so we're going to peak and we're going to return the front so the q and then the front is going to be 2 let's see 0 1 2 and let me see here so front is 2 and then we're going to go into the 0 1 2 so the front is now 3 and that's the one that's going to be returned and that is exactly
how cues work um i hope that you guys enjoyed this i hope it wasn't too confusing i know data structures are confusing if you did enjoy this and you learned something make sure to hit that like button make sure to hit that subscribe button and as always thank you for watching