In the previous video, we have discussed the introductory part of queue data structure, right? In this video, we will see how actually we can implement queue. Queue can be implemented either using arrays or linked list, right?
or using stack also we can implement queue. So in this video we will see basically how you can implement queue using arrays. So before going to the implementation part you must check out the previous video. You can check out in this I button.
What we have discussed? Queue is a collection or you can say it is a list where the insertion is possible from one end and that end is known as rear. And deletion is possible from other end and that end is known as front.
This is how logically you can represent a queue. This is what front, this is what rear, right? You can insert data from rear end and if you want to delete the data, then you can delete from front, right? And... if you suppose consider here front and here rear that is also fine so insertion would be from rear and deletion would be from front right you can choose any option fine but you have to take care of the rule of insertion and deletion fine Now, how we are going to actually implement this queue using arrays?
Arrays means we will use static memory location. First of all, obviously we are going to specify the size, how much data we can store in this queue, right? the capacity of the queue and while implementation of the queue you have to take care of that rule that is FIFO rule because queue follows what FIFO rule first in first out that we have discussed in the previous video right. So obviously we are implementing this queue using array.
So you have to enforce this rule here. And the time complexity for enqueue and dequeue operation must be order of 1. See here insertion is known as enqueue and deletion is known as dequeue operation. Enqueue.
Right. These are more technical terms in context of queue. So we will use enqueue and dequeue.
Now we actually implement this queue using arrays. Fine. Now how to define how to declare array. Like.
this int array name and here size of the array that is any constant 5 10 as you wish 50 100 right here i am taking the name q because i am implementing q that is why i am taking the array name q and here obviously we will specify the size now how to write down the code c so now suppose in main function i am calling first of all nq function three times i am passing this two here five and minus one means first of all i want to insert 2 then 5 and then minus 1 right after that we will display the content of the queue after that peak function peak operation fine peak operation means it is going to tell you whatever the front value of the queue right without removing that value from the queue again dq function dq means it is going to delete the data from the front of the queue again peak and then we will display the content of the queue right now first of all Obviously, we are going to declare the size. We are using what static memory allocation here. Fine, in array. You can use dynamic array also. And for dynamic memory allocation, you can use linked list also.
Fine, but here I am using static memory allocation. So, I have defined int. Here the name is q.
And size is n. See, this q we will use in all the functions. That is why I am declaring it globally. Outside of main function and outside of all these functions.
After header file directly. Right. And here I am not writing that size in constant term. I am using what macro definition. Hash define n 5. So in program wherever you will use n then it will replace this n by this 5. Or here you can write hash define max size is 50, 100 like this.
But if you declare this q size within these functions then you cannot write here the variable. Then you have to specify here the constant. either 5, 10, 50 something like this right.
I am declaring globally that is why I can write here n. Fine now front and rear in Q these are two important parts that we have discussed. So you have to take two variables one is front and We will initialize this front first of all with minus 1 and rear is equal to also minus 1 globally because front and rear both the variables we will use in each function.
That is why I am declaring these variables globally so that we can access these variables in each function rather than declaring those variables again and again. every function right and minus 1 minus 1 means q is empty we don't have anything now we are going to define this nq function right how to write down the code so here also So see here I am passing value 2. So obviously you have to receive this value when you are going to define this function. Right.
Here I am calling this function. Fine. So now the data type of 2 is integer.
So you have to take int variable 2. to receive this 2, right? So you can say this is the logical representation of this Q. Size is 5, n value is 5, 1, 2, 3, 4 and 5. This is actually an array but we are using this array for implementation of Q.
So you can say here I am using this array as a Q data structure, right? Both front and rear pointing to minus 1 right now. Fine?
Now see. First of all, you will check for the overflow condition. And what is that overflow condition we have discussed in the previous video. When rear becomes here, rear is pointing to here, 4 means this n minus 1, maximum size minus 1. insert from the rear when you will insert first element then here it would be inserted means rear plus plus then again rear plus plus rear plus plus rear plus plus and rear plus plus plus when rear is reached to here maximum minus one it means we cannot insert any other data here q is full so first of all you will check for that thing for over overflow condition now here you will write if Rear equal to equal to n minus 1. It means here simply you can print printf overflow. Or you can say printf q is full.
Right? This thing we have discussed in detail. in the previous video you should check out that video first right now this is not a case so second case may be else if there is nothing in the q like this if q is empty then both front and rear would point to minus 1 that is the condition we have considered for empty q right so now else if front equal to equal to minus 1 and so Rear equal to equal to minus 1. In that case, Q is empty. Now, what you will do?
First, we will insert here. So, front would be increased as well as rear would be increased. Both would be increased. Right? Now, both would point to 0. Now here simply you can do first of all front is equal to rear equal to 0 minus 1 after that it would be 0 after incrementing.
So now you can insert here the data. So here what you will write see the name of array we have taken is q q and here you will write rear because always we will insert from the rear in q would be from the rear part is equal to whatever the value in x. Right? Now, third case may be else. See, first of all, n q 2 at starting q is empty.
If rear equal to equal to n minus 1 that this condition is not true. But this condition is true. Front is minus 1, rear is minus 1. Yes, both are minus 1. Right?
Front and rear equal to 0. means front is pointing to here, rear is also pointing to here, Q of rear, rear value is now 0, Q of 0 means here you will insert X, X value is what? 2, so now here I have inserted 2, now third case may be, Q is not empty, Q is not full. Q has some data and after that I want to insert another data.
In that case what you will do, suppose I have 1 in this Q, that is 2 I have inserted. Now I want to insert 5. In that case what you will do, we will not touch this front because we will always insert from the rear end, that is the rule of the Q, right. So now first of all rear plus plus, so here what you will do, rear plus plus means rear is now pointing. to here 1 now you can insert here the data so q name is q q of rear equal to x that's it see you can check now after inserting to both rear and front would be pointing to 0 now again call nq function q nq 5 x now become 5 if rear equal to n minus 1 no this condition is not true because rear is 0 Rear is not 4. This condition. This condition is also not true.
Now control will go into else part. Rear plus plus. Means rear would be pointing to here now. Q of rear.
Q of 1 is equal to x. Now here we can insert 5. Again call nq minus 1. This condition is not true. Because rear is still 1. This condition is also not true. also not true now rear plus plus now rear is pointing to here rear becomes now 2 q of 2 is equal to x q of 2 here we will insert x x value is minus 1 so this is the nq function now we will see the decoding for dq function right here i am writing this dq function Nothing, we are, see dequeue function, we are not passing anything, so I will not write anything here, right. Here also some cases are there.
First case is, if the skew is empty, in that case dequeue means, can we delete anything? No, we can't delete anything, right. So, first you will check if.
Front equal to equal to minus 1 and rear equal to equal to minus 1. This is the condition of empty queue right. In that case here simply you can print what less is empty or you can say under flow condition. Now else if.
When both front and rear are pointing to the same index, this thing also we have discussed clearly in detail in the previous video. So you should check out that video first of all. Suppose I have called DQ function two times and DQ means I have deleted this, I have deleted this. After deleting these two, front would point to here and rear is also pointing to here.
Means both front and rear are two. It means there is only one element in the queue. So here if if front equal to equal to rear it means there is only one element and after deleting this element this q will be empty right so here what you will write you will set both front and rear equal to minus 1 because this is the condition we have taken for empty q for showing the empty q front is equal to minus 1 rear equal to minus 1 right so that's it now third case may be this case q is not empty and there is more than one element in the queue in that case what you will do suppose we have three elements in the queue now simply you will do what front plus plus here i can write front plus plus because dq operation can be performed on the end that is front we can delete data from the front only that is the rule of the queue you have to take care of these rules right Because we are implementing here Q.
So you have to enforce these rules here. Right. So front plus plus. Front plus plus means now front is pointing to here.
Right. Now this is what a garbage value. Means we don't care what garbage value lies here in this cell.
Because suppose in future you will access this cell. Then you can overwrite this value. The Q is basically from front to rear.
means this is now the q so this is now a garbage value for us you can delete it right simply you have to do front plus plus and that's it and if you want to print the dq element you want to check out what element you have dq then before doing front plus plus here you can write print f the dq element from the q is percentage d i am writing only this thing because i don't have space so here you can print first of all this data so how you will access this data name of this is q q of this front because before updating this front, front was pointing here, q of front means q of 0, this would print 2 and after that you can do front plus plus and that's it. Now suppose again after this dq again, I am calling dq right again control will go here front equal to minus 1 rear equal to minus 1 no this condition is not true because front is 1 rear is 2 so control will not enter here in this loop else if front equal to equal to rear no this condition is also not true because because front is 1, rear is 2, so control will go in else path, printf the dequeued element is q front, q front is 5, so it will print 5, after that front plus plus means after that front becomes here, 2, front would point to here, now again suppose third time we are calling q function, in that case what would happen, see, now see this is the garbage value, front and rear are pointing to here, so the q is only between, between front and rear so this is only the q now these are garbage value means we have diluted these values right we don't care what values lies here now this condition this condition is not true this condition is true now front equal to equal to rear front is 2 and rear is also 2 as you can see right so it means we have only one element and as you can see we have only one element left in the queue. So, front and rear equal to minus 1. We will both set these front and rear equal to minus 1. Both will point here.
It means this is also now garbage value. We do not have anything in the queue. Right? So, this is. how we are going to enqueue and dequeue the data now we will see the coding for display and peak function now at some point of time in queue i have 2 5 and minus 1 right after these enqueue function now i want to display this data whatever the value in the queue so now i Now I will define the display function. First of all you will check if the queue is empty in that case obviously there is nothing to display in the queue.
So, what is that condition you will write if front equal to equal to minus 1 and rear equal to equal to minus 1. In that case here you will print what queue is empty right else. Now we will print this data. See every time I use while loop when you are traversing the list or anything or you are displaying the content. But someone asked me why all the YouTubers use while loop. So in this video I will use for loop.
Fine. So I will display using for loop. So for that case else.
Now obviously you have to take a variable i. So you have to declare that variable i here in the display function. Right.
Otherwise it will give you error. Now first of all i is equal to front. front from where I will display the content that is 2, 5 and minus 1. So, first of all I should point to here I is equal to front, front is 0. So, I is also 0 right.
Now, we will move this i and print the data till here right till here i want to print the data once i reach to 3 then i will stop so here you can write i'll print the data till i less than Rear plus 1 that is rear is 2 rear plus 1 that is 3 till then I will do I plus plus and here I will print that data. So here simply write printf percentage d and q in subscript I will write I. Right. Here in else part you can write the q is and after that you can print. Fine.
Now see. See the working of this for loop. This is not the case.
So control will go into else part for i is equal to front. Front is 0, I is also 0. I less than rear plus 1, rear plus 1 is 3. Yes, condition is true. So, control will go into this loop. Percentage D, Q of I.
Q of I means Q of 0. 2 would be printed. Right? I plus plus.
Now, I becomes 1. 1, i is pointing to here now, i is containing value 1, 1 less than rear plus 1, that is 3, yes, control will go enter into this loop again, q of 1 would be printed, q of 1 is 5, 5 would be printed, again i plus plus, now i becomes 2, so here is i now, i is 2, 2 less than rear plus 1, rear plus 1 is 3, yes, condition is true, again printf q of 2, that is minus 1 would be printed, i plus plus, now i becomes 3, so i is here now, 3 less than 3, no, No, condition is not true. So, we are not going to enter into this loop. Now, exit from this loop. Right.
So, the output is 2, 5 and minus 1. Right. So, this is what our display function. Now, the peak function.
How you will write that coding? This is very simple. We are just checking what the value here at the front of the queue. What is that value? Right.
Without removing that value. In peak also you can write down this condition for checking the empty queue. Right. I guess you can write down this condition I am not writing this in else part what you will write now if condition now the queue is no tempity in that case you have to print that data fine the data at the front so simply you will print here write printf percentage d you can write the data at front is then percentage d and here you will write queue and front in subscript you will write front in this case this condition is not true because q is not empty suppose i am taking this case else print f percentage dq front q is 0 means q of 0 would be printed that is 2 would be printed right and that's it the front of q is what 2 only right now and thus display function would 2 5 and minus 1 would display 2 5 minus 1 then dq function if you will print the data then it would print 2 after that if you will call this peak then it would print 5 because after 1 dq the front would be pointing to here front plus plus and now q is this one and the front of q is 5 so now this would display 5 display function would display 5 and minus 1 5 and minus 1 so this is how we can implement a q using arrays right But here the problem is the drawback is suppose the case is I have called enqueue function 5 times.
Suppose I have enqueued here 0 and here 7. After that I have dequeued I have called the dequeued function 3 times. and 3 so this this this i have deleted rear would be pointing to here and front would be pointing to here now suppose this is a q at some point of time right and i want to insert some another data i want i i called in q function again in that case it would show you q is full why so because see condition is what rear is equal to 4 means rear is maximum size minus 1 and that we have discussed that is condition of what q is full it will show you q is full but still we have we have space now we have three cells left so this is what wastage of space space we cannot insert data here although we have space but we cannot insert here now what if you modify your coding something like that if there is some space then what after 4 after this 4 again if you do some increment in this rear obviously we will not do rear plus plus we will modify that thing so this would give you 0 And we can insert here. After that here also, here also we can insert. Right?
So this is what a circular type of thing. Right? After here again 0. If q is empty. If there is still some space. space in that case you can do this thing right so this is what a circular q in this case everything would be same just you have to update a little bit in your nq function and dq function you cannot simply do front plus plus in dq and rear plus plus in nq you have to modify that thing a little bit right rather than rear plus plus what you will do rear plus one mode n you n means size of this one that is 5. Now in this case see rare is 4 right 4 plus 1 mode n value is 5 that is 5 mod 5 is 0 and now we are getting rear is what not 5 we are getting rear 0 and this space is free we can overwrite this car base value we can insert here the data right same for front also you will will do front plus 1 more and this thing we will discuss in detail when we will discuss circular queue fine this is a just an overview of that circular queue this is how you can overcome that drawback of this queue so now in next video we will see how to implement a queue using linked list so i will see you in the next video till then bye bye take care