Implementing a stack in C

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's video i'm going to take a look at how to actually implement a stack in c it's actually very simple and we're going to do it is first start with a simple array of let's say integrals so to start off we're going to actually define our array here let's say in stack and i'm gonna store maximum 256 integrals you can change this however you want i like to 56 because it's a power of two uh then we need a count so we need a way to count however many elements are in the stack so i'm going to say here count and i'm going to initialize it to zero because there are no elements in the in the stack in the beginning so first thing for us let's actually implement the push operation here we're gonna have void push and we're just gonna give it an x variable let's say and this push operation what it's going to do is going to find the first slot available inside the array and add the elements there now how do we find the first slot available inside the array suppose this is our array and let's say we have uh these elements inside of it right we have one two three and five and the count is going to be four right well we know that at index zero there's a number so we cannot add it here we we know that in x1 there's something that index 2 or something at index 3 or something but at index 4 there's nothing in fact we want to add it at index 4 just like so um and it just so happens that the count is going to always be equal to this index that we need to add to because uh count minus 1 is really the last element that we have added to this array that means that count is going to be the next free slot i'm going to add x here to do so inside the code all we have to say is stack of count equals x and this is going to work even if the array is empty simply because count is going to be zero it can't be zero you're just gonna add it to count of zero which is the first index and then of course when i have to increment count and really that's it for pushing elements into the stack very very basic right now for taking elements from the stack what we have to do is create a function really say pop and that's going to return an integral not taking an integral like we did with the one up top and well to taking an element from the stack we need to think about the order these elements were put in place remember with q link up top if you haven't checked the video about it uh we took elements from here from the left simply because they are the first one to be added right we first added one then you add two and you have three five and then x right and uh you would take this one inside a from a queue because it was the first one at it but with a stack it's different we take in the last element that was added so instead of taking the first one we're taking the last one and just so happens that the last one is the one that we just added right so we have to take in the element that is here but the count did actually change right so once we added x whatever value we have here to the stack the count is now five right so we're gonna have to take the element at a stack of count minus one because that is the index here so i'm gonna say here uh int result equals stack of count minus one simple as that and then once i'm done i'm gonna decrement count and also return our our res here and now we can actually do some operations with these uh with this stack we can say let's push one and then two three and five like we did in the example up top and let's say we have a for loops and i will say i equals zero i less than four we have only four elements and i'm going to print f percent d space i'm going to print f the return value of pop right and in this order we should see that 5 is going to be the first one printed because the 5 was the last one to enter the stack so if i try to launch this you will notice that i get five three two one instead of one two three five the way we got it with the q and similarly like we did with the queue it will be nice to add some conditions here so if uh count is already 56 the number here let's be added as the number of slots we have in the stack then we cannot add anything anymore so i'm gonna just uh print f to the standard error saying that uh there's no space in the stack and i'm gonna return out of that function and similarly in the pop function i'm gonna say if count is actually zero then there's nothing to take from the stack right there's no elements so we can simply say fprintf to standard error output and say uh nothing to take from stack and then backslash n and we can return now here you can either exit and just crash the program all together or i can return negative one but that only works if your stack has positive integrals only so uh error handling really depends on the way you want to do it here it could be done in many different ways but this is the very basic way of doing it and now if i but if i run it again it's gonna run the same way that's no problem five three two one but if i change this to a five accidentally trying to take five elements from a stage it only has four uh it's going to run and actually print on the screen nothing to take from stack and it's going to give us a negative one because that is what we got here and that's really all there is to uh a stacking saving is the easiest way to implement a stack really you only have to do an assignment and an increment heck you can even do it in one line if you wanted to you can do it like this i just think this is more readable and you only need to do this and again you can do this in one line by just decrementing it an account here as well but i do it like so because it is more readable you don't need the functions actually you can just add and subtract from the stack just using these two lines from each function and also notably you don't actually need a struct really to implement a stack or a queue you can just simply have an array that is global or maybe dynamically allocated okay i hope you got something out of this video if you do have any questions in the comments below or on our discord server again the source code for all this will be found on our website link in the description below take care bye
Info
Channel: CodeVault
Views: 6,652
Rating: 4.96 out of 5
Keywords: codevault, c (programming language)
Id: 8nix1eqoMHM
Channel Id: undefined
Length: 7min 40sec (460 seconds)
Published: Sun Jan 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.