Stack Data Structure And Operations | C Programming Example

Поділитися
Вставка
  • Опубліковано 29 гру 2024

КОМЕНТАРІ • 42

  • @tdoc666___
    @tdoc666___ 10 місяців тому +9

    dude just *stacked* this video on the top of youtube suggestions...

  • @matriculadigital3926
    @matriculadigital3926 Рік тому +3

    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

  • @steevenkenny9791
    @steevenkenny9791 2 роки тому +1

    Sir your work is excellent... that was so helpful and crystal clear... keep up the amazing work...

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому +2

      Thank you for the kind feedback I’m glad to hear you enjoy the videos! :-)

  • @iwannapressit6886
    @iwannapressit6886 Рік тому +1

    thank you, this will help me through my CS1 class, my teacher just codes so fast its hard to keep up lol

    • @PortfolioCourses
      @PortfolioCourses  Рік тому +1

      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. :-)

  • @mktaleb7887
    @mktaleb7887 Рік тому

    Thank you very much. Very nice presentation and very clear explanation.

    • @PortfolioCourses
      @PortfolioCourses  Рік тому

      You're very welcome, I'm glad you enjoyed the explanation! :-)

  • @philipphortnagl2486
    @philipphortnagl2486 Рік тому

    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?

    • @PortfolioCourses
      @PortfolioCourses  Рік тому

      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! :-)

  • @ndttechnicaluniversityute-3246
    @ndttechnicaluniversityute-3246 2 роки тому +1

    Your lecture is very good, i hope you will do the queue structure.

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому +1

      That's a good idea for a video, maybe one day I can do that, especially an array-based queue. :-)

  • @swarajgaikwad8787
    @swarajgaikwad8787 10 днів тому

    what text editor your using

  • @zakariaeelkalai
    @zakariaeelkalai 10 місяців тому

    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.

    • @PortfolioCourses
      @PortfolioCourses  10 місяців тому

      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.

    • @zakariaeelkalai
      @zakariaeelkalai 10 місяців тому

      @@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
      @PortfolioCourses  10 місяців тому

      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

    • @zakariaeelkalai
      @zakariaeelkalai 10 місяців тому

      @@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 :)

    • @PortfolioCourses
      @PortfolioCourses  10 місяців тому +1

      No worries, I'm glad you're enjoying the content. :-)

  • @akithjayalath8686
    @akithjayalath8686 Рік тому

    Finally! Thank you very much

  • @GosnSvetolik
    @GosnSvetolik 10 місяців тому

    You are the best! Thanks a lot.

  • @Architector120
    @Architector120 2 роки тому +1

    Great video as always!
    By the way, can you make a video on how to implement a "FIFO" circular buffer in C?

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому +1

      Thank you! And I will add it to the list of future video ideas. :-)

  • @salmantechnologies282
    @salmantechnologies282 Рік тому

    Sir What is the Good approach to master and fully confident in c algorithms and c programming by practice or any other approach

    • @PortfolioCourses
      @PortfolioCourses  Рік тому

      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! :-)

    • @salmantechnologies282
      @salmantechnologies282 Рік тому

      ​@@PortfolioCourses Thank you Sir Hope Your Answer Will be valuable For many Students thank you.. All the Best Sir

    • @PortfolioCourses
      @PortfolioCourses  Рік тому

      @@salmantechnologies282 You're welcome! 🙂

  • @BaxxyNut
    @BaxxyNut Рік тому

    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

    • @PortfolioCourses
      @PortfolioCourses  Рік тому

      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.

  • @eliga3954
    @eliga3954 2 роки тому +1

    how can i print the full stack?

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому +2

      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]);
      }

    • @eliga3954
      @eliga3954 2 роки тому

      @@PortfolioCourses Thank you very much, your videos are really incredible, you explain better than my teachers at university

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому

      @@eliga3954 Thank you, I'm glad to hear you enjoy them. :-)

  • @berkouklahcene2594
    @berkouklahcene2594 Рік тому

    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

    • @PortfolioCourses
      @PortfolioCourses  Рік тому

      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.

    • @berkouklahcene2594
      @berkouklahcene2594 Рік тому +2

      @@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 🙏

    • @PortfolioCourses
      @PortfolioCourses  Рік тому

      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.

  • @raoufbouattour555
    @raoufbouattour555 7 місяців тому +2

    man if there is paradise of any religion in the after life you will enter i ll make sure of that.

  • @lawniczakjohn
    @lawniczakjohn Рік тому

    Love it.

  • @Historyhub41
    @Historyhub41 2 роки тому

    Tqs