How to Perform Operations on Queue | by Mr. Srinivas

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to nourish technologies this is famous in the last session we discussed so completely a theoretical approach of a queue implementation now in this session we'll see how to perform operations on a queue operations means how to insert an element into queue how to delete an element from the queue and how to traverse all the elements okay see here here it is queue implementation queue implementation using static arrays heading is queue implementation using static arrays static arrays means what the fixed in size queue size is a fixed size will not change so that is what we called a static array right how to implement first declaration of a queue is very very important declaration of queue size is a fixed size suppose here here it is I am declaring 1 queue int int queue the size is a five or more clearly if you want to write with the help of preprocessor directive hash define has defined the capacity capacity is a 5 that capacity we are writing here capacity that is this is declaration so globally you have to declare a variable so then it will get memory allocation with the size is a 5 so we can insert five elements so maximum because here it is a size is a fixed size and next one two more variables we have to declare so what are the variables means if you want to insert or if you want to delete mostly queue operations we need to perform q operations we need to perform with the help of two variables friend and rail because insertion of elements into the queue is possible through rail an x1 deletion is possible through friend right physically we can say that right from the rear we are inserting the elements into the queue and from the front we are deleting the elements from the cube but programmatically how can we show by declaring variables only so here we are declaring two variables one is a int friend equals to 0 and x1 int rare equals to 0 in front equals to 0 and entry equals 0 initially front and rear both are pointing to the first location 0 and 0 ok and remember whenever front and rear is equals to 0 so that is what we called queue is empty we are not inserting any elements at so initially here it is it is pointing 0 and the red is also pointing to 0th row tation okay so based on these variables these variables how to perform insertion operation a deletion operation and next one display operation observe here suppose this is a queue the size is a 5 1 2 3 4 5 and index index initially the front value front value is 0 and rare value is a 0 now all the 3 functions I am writing side by side here it is first one is a insert function how do we insert an element into the queue insert function second one is a how to delete an element from the queue delete and next one how to traverse elements traverse is nothing but displaying the elements so total three functions all the three will discuss so first of all if you want to insert the elements insert and delete both of your writing suppose if you want to insert elements into the queue we need to check Q is full or not for insertion because if Q is already full you cannot insert the elements in the same way suppose if you want to delete the elements then we need to check q is empty or not because if q is empty we cannot delete the elements from the q okay see here it is we are checking if if on some condition to declare local variables leave some space if based on some condition we do not know condition as of now here it is a printf printf q is full the situation will come but when it will come we do not know that we will see but we are writing the condition else we are inserting the elements in the same way in case of deletion also if if now on some condition we need to check printf Q is empty so you cannot perform a deletion operation on the q we cannot perform deletion operation because Q is empty no element is there to delete servants are very clear observe here whenever front value is equals to rare because front T equals to zero and there equals to zero right so then simply here we are writing Q is empty whenever front equals to rare Q is Mt q is Mt and next one in case of a traverse also I want to display the elements same story first you need to check right q contains elements or not if suppose no elements simply we are printing queue is empty queue is empty this is right insert delete a traverse but here sir what is the condition that you will understand slowly in the else part else part suppose if space is there initially front is pointing to the zeroth location and there is also pointing to zero zero location front and rear now I want to insert the elements so we are asking here it is suppose we are declaring one variable element just read that element directly I am writing actually here we have to write a printf and scanf right we are reading that element after reading that element we need to insert we need to insert that element into this location sir which variable we have to use sir front is also pointing to the first location rare is also pointing to the first location now which variable we can use can I use a friend no because in algorithm strictly specified that right all the insertion operations from rare only so variable rare variable only you have to use so here that element whatever the element you read that will be stored into queue of rail that will be stored into queue of rare because Q is pointing to this array element suppose 10 consider that will be stored into rare rare value is a girona yes so that will be stored here after that we should move the rare position to insert the next element here simply rare value is increased by one rare plus place this is simply insertion next suppose they want to insert one more element they want to insert one more element then what will happen so very simple again insert function will call if condition later because some more elements are there and rare value is a1 increased now rare is pointing to second location nothing but first index number second location now we are reading the element using printf and scanf functions next element will be collected into queue of rare queue of rare means what now this is the location next rare value will be increased rare value become 2 next rare is pointing to this location location third element insertion and here it is a it will store 30 next rare value become 3 next rare pointing to fourth location next element insertion 40 rare value become 4 next 5th element insertion rare is pointing to next location 5th element and the rare become 5 rare become 5 so whenever rare is of 5 rare is pointing to the next location which is not present rare values of 5 so q is full whenever rare value is equals to 5 5 is nothing but a capacity whenever rare is equals to capacity capacity of your q of the queue so then we can say that Q is full we can say that Q is full this is simply insertion condition we can keep on inserting so finally write the rare value is equals to the capacity by that time if block will execute not else block because so there is no place to insert the next element 6th element insertion there is no space okay so this is inserting an element into the queue so finally rare value is a 5 rare value is a 5 now there is no option to insert so we are deleting just consider we are deleting or we can display also so first we will see a Traverse Traverse very simple if you want to display all the elements first we have to check elements are present or not elements are present or not we have to check suppose here it is if front is equals to rare we already discussed that if both are equal right in the initial stage whenever you start whenever you declare the variables by the time front value and rare value both are same so by the time no elements inside the queue that we can say that Q is empty now in the else block we are displaying all the elements displaying and here it is we are just giving the message printf Q elements are Q elements so many elements will be present then how can we print just use the for loop I value starts with a 0 from the first location or more clearly from the front location because front is always pointing to the zeroth location friend I is less than rare not less than or equals to sir why see suppose here it is elements up to fourth location rare is pointing to fifth location so we cannot access the element which is inside the rare location so less than rare up to here so less than rare and I plus plus I plus plus we are printing the elements printf percentage D slash in and here it is simply Q of I the value here printing simple we should move right so we should traverse or simply we should iterate from first location to nothing but from front location to less than rare location how many elements are there all the elements we are displaying simple thing so this is how to display the elements directly you can tell first you have to check right the queue is empty or not if Q is empty you cannot perform any operation okay may we cannot display the elements which are inside the cube now the last operation is a deletion operation deletion so deletion for example if front is equals to rear queue is empty so by the time you cannot delete or else or else so what we have to perform is first directly we are deleting this element we are deleting ten we are printing directly printf deleted item is percentage D that is q of friend that is Q of friend so what is that q of friend Q value Q is pointing to this location next Q of friend means front is a girona front is a zero front always pointing to zero only we cannot disturb the location right front variable no need to disturb from starting to ending it is always pointing to zeroth location only so zeroth location so here it is a ten will be deleted deletion it is impossible to delete the information we have to overwrite the information that is so after saying that deleted that value 10 next what we have to do means we need to shift all the elements we need to shift all the elements how to shift same story using for loop using for loop we should start from the first location first location means what from front plus one location here it is here it is I value starts from one I is less than how many locations simply less than rare less than rare I plus plus I plus plus then how can we shift simply Q of Q of I value will be stored into Q of I minus 1 or more clearly sir I will start with a 0 only then no problem then Q of I plus 1 value we'll be stored into q of i q-- of i so that means this is Q that means suppose initially I valued 0 Q of 0 plus 1 means what 1 the 1 value will be stored into Q of 0 that is a 20 shifted to here 20 next I value will increase I will you become 1 here it is 1 is less than rare rare value is a 5 and next one Q of I plus 1 I plus 1 value means what 1 plus 1 is a 2 to value will be stored into Q of 140 will come to here 30 right next I will you become 2 so 3 value will come to here 40 next I value 450 come to here 50 so come to here next I value 5 sorry I value 4 but here it is a 4 is less than rare value 5 so whenever we are writing like this this location will come to here this value because already 50 is shifted no need to shift this one up to 4 only less than rare right here it is a rare value 5 so this is less than rare minus one rare minus 1 now 4 is less than 4 condition false because no need to shift the element 50 simple thing so then what we have to do very simple finally rare values of 5 1 element deleted means rare value should be decreased by 1 rare is not pointing to the fifth location rare should point into the fourth location simply what we have to write rare - - simple after insertion rare plus plus after deletion rare - - and very very important concept this for loop shifting of elements very very important generally the people saying that after deleting the element from the front they will move the front value impossible we should not suppose to move the front you they should not suppose to move the front value then what we have to do what we have to do means always we have to shift the elements from first location to rare location all the elements we have to shift by one location towards a forward direction right words forward direction this is how to perform insertion operation deletion operation and a traverse operation in a queue okay so this is all about a queue abstract data type or queue data structure abstract data type or queue data structure okay so all the operations how to perform the complete explanation about queue data structure okay so thank you thank you for watching for more videos please subscribe to nourish 80 channel thank you
Info
Channel: Naresh i Technologies
Views: 227,440
Rating: 4.9199295 out of 5
Keywords: Srinivas, Balu, Data Structures, Learn Data Structures, Data Structures Demo, Online Data Structures Training, Hands on Data Structures Training, Data Structures Overview, Naresh IT
Id: EXsR7HXlGLw
Channel Id: undefined
Length: 18min 57sec (1137 seconds)
Published: Thu Oct 06 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.