QUEUE IMPLEMENTATION USING ARRAYS - DATA STRUCTURES

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello friends welcome back to our channel so in the previous session we have seen the implementation of a stack which is a linear data structure and the implementation of a stack can be done in two ways one is by using the air is another one by using the link list so in the previous session we have seen the implementation of the stack using arrays now in this session we'll go with the another linear data structure that is called Q's Q so this is also a linear data structure similar to the stack with a little bit difference and here also there are two operations to be verified that is insertion and deletion so in the stack we have seen the insertion is called as push and the deletion is called as pop here the insertion will be called as NQ and deletion will be called as DQ and see this is also a linear data structure similar to the stack so it is a linear data structure the first point right so that means all the elements will be arranged in sequential manner so it remains in sequential manner this is third point so in the stack it follows the lastin first-out mechanism that means the recently added element or recently inserted element will be removed first so here the Q is it follows the FIFO mechanism so it follows fearful if I fo that means first in first out so that means whatever the element inserted first that can be removed first right so best example for this cue is standing on a line for ticket reservation this is the best example for this cue okay standing on line for ticket resolution so the person who comes first if you will get the ticket first it first come for sir similar to the first come first serve so here all in the stack there will be a top position and both the insertion and deletion will be done at the top position right so here the insertion and deletion will be performed on different positions see here two operations will be there first one and Q and Q and did you so thank you is inserting an element into the Q DQ means deleting an element from the queue so and Q is inserting an element DQ is deleting energy and also there are two conditions to be checked while implementing these enqueue and dequeue operations to two conditions a water flow condition under flow condition more for a laminar flow say these two we have seen in stack implementation so the definition is same so our view means insertion into you which is full which is full enough amines deletion from and big queue so that is called a waterphone this is called enough room so overflow condition must be checked in MQ operation and under flow condition to be checked in and take your operation right then so in the stack we have seen another thing that is called a top tup which is at top of this tab so go to him in I mean the push operation is top operation will be performed on the same thing that is from here the mq will be performed on one position and think you will be performed on another position right let us go with that and then we will directly go with the implementation here there will be two ends friend ray find points to starting in with whereas arrange points to mastiff and here also we need to specify the size okay so that we will see later so friend end grain so mq operation NQ increments in session we'll be done at rare and rare similarly DQ will be done at friend there's only difference and now the representation representation of the Curie's in this way so this is the friend this one will be the rain right so okay Frank Andrea here we will perform beat you here will be perform thank you so if you want to insert an element into the queue that will be inserted here if you want to delete an element from the queue it must be deleted from here because the first element entering into the queue which will be the first element to be removed right so this way we have to represent a queue so this is a queue this is a queue Frank and Ray okay so if you are right so initially the front end rail will be initialized to minus one initially friend is equal to where is equal to minus one so if you want to insert an element first we need to build the rail and then we have to insert an element if you want to delete an element first we want to reduce the tellement which we want to remove and we need to update the friend right hope you understood this one right now like we'll go with the implementation paths implementation how the queue can be implemented here also queue can be implemented in two ways so first one by using Alice another one by using linked list in two ways we can implement this true now in this session we'll go with implementation of queue by using as in the later video we will see the implementation of Q using lincolnís right Q using ants implementation of queue using Alice no see initially the front end rail will be minus one and if you are inserting an element that first aware should be abraded right so let us see this first one we need to fix the size of an area because it is a static allocation before starting the queue itself we need to specify the size fix the size so later we can't update the addresses there is a main project of implementing the queues using arrays the same car back we have seen in stacks also so that's why we will move on to the linked list now let us take the size as some 10 right next friend is equal to red is equal to minus 1 so both are equal front and drain both are equal and both are initialized to minus 1 now after every enqueue operation we need to update this 1f after every dequeue operation we need to have data friend now let us go with them so I also declare a stack I mean Q and Q are a with a size n now we will go with the enqueue function and Q function so in the n cube first we are supposed to check whether the stack is full of not if the stack is full then the insertion operation needs not be done right so we can't insert the element out of the size so we have to check the cheerful so if there is equal to is equal to size minus 1 so after every enqueue operation we are updating the mayor right so rare points the last statement of the queue so which we have discussed just now so that's why if R is equal to zero size minus one so here the size is 10 so the size of the queue is from 0 to 9 that will size minus one so if rain is pointing to the size minus 1 automatically that is a queue is full so we can't inside the add elementary to the queue so that's why if RL is equal to is equal to size minus 1 simply read f war flow condition Oh similarly can bring the queue is full yes so if it is not size minus 1 then we can insert an element into the queue so fast rear element scanf % HD ampersand in here again we need to check one more condition that if rent is also minus 1 so because we have to update the front also so initially this is a queue this is the queue so friend and then minus 1 so if you insert an element here let it be 10 rain will be updated so rail will be pointing here but still the front is equal to minus 1 so Fred should point at the top element I mean first element so if we have to check that condition if friend is equal to is equal to minus 1 what we have to do just SNe to 0 then friend is equal to 0 right so if it is minus 1 is equal to 0 then after completion of this condition before inserting what we have to do see so here an element temp is taken from the keyboard and now friend is equal to equal to also friend is equal to zero so this is the zero position one two three and four now fans day is pointing to here Frank is pointing towards here this position right yes is now rate is equal to minus one so in order to insert an element we need to update that name right so open the place through the breeze so just to avoid the confusion here we we need to update the rave so where is updated so rave minus one so where we were updated so the rail value will be here so that means pointing towards zero so at the zero we have to insert arrangement so Q of L is equal to n so now the N will be here now the 10 will be here that's all close close one section if you call this enqueue operation see this is equal to size minus one now ray is pointing towards zero zero is equal to nine funds it comes here it reads the element if it reads the twenty then if friend is equal to minus one that is not equal to minus 1 because friend is already initialized to zero that we see is pointing towards the first element that is why it wishin is passed this is simple if condition right then recklessness zero now array will be pointing to one right then q o3 r where q of Ray is equal to Q of 0 so 33 of 1 is equal to 20 so party will be inserted here so this is a great position and again if you again you want to insert another element then again you call the NQ operation so rate is equal to size minus 1 is equal to is equal to size minus one so condition false yes condition it reads another element so 30 so friend is equal to minus one threat is our but is not equal to minus 1 so condition fails and out from the conditional statement read plus plus now then will be pointing towards to Q of red is equal to element so here the 30 will be printing similarly for me in the next all right in the next one the party will forget until this condition becomes false sorry this condition becomes true if it is true we can't insert an element into the Q so this is how we can insert an element into the cube so insertion always done after a rain okay that's we should remember so here there will be two positions front and rain and all the instructions will be done at the very end so first we have to update the rain and we need to insert the element into the rain now let us move with dequeue operation that is how to delete an element from the queue so this insertion operation so very simple thing DQ so this sort of function let us write the function itself right so DQ means deleting an element from the queue so we we have to do it from where we have to delete so that all the elements will be deleted from the fronton right friend thing so before deletion we need to check the condition that is under flow condition if rain is equal to is equal to minus 1 if rain is equal to is equal to minus 1 simply that implies our queue is an empty cube because if you want to insert an element first compass re we have to increment this rare so if rain is equal to is equal to minus 1 that implies our queue is empty that means lower elements available in the work you so simply we can write pre them on the flow or simply we can write empty queue empty queue yes so if it is not an empty queue then what you have to do it the first element should be deleted so element is equal to Q of friend so M will be assigned to element so we can write the printer percentage D is delete table percentage is what is the person density anybody then we need to obtain the friend because we have deleted M so 20 is the first statement so we have deleted the 10 so deletion means just we are pointing towards another one right so the memory will be same we are not freeing the memory right so 20 is now the first element of the queue is 20 so we need to build a friend so friend plus plus friend plushness so friend will be moved to here right ok then close hope your muscle every time the mentor is deleted from the front end and we are incrementing the friend and here we can also check the condition there is equal to zero to minus one or friend is greater than 3 this is also one condition so friend if friend is greater than red so deleting one by one element the find will be pointing towards here so 4 is greater than 3 that means no more elements are there in evening in queue so that is why we can take this condition also friend greater than array Frank go to them so if anyone's condition is true automatically that indicates this is under flow that will secure is empty so this is how we can delete an element from the queue see range is equal to minus 1 false so initially let us let us see here under so else element is equal to your friend so 10 will be removed and fine plus this now friend will be pointing towards 20 again if you again apply this DQ operation array is equal to minus 1 pulse so 1 is greater than 4 false so total false yes what will be executed so if it is equal to your friend so 20 will be deleted and find restless so friend will be now here itself right there is equal to minus 1 false finding better than 2 greater than 3 false so element is equal to cure friend that it will be deleted in here and friend plus place now friend is pointing towards the same thing rate is equal to minus 1 false try to greater than 3 so 3 rather than 3 Fox so under flow again false so the else part will be executed that 40 will be deleted and fretless place so no friend is pointing towards 4 now again if you again if your applying this I mean if you are deleting this DTO operation you are applying this dequeue operation and that is equal to minus one Falls and finds greater than they so for greater than three condition is true so under flow that means our Q is and D so this is how we can apply the deletion operation on Q's right so hope you understood this insertion and deletion and Q and EQ now we'll go with the third one that is a display how to display the elements of a cube that's also very simple T so this is the Ray this is the friend right now display operation so we need to display all the elements of a cube first let us check whether it is an empty cure so for that we need to check if Ray is equal to is equal to minus 1 or friend rather than Ray so this is also condition if any one of these condition is true automatically we can say it is in empty queue empty queue right yes so if it is false then we need to pray all the elements from front to where so we need to use the introduced segments for I is equal to friend simple for me I less than or equal to V I plus plus here we can print percentage D Q of I simple display function all right so is equal to friend so I is equal to 0 0 less than or equal to 3 so Q of I so Q of 0 that means my repeated next I will be limited to 1 so 1 less than or equal to 3 so Q of 1 and this 20 next is increment K so I is equal to 2 2 less than or equal to 3 so again to 0 of 2 is equal to 30 so I is equal to 3 the third next condition next increment so 3 sorry 3 less than or equal to 3 condition 2 Q of 3 is equal to 40 so I is equal to 4 so 4 less than or equal to 3 things so it comes out from the loop it comes out from the loop and the display function will be stopped here so these are the elements which are presented in the queue so 10 20 30 and 40 which are here so hope you understood this one display function write a very simple logic while implementing these cues using headings so here we draw a case we need to specify the size of the queue before starting the operations itself so later we can't change the size of the queue right so that's a worried or back in this queue implementing the implementation of queue using arrays so here the mq & DQ are the operations for insertion and deletion and Q will be done at rain DQ will be done at front end so in this session we have seen the NQ operation the king of Christmas display how to display the elements of vacuum and right so hope you understood this session so let us stop here if you are having any doubts regarding this session so feel free to post your notes in the comment section so that definitely clarify all your don'ts and if you really understood my sessions like my sessions shave my sessions with your friends and don't forget to subscribe to our channel keep following thanks for listening thank you
Info
Channel: Sundeep Saradhi Kanthety
Views: 71,100
Rating: 4.9506903 out of 5
Keywords: sundeep, saradhi, kanthety, data structures, fundamentals, basics, arrays, lists, linked lists, stacks, queues, trees, graphs, searching, sorting, insertion, deletion, operations on data structures, primitive, non primitive, linear, non linear, binary trees, binary search trees, display, overflow, underflow, index, array size, front end, rear end, queue using arrays, enqueue, dequeue, queue empty, queue full, implementation of queue
Id: O3y6A7WLlao
Channel Id: undefined
Length: 28min 9sec (1689 seconds)
Published: Sat Jun 08 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.