STACK 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 covered the data structure concept that is a linked list so in that we are seeing the two operations the insertion and deletion in all the three cases so for signaling list double interest and circle of interest so in today's session let us see about another linear data structure stacks so first point it is a linear data structure so stack is a linear data structure so in the data structure means all the elements are arranged in a sequential manner so elements are arranged in sequential right then it follows the last team first out mechanism it follows L I have oak leaf oak that is last in first out mechanism so here the name itself indicates whatever the elements whatever the element inserted into that stack and last that should be the mood first so last in first out whatever the element is inserted at the last that should be removed first right last in first on the best example for this tank is arranging the plates in a tree plates in a tree or coins arranging the coins arranging the coins or books so what are you clearly anything we can consider it as an example right so because so consider this plates arrange the clinks so if you consider a tray and if you start inserting the plates so the first plate will be here the first plate he will be here and the second place will be on the above the first plate so the second plate will be placed on the elbow of the first ability third plate will be inserted here fourth grade will be inserted here one two three right now it take insists of four plates so if you want to remove the plate which plate should be removed first the fourth plate should be remove first we can't remove the first plate the fourth plate should should be removed first that means whatever the element which is inserted last because in these four plates what is the element which is inserted in the last that is fourth plate for so that should be out that means that element should be report first so see here these it is having two operations mainly one is push operation another one is pop operation push and pop two operations push operation means inserting and admit inserting an image pop operation means deleting an inmate right right inserting an element into the stack visiting a little bit from the stack there is oh these two operations can be applied for stack next here both the insertion and the deletion can be performed on only one side that is at the top of the stack so if you want to insert an element to the existing stack we have to place the element at the top of the stack and if you want to delete the element you have to delete the element from the top of the stack so either insertion or the division both operations can be performed on only a single set that is top so we can use it tup top is it top off step so where can we perform both push and pop operations on the top of the stack both push it pop and pop operations can be performed initially initially then if there are no elements in the stack this top is equal to minus 1 if the top is equal to minus 1 initialization right initial value of the top so if the top is minus 1 that implies the stack is empty no element is present in the stack and if you are trying to insert an element into the stack first we have to obtain the top and then we have to insert the image similarly if you want to delete the element from the stack you have to delete the element from the stack and if you have to update the top right you have to decrement the top right so now this stack can be implemented in two ways all right here so implemented in two lists what are they first one using arrays second one using linker this right so two ways we can implement this stack by using the arrays and linkedlist so in today's session we will see the operations of push and pop so these will not change okay the only the implementation will be change but the everything is same for both the cases if you are implementing stack using arrays the same thing will be applied if you are implementing the stack using linkedlist the same things will be applied right so the only difference is implementation part so in the address we have we need to take the maximum number of elements prior to the insertion or deletion right we need we need to take the size of the end so coming to the linked list we need not take the size of the atom first because the size of the stack because then we will use the dynamic memory allocation right here we are using the static memory right that means before before starting the stack itself before inserting the elemental stack itself we have to decide the size of the stack okay then let us see the push and pop operations of stack which is implemented by using hex so hope you understood this one right so it's a linear data structure it follows a little format the mechanism and two operations will be there push n pop right there are two more concepts just trying to explain and then we will go with the operations see so by implementing the stack we need to verify two conditions there is lower flow condition and under flow condition so when implementing the stack either using errors or a linkedlist we made to check these two conditions water flow condition and under flow condition where this water flow condition will occur when you are trying to insert an element into the stack already which is having the maximum element then this water flow will be occurred for example instead consists of finally means five elements if you have time to insert sixth element if you are trying to insert sixth element into the stack which can hold only five elements then this overflow condition will be occurred right that means this will be used in push operation this will be used in push operation that means we need to check the condition while performing the push operation so inserting an element to the stack which is already having maximum inmates so this occurs when we are inserting an element to the stack which is already having the maximum elements then I am a flow condition and a flow condition means the reverse that means if you are trying to delete an element from the empty stack so there is there are no elements in the stack but still you are supposed to remove an element from the stack right so then this condition will be occurred that means deleting an element from the stack from the empty step simply we can write from the young P step then automatically this condition will be occurred under flow so that means this condition is to be check that form operation in the push operation we have to check this one and the form operation will have to check this one right now let us go with the direct functions push and pop right so implementation of a stack using Harris so we will go with the Harris right so in this we need to give the size of the stack before starting the insertion right so static memory allocation so we need to give the size of the let it be 10 face maximum 10 elements can be inserted and initially the power is equal to minus 1 that means let us take here right so let us consider here au is equal to minus 1 so coming to the push push operation first we need to breathe element because this is a insertion push mix insertion so read an element so scanf % HD that is really a numerator element right so we are reading an element so we need to insert this element into the step so we need to declare the stack also we need to declare stack on source tap it's an area it's an array of size M stack is an array of size iam 10 so in the push operation we need to check the overflow condition so if top is equal to is equal to size minus 1 so here pop is equal to minus 1 initially before inserting arrangement size is 10 so sizes 10 means the index values will be from 0 to 9 that means size minus 1 so for every insertion we need to update the top of the step so if the top of the stack is equal to size minus 1 that that implies the complete elements are inserted into the stack the stack is full so if this condition is true we can print lower float or simply we can print stack is full yes yes yes that means if this condition is filled that means there is a vacancy in the stack to hold the amen then we need to increment the top first because initially top is minus 1 and after inserting the element out will be at 0 so first we have to update the top so top press press so we need to update the top so then the top will be updated to the next position then we have to insert the element and the position so here if you let us take the position up plus plus pump is equal to 0 so here it is top is equal to 0 now we need to store the element in this position so then stack off top is equal to any main stack of top let my stack of 0 is equal treatment so let it be 10 the 10 will be inserted here right so close the braces so this is a simple push operation coming to the second iteration right so again if you if you apply this push operation again it will read an element it will be 20 pump is equal to size minus 1 top is 0 is equal to is equal to line fails again power plus plus no top is equal to 1 right stack of top stack of 1 is equal to 20 so 20 will be inserted here again if you again called the push operation 30 so Tom means 1 1 is equal to is equal to 9 again false so again else condition a plus plus initially now drop is equal to 1 power plus plus means out is equal to 2 so obviously top stack of 2 is equal to element 30 so 30 will be inserted here ok so you understood this one right so initially first we have to check the power flow condition if it varies then we have to increment the top and then we have to insert the element into the stack and the only disadvantage of this implementation is before starting the implementation we need to specify the size of the stack so beyond that size an element cannot be inserted into the stand that is the main topic of implementing the stacks using errors right so coming to the linked list then we will use the dynamic memory allocation so there is no restriction to the size of the step right now so hope you understood this push operation now we will move with the pop operation see after insertion inserting the elements the pop is I send you to to write no provision or piece deleting element so before deleting we need to check the under flow condition that means whether we are supposed to trying to delete the element from the empty stack or not so for that we need to check the under flow condition so if is equal to is equal to minus 1 so obviously if you are not inserting any element then the top is always initialized to minus one if you insert an element automatically the top must be get updated so top will not be equal to minus 1 right so if a physical is equal to minus 1 we can say that pre there underflow on a phone so if it fails if it fails that means else condition if it fails then we have to remove the element which is available at the top right so here we have to remove the 13 we cannot remove the 20 and 10 until you remove that 4:30 right so that's why yes what is the element to be removed so element is equal to stack off oh that means here the element removing element is stack of doubt that will take off to step of two means thank you so that angle will be removed and after removing the element they start to should be updated so top - - so automatically tom is referred to he after applying this pop operation out will be 1 right that's all this is a simple pop operation ok a simple pop operation again no tom is equal to 1 again repeat if you want to again remove the pop I mean if you again apply the power operation then F is equal to minus 1 1 is equal to is equal to minus 1 false so else element is 1 stack of 120 in the second thing if you want to again apply this pop operation 20 element will be deleted I thought - - no it will be 0 at 0 again if you apply this town if you call this pop operation next do is you put is equal to minus 1 again it is false so individual zero so event is equal to 10 and the top - - so it will be minus 1 now automatically the elements of the stack is see after playing the pot 3 times the top is equal to minus 1 again if you are applying the top I mean pop operation the pop is equal to minus 1 that is minus 1 is equal to is equal to minus 1 it's true because there is no element available in the stack so that's why it will be printed right so hope you understood this power operation also simple operations first you have to delete an element and then we have to reduce the top that means update that decrement the top next how you display the elements of a stack display function we will see the display function also if stack contain four elements so f is equal to 0 1 2 3 so f is equal to 3 now are visible to 3 now right so display function how to display the element surface step simple we need to check whether it is an mtech stack or not so if not equal to minus 1 if it is not equal to minus 1 automatically that implies there is it I mean there are elements available in step then use a for loop for I is equal to top that means 3 I greater than or equal to 0 you know than or equal to 0 anything top is equal to 0 I - - I - - every time we have to be given because we are assigning I with the top element here we can print the a print printer percentage D step off I stack off i close yes if this condition fails not equal to minus 1 it is equal to minus we can simply write printer we can simply write empty SCAP empty step okay let us say let us trace it so initially here pop is equal to 3 now naught is equal to 3 in the first iteration two top three top not equal to minus one right top not equal to minus one so three not equal to minus 1/2 so far I is equal to top I is equal to three first iteration I greater than or equal to 0 so 3 greater than or equal to 0 condition 2 then stack of white it so stack off three will be drink it that is 40 will be printed next I minus minus my Y is equal to 2 then again 2 greater than or equal to 0-2 then pre stack of 2 which is 30 which is 30 and n I disappointed now I is equal to 1 1 greater than or equal to 0 you need to print stack of 1 which is funding right next i DQ my ID is decremented now I is equal to 0 so 0 greater than or equal to 0 here we are using the is equal to also so that's why I'm still they condition is true so stack is stack of 0 that is ten will be printed again I - - is equal to minus 1 minus 1 greater than or equal to 0 condition chains come out from the and it's the display function right so hope you understood this one simple logic for a push operation pop and display operation right so so implementing the stack using arrays we will be having two operations push and pop in such for insertion push operation for deletion pop operation so while inserting an element into the stand before we have to check the water flow condition and why decrementing an element from the stack we need to check the overflow underflow condition right so these three operations so let us stop here so the main drawback of using arrays is the static memory so before we have to declare the size of the array and if you want to increase the size of the array it's not possible in here and unless you use the dynamic memory allocation where we implement in linkedlist right so I hope you understood this one so stack and the operations of a stack and the implementation of a stack using hands right if you are having any doubts regarding this session feel free to post your thoughts in the comment section so that definitely I will try to clarify all your thoughts 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 thanks for listening thank you very much
Info
Channel: Sundeep Saradhi Kanthety
Views: 87,906
Rating: 4.9522567 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, stacks using arrays, push, pop, display, top of stack, overflow, underflow, index, array size, stack full, stack empty, LIFO, LAST IN FIRST OUT
Id: DKlXEAZYB7I
Channel Id: undefined
Length: 26min 22sec (1582 seconds)
Published: Fri Jun 07 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.