Creation of Stack in DS | Data Structures Tutorial | Mr. Srinivas

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to nourish technologies this is fin wash in the last session we discussed the basic points of stack so in this session we are going to discuss how to implement a stack using static erase so nothing but with the constant size how can we perform all the stack operations like a creation write storing the information removing the information printing the information all these things okay so already we discussed in the stack abstract type so many specifications they have given read all these specifications now we are implementing with logics using C language syntax okay first so first of all creation of a stack creation of stack so stack creation is very easy stack relation so initially suppose we are declaring capacity one variable capacity any capacity you can give so that is 5 that is 5 so in the main method either locally you can declare or globally we can declare mostly write of variables we are declaring globally only the reason we are accessing the stack from different functions in the program so this is a global variable capacity and next one so one more variable is a integer stack integer stack and what is the size what is the size means suppose I am giving the capacity I am giving capacity sir here doubt in C language whenever you declare a variable like this it is impossible to pass another variable in the form of square brackets right in a size you cannot specify ordinary variable impossible case sir then when it is possible when it is possible means instead of declaring ordinary variables we have to declare a macro so macro is nothing but a constant so how can we define constants using a preprocessor directives so using preprocessor directives how can we declare a constant means hash define hash define capacity constant variables mostly we are using we are representing with the help of capital letters only capacity v there is no space there is no semicolon there is no equals to symbol this is identity and this is the value right and here so now we need to write all our capital letters because C language is a case sensitive language capacity capacity right here so hash define is a preprocessor directive so which is used to create a constant nothing but a macro this is a macro name and this is the macro value macro name and macro value right only macros we can pass whenever we are declaring ordinary variables we can pass macro wiser what is the reason here right in place of a capacity the five will be substitute at the time of pre-processing phase pre-processing phase pre-processing phase means that is before compilation before compilation of application see application pre-processing phase will be present right in a pre-processing phase the value of this macro will be substituted here then by the time right at the time of execution memory will be allocated to this stack ok so this is how to declare a stack variable right with the help of a constant size constant is nothing but a write preprocessor macro preprocessor macro then how the memory will be allocated just we are writing in the form of a stack we are writing in the form of stack right so what is the size size is a5 size is a5 right 0 1 2 3 4 and remember two things first one is the capacity over capital letters sorry capacity is a5 capacity is a5 so index 0 to 4 and next array variable always holds a base address the base address suppose two zero four six two zero four eight two zero five zero two zero five two two zero five four so here it is stack is holding the base address the base address is a two zero four six is pointing to this stack and one more we discussed one more we discussed in the introduction of a stack right in the stack so whatever the operation you want to perform write either insertion of elements if you want to push any element deletion of elements if you want to pop any element all the operations you should perform from the top of the stack sir physically nothing but generally so we can say that so we are performing means what we are inserting the elements from the top we are deleting the elements from the top is okay physically we can show like this sir programmatically how can you perform all these things very simple you have to declare one more variable in teaser one more variable you have to declare so what is that variable name top sir what is the value initial value equals to minus one initial value equals to minus 1 so here op equals to top equals to minus 1 so remember variables capacity is a five top variable value is a minus one service or minus one because if it is a zero it is pointing to the first location index as of now top variable is not pointing to any index so the value is a minus one so this is just a creation of a stack simply declaration of an array with the name stack with a constant capacity constant capacity is once again preprocessor directive it is a macro this is creation okay next we will see how to insert elements into the stack nothing but how to push the elements into the stack how to insert the elements see for example method is a push method push method it is not returning anything to anyone but what element you want to push that it is taking as an argument if you want to push an element into the stack what element you want to push that you have to pass but remember so whatever the element they are passing that we cannot store directly into the stack reason because here stack is a fixed size right it is a static memory allocation and of course it is a dynamic memory memory allocation also so first we have to check whether there is a chance of inserting an element into stack or not if stack is a full right or so in dynamic memory allocation there is no memory right to store the next element into the stack so that will fail is a failed case so first we are checking if if a stack is full sir how can we check sir stack is full for that logic we are writing in another function sir what is that function function is a is full function is full inside the if block we are calling one function is full function so logic for useful function will see so what it will do means useful function return type is the integer if stack is a full it will return one right if stack is not full it will return zero okay on success it returns a one on failure it returns a zero so like that you have to write logic I will show you very clearly okay so here how is full method will be method is a is full method is full written type is a integer if a stack is full the condition full condition I will show you right if the stack is a full it returns one if condition is a true or else it will return false it will return zero the conditions will discuss because according to operations if I explain you will get more clarity on this as of now what is the condition put aside now observe so first of all stack is not full stack is empty only stack is empty so whenever you call is full it returns a 0 so if block fails if block fails else block executes else block in the else block what we have to write in the else block we are writing insertion logic so what is that insertion element we are inserting element where we have to insert in the first location so how to insert elements insertion is possible with the help of top element only but top is not pointing to zero actually we have to insert element into zeroth rotation but the top is what is a minus one so first increase the top value here first what you have to perform top less plus increase the top value observe here the top value become zero top value become zero now it is pointing top so now we are storing element element we are storing into which location into the zeroth location of a stack array right so here into stack of 0 is nothing but top into the location sir element is what suppose consider element is at 10 10 will be inserted 10 will be inserted so next one so clearly we are printing one message printf printf element inserted or element pushed into the stack right here it is so percentage D means what element this message will print clearly on the screen so that end user can understand okay so whatever the element I just want to insert so that will be pushed to successfully pushed onto the stack successfully so that is insertion of element insertion and next one right this is a first element insertion next they are calling this push method once again second time they want to insert one more element 20 so same story again else block will execute because useful condition fail because the stack is still having some of the locations to insert the elements chances are there so here it is a topless place so top value become 1 element will be stored into 20 next I want to insert one more element for example write 30 top value 2 element is a third next top value three-element is a 40 top value for element is a 50 so now no chance of inserting the elements no chance so so by this time whenever they are trying to call push function once again now useful method should return the value 1 what is the condition based on which condition very simple so top value is a for whenever top equals to capacity minus one top equals to capacity minus one reason capacity is a five five minus one is a four top value equals to four whenever a top value reaches four so then automatically stack is full yes now top is a four four equals to four condition true then it will return one so by this time it is running full then we are printing the message printf printf stack is full message we are passing clearly stack is full message okay so this is the logic that how to insert the element into the stack how to insert the element into the stack so first whenever we are inserting the element first you have to pass that element directly we cannot insert first we have to check whether the stack is full or not directly we cannot write the logic here so we need to take the help of another function so what is the function is full function useful function will return the write the stack is a full or not if stack is full it written one if stack is empty nothing but the sum of the locations are there to insert the elements it returns zero that we are collecting in the else block here it is a topless place yes we are increasing the top value every time after increasing the top value element we are inserting into stack of top sir wiser we are using top means so this is the thing that we are performing all the operations into the stack with the help of top here also to check whether the stack is a full or not we are taking the help of top this is representing we are performing all the stack operations with the help of top from the top so that is why so mostly we have to concentrate on a top variable every logic you should write with the help of a top variable okay so this is how to insert elements into the stack so now we will see how to pop the elements nothing but how to delete the elements from the stack so as we are writing stack with some elements with some elements suppose elements are 10 20 30 40 50 so we know that capacity capacity is a 5 and top is pointing to 4 so initially top is pointing to 0 next 1 2 3 4 now top is pointing to 4 and next here it is a base address two zero four six two zero four eight two zero five zero two zero five two two zero five four two zero five four here it is the base addresses and the base address of a block will be stored in two variable stack so that stack is pointing to that array variable so we can call it as a stack okay so these are the things already we discussed now how to pop the element from the stack so method we are writing method name is a pop so this method should return in in teaser this method should return an integer here it is so return type is integer and it is not taking anything sir wiser why pop method is not taking any element means so whenever you call the pop method always it will delete the top element of the stack we cannot delete a particular element from the stack because always a last in first out what you inserted last so that will come out first simple thing so so that we have to pop so first we are declaring one element eld sir can I pop the element directly impossible right reason first elements are present or not in the stack we have to check suppose if a stack is empty then we cannot do anything so if if we have to call a method is empty is empty right so what is empty method will do is empty method will return one if there is no element in the stack right if it returns a zero that means a stack contains some of the elements if there are no elements in the stack it is returning one if elements are there in the stack it returns zero so for example if elements are not present then it returns one if one condition true and it will execute but as of now according to our Strack elements are there right already elements are there five elements are then right and here it is a top value is a four top is pointing to this fourth location so here we have to pop nothing but we have to remove the top element only so top element is a tea okay so here else block execute else block sir can you write the logic fir is empty function yes parallely I will explain is empty function also is empty function is empty and here also condition on some condition if else we will write if else is empty method is returning 1 or 0 so 1 or 0 that is integer just consider suppose if it is empty if this condition is a true it will return 1 if this condition is the false it will return 0 sir what is this condition we will see whenever it reaches okay now as of now szostak already having some elements so whenever you call is empty method else block execute it will return 0 so 0 will be collected here if 0 condition false come to this else block and here we need to delete the element so stack of top top element we have to collect stack of top element we are collecting into LG element and next after removing this one after removing this one top should point into the next location top should pointing to next location after deleting this element so here right after deleting this one the top - - we have to do OP - - and finally we have to return top we have to return top element that's it either if block execute or else block xq here it is a return function return statement will return top element right for example if top element is not there top element right element is not present then if block will execute it will return it will return zero this consider so nothing but element equals to zero you can assign or directly you can return zero directly okay so here it is a pop element means always returns the top element so second time we are calling second time also elements are present so we are deleting this 140 will written and top will come to here next to 30 we written top come to here 20 we written top here 10 will return top become -1 top value become minus 1 so whenever top value is equals to minus 1 so what is the meaning no elements in the stack see that means whenever top equals to minus 1 in the beginning also at the Declaration of top value also by the time top value is equals to minus 1 top value is equals to minus 1 so by the time right so it is returning at a value is empty function returning the value 1 because is empty means yes it is empty so whenever you are calling is empty if top is equals to minus 1 it is returning 1 1 come to here if one condition true come inside right directly it is returning this 0 so 0 where you are collecting 0 we are collecting right in the calling method from where a pop method has called there we collected suppose if it is returning the top value there we are printing the top value suppose if it is printing 0 so there if the return value is a 0 so there we are printing the message stack is empty messes or else the return value we will print so this is right deleting the element from the stack nothing but pop the element from the stack two methods we are using one is a pop method and next method we are using is empty method we are using is empty okay and the next method we'll see so what is the peak method so what is the peak method means right it will return the top element but it will not remove the top element simple peak method so what is a peak method means it will return it will return the top element but the element will not be removed for example here on the stack on the stack suppose elements are 10 20 30 40 only present so top is pointing to 3 top is pointing to 3 because 4 elements are the capacity as usual 5 5 and the stack is pointing to this one now so peak method means so as usual first we are checking if stack is empty or not so same story if stack is the Mt it will return 0 it will return 0 return type is a in teaser is empty means what same function just now we discussed write if there are no elements in the stack it will return 1 right if already elements are there it will return 0 if stack is empty it will be 1 if 1 condition true it will return 0 nothing but peak method is not returning any element because if return value 0 in the calling function we will write there are no elements in the stack to print or else or else so directly it will return the top element stack of top but remember one more thing here in the last program nothing but in the pop after returning a stack of top we are doing stop - - I think but it is pointing to the next location so here no need to write this top - - because after returning the top element peak element after returning returning still the control pointing to the same location it is not pointing to the next location understand the difference between peek and pop peek means it will return the top element but not removes pop element means what it will return the top element but removes so here it is one more thing that we are not getting is what so you are saying the data is deleting sir where you are deleting the data how the data will be deleted and one more thing in any programming language you are deleting the information from the memory means you are just make the pointer which is not pointing to the location that is it except that except that it is impossible eat the data which is inside the memory location impossible case there is no such type of function to delete the information which is inside a memory location the people saying that deleting deleting deleting there is no question of deleting only just a pointer which is not pointing to location if you write a top - - top - - means what top is start pointing to this location sir then what about this location it is there still data will be there that is working like a garbage value next time suppose if you are inserting one more element then by the time the element will be over right so that will be rewrite with another value only rewriting possibility is there there it is impossible to delete the memory location but it is impossible to delete the data which is inside the memory location only you can replace that information that is only possible okay right so here just we are not doing top - - here in a pop method top - - operation peek method means what it is returning stack top of the stack but it is still pointing to the same location only okay so this is the peak method now the last method lost functionality of a stacked abstract data type so what is that how to traverse the elements sir what is traversing how to print all the elements of the stack is nothing but a traversal how to traverse all the elements of the stack so function name we are writing while traverse it is printing directly traversing means for just displaying the elements displaying so first as usual we are checking if stack is empty or not same story if stack is empty directly we are printing messes no elements in the stack no elements in the stack for example stack is having some of the elements for example here it is a top value is a 3 stack and denser four elements so then you have to traverse all the elements two options either you can traverse from 10 to 40 or 42 10 that is your interest simply using for loop printf stack elements are for loop I value starts with a 0 to I is less than or equals to top i plus plus i plus plus and we are printing printf percentage D slash in simply stack of I because I value is iterating from 0 to up to top location top location is a 40 okay so here it is I valued zero all right so zero is less than or equals to top values what 3 so 0 2 3 that we are collecting 10 20 30 40 all the elements here just declare the variable I I we are declaring there or here also you can declare no problem okay so this is so completely about all the operations of a stack in the next session we will see how to implement practically I will show you the program how to implement a stack nothing but just all these logics so we are writing into a single program then you will understand right how to perform stack operations and how to combine all these functions into one place okay so thank you for one so thank you for watching for more videos please log in to nourish id channel thank you
Info
Channel: Naresh i Technologies
Views: 361,952
Rating: undefined out of 5
Keywords: Srinivas, Naresh IT, Hands on Data Structures Training, Online Data Structures Training, Data Structures Tutorial Videos, Data Structures Interview Questions, Creation, Stack, Creation of Stack, implementation of stack, creation of stack using linked list, Data Structures interview questions, c language tutorial videos, similar data type, stack in ds, ds tutorial for beginners, stack in data structure, ds interview questions, Data Structures Tutorial for beginners
Id: Y1vSGA0jm5E
Channel Id: undefined
Length: 32min 22sec (1942 seconds)
Published: Fri Sep 23 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.