thank you for explaining 20:30 I was always confused on other programing languages if it mattered to delete the last collection of the stack, by decrementing it, we can override the last value even if we keep the old value, it makes so much sense, so far you are the best at explaining the C language you are a life saver, thank you
That's a great question Philipp! :-) So the functions we are building are essentially a library for working with Stacks, and those library functions will be expected to perform certain functionalities, etc. The main() function is what we could call the "client code" that uses this library. So there is a separation between "implementation of the stack data structure" and "code that uses the stack data structure". We could put all these library functions into a separate file, and different programs could all use these library functions for different purposes. Our library function therefore handle the NULL value being returned in a very "generic way". Rather than exiting the program, it leaves this decision to the client code and its specific purposes/desires by just returning NULL in this case. This is similar to how when C library functions fail, they do not cause the program to exit, they typically return an error code or value and they leave it to "us" as the programmers to decide how to handle the error. The same thing is happening here, we are writing the library such that we leave it to the user of the library as to how to handle the error situation. And so they may need to check the value in main again and decide to quit (or not, or handle it some other way, etc.). Hopefully this helps! :-)
Sir, your work is outstanding. Thank you very much. I have just one comment to add. The sizeof(stack) is allocated to stack in create_stack, and this expression is used to determine the size of the pointer, not the size of the object it refers to. In my opinion, this is an error and you should allocate sizeof(*stack) since it determines the size of the object that stack references. PS: using the destroy_stack function may lead to a memory leak.
We are allocating space for a Stack struct that *stack will point to, so it needs to be sizeof(Stack). The calls to free() in destroy_stack look fine to me too.
@@PortfolioCourses stack *stack = malloc(sizeof(stack)); -----> In this context, stack is used as both a variable name and a reference to the type. The memory allocation in this line is based on the pointer's size rather than the structure (since sizeof(stack) refers to the pointer named stack). The memory allocation will be equal to the pointer's size (4 or 8 bytes), which is not sufficient for the stack structure. stack *s = malloc(sizeof(*s)); ----> This correctly allocates memory for the structure that s points to. Attempting to access stack->collection, stack->size, or stack->collection may result in undefined behavior due to writing to unallocated memory.
@@PortfolioCourses my bad, I apologize for any confusion my comment may have caused. Your tutorial was spot-on, and I appreciate the clear explanation that led me to the correct solution. Keep up the great work :)
That's a great question but it's also a very hard question to answer too. :-) Because we almost never really "master" and become "fully confident" in topics so large, there is always more to learn. And how people learn can be very personal and individual, some people learn better from books and other people from videos. I would suggest alternating between "studying" by reading books and watching videos and then practicing by trying to write code yourself to solve problems and build data structures. Alternating between "reading/watching" and "practicing" can be a best of both worlds because you kind of need both.... the reading and watching videos to help expand your knowledge, and the practicing to make sure you can actually apply it for real to solve problems. I also think it's a good idea to have one or two really good books on a topic, because those books can guide your learning, even if you end up using other resources too. The books act as a "framework" in terms of the concepts you should learn, and they often have practice exercises too. Hopefully this helps! :-)
I followed perfectly but my output always says stack size is 0, I am lost edit: i somehow missed a line of code, probably between the scrolling around between code. You have also shown me that it is good to leave spaces above and below when I am going to be adding to my main, and to go ahead and put { and } at the same time and just type between them to avoid making errors
It depends on whether you want to pop each item off to print it, or just print the items. If you want to pop all the items to print them you could keep popping as long as the stack is not empty and print out each popped value, in a loop like this: int pop_val = 0; while (!isempty(stack)) { pop(stack, &pop_val); printf("Popped Value: %d ", pop_val); } If you just want to print the items without popping them, you could have a loop with a counter variable (e.g. i) going from 0 up to stack->size, and then just use printf to stack->collection[i], something like this: for (int i = 0; i < stack->size; i++ { printf("%d ", stack->collection[i]); }
Great question Berkouk! :-) So the "capacity" is the maximum possible number of elements that can be stored in the stack, and the "size" is how many items are actually stored in the stack.
@@PortfolioCourses thank you so much for the kind reply, so from what i understand is that size acts like the index of the top element in the stack? Btw I'm in CS in university and only god knows how much i appreciate your videos thank you for everything 🙏
Yes exactly, size is letting us know where the top of the stack is. :-) And that's great to hear these videos are helpful for you, thank you for sharing that with me.
dude just *stacked* this video on the top of youtube suggestions...
thank you for explaining 20:30 I was always confused on other programing languages if it mattered to delete the last collection of the stack, by decrementing it, we can override the last value even if we keep the old value, it makes so much sense, so far you are the best at explaining the C language you are a life saver, thank you
You're very welcome! :-)
Sir your work is excellent... that was so helpful and crystal clear... keep up the amazing work...
Thank you for the kind feedback I’m glad to hear you enjoy the videos! :-)
thank you, this will help me through my CS1 class, my teacher just codes so fast its hard to keep up lol
You're welcome, good luck with your class! And I hear you about teachers going too fast... I love being able to pause and rewind videos on UA-cam. :-)
Thank you very much. Very nice presentation and very clear explanation.
You're very welcome, I'm glad you enjoyed the explanation! :-)
at around min 12:20: why do you check in main another time if stack is NULL? we already check it in the function, don't we?
That's a great question Philipp! :-) So the functions we are building are essentially a library for working with Stacks, and those library functions will be expected to perform certain functionalities, etc. The main() function is what we could call the "client code" that uses this library. So there is a separation between "implementation of the stack data structure" and "code that uses the stack data structure". We could put all these library functions into a separate file, and different programs could all use these library functions for different purposes. Our library function therefore handle the NULL value being returned in a very "generic way". Rather than exiting the program, it leaves this decision to the client code and its specific purposes/desires by just returning NULL in this case. This is similar to how when C library functions fail, they do not cause the program to exit, they typically return an error code or value and they leave it to "us" as the programmers to decide how to handle the error. The same thing is happening here, we are writing the library such that we leave it to the user of the library as to how to handle the error situation. And so they may need to check the value in main again and decide to quit (or not, or handle it some other way, etc.). Hopefully this helps! :-)
Your lecture is very good, i hope you will do the queue structure.
That's a good idea for a video, maybe one day I can do that, especially an array-based queue. :-)
what text editor your using
Sir, your work is outstanding. Thank you very much. I have just one comment to add. The sizeof(stack) is allocated to stack in create_stack, and this expression is used to determine the size of the pointer, not the size of the object it refers to. In my opinion, this is an error and you should allocate sizeof(*stack) since it determines the size of the object that stack references. PS: using the destroy_stack function may lead to a memory leak.
We are allocating space for a Stack struct that *stack will point to, so it needs to be sizeof(Stack). The calls to free() in destroy_stack look fine to me too.
@@PortfolioCourses
stack *stack = malloc(sizeof(stack)); -----> In this context, stack is used as both a variable name and a reference to the type. The memory allocation in this line is based on the pointer's size rather than the structure (since sizeof(stack) refers to the pointer named stack). The memory allocation will be equal to the pointer's size (4 or 8 bytes), which is not sufficient for the stack structure.
stack *s = malloc(sizeof(*s)); ----> This correctly allocates memory for the structure that s points to.
Attempting to access stack->collection, stack->size, or stack->collection may result in undefined behavior due to writing to unallocated memory.
stack with a lowercase s is the variable name, stack with an uppercase S is the struct name, that’s why we have sizeof(Stack) for allocation
@@PortfolioCourses my bad, I apologize for any confusion my comment may have caused. Your tutorial was spot-on, and I appreciate the clear explanation that led me to the correct solution. Keep up the great work :)
No worries, I'm glad you're enjoying the content. :-)
Finally! Thank you very much
You're very welcome! :-)
You are the best! Thanks a lot.
Great video as always!
By the way, can you make a video on how to implement a "FIFO" circular buffer in C?
Thank you! And I will add it to the list of future video ideas. :-)
Sir What is the Good approach to master and fully confident in c algorithms and c programming by practice or any other approach
That's a great question but it's also a very hard question to answer too. :-) Because we almost never really "master" and become "fully confident" in topics so large, there is always more to learn. And how people learn can be very personal and individual, some people learn better from books and other people from videos. I would suggest alternating between "studying" by reading books and watching videos and then practicing by trying to write code yourself to solve problems and build data structures. Alternating between "reading/watching" and "practicing" can be a best of both worlds because you kind of need both.... the reading and watching videos to help expand your knowledge, and the practicing to make sure you can actually apply it for real to solve problems. I also think it's a good idea to have one or two really good books on a topic, because those books can guide your learning, even if you end up using other resources too. The books act as a "framework" in terms of the concepts you should learn, and they often have practice exercises too. Hopefully this helps! :-)
@@PortfolioCourses Thank you Sir Hope Your Answer Will be valuable For many Students thank you.. All the Best Sir
@@salmantechnologies282 You're welcome! 🙂
I followed perfectly but my output always says stack size is 0, I am lost
edit: i somehow missed a line of code, probably between the scrolling around between code. You have also shown me that it is good to leave spaces above and below when I am going to be adding to my main, and to go ahead and put { and } at the same time and just type between them to avoid making errors
I'm glad you figured it out! :-) Note that the original source code is also posted here: github.com/portfoliocourses/c-example-code/blob/main/stack.c.
how can i print the full stack?
It depends on whether you want to pop each item off to print it, or just print the items.
If you want to pop all the items to print them you could keep popping as long as the stack is not empty and print out each popped value, in a loop like this:
int pop_val = 0;
while (!isempty(stack))
{
pop(stack, &pop_val);
printf("Popped Value: %d
", pop_val);
}
If you just want to print the items without popping them, you could have a loop with a counter variable (e.g. i) going from 0 up to stack->size, and then just use printf to stack->collection[i], something like this:
for (int i = 0; i < stack->size; i++
{
printf("%d
", stack->collection[i]);
}
@@PortfolioCourses Thank you very much, your videos are really incredible, you explain better than my teachers at university
@@eliga3954 Thank you, I'm glad to hear you enjoy them. :-)
I still didn't get the idea behind using capacity and size and how are they are not different, can you please explain? thank you
Great question Berkouk! :-) So the "capacity" is the maximum possible number of elements that can be stored in the stack, and the "size" is how many items are actually stored in the stack.
@@PortfolioCourses thank you so much for the kind reply, so from what i understand is that size acts like the index of the top element in the stack?
Btw I'm in CS in university and only god knows how much i appreciate your videos thank you for everything 🙏
Yes exactly, size is letting us know where the top of the stack is. :-) And that's great to hear these videos are helpful for you, thank you for sharing that with me.
man if there is paradise of any religion in the after life you will enter i ll make sure of that.
Love it.
Tqs