Stack Data Structure And Operations | C Programming Example

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ส.ค. 2024
  • How to implement a stack data structure in C, including a library of functions that implement the operations pop, push, peek, is full, and is empty. Source code: github.com/por.... See the Wikipedia article on Stack data structures: en.wikipedia.o.... Check out www.portfolioc... to build a portfolio that will impress employers!

ความคิดเห็น • 41

  • @tdoc666___
    @tdoc666___ 6 หลายเดือนก่อน +5

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

  • @matriculadigital3926
    @matriculadigital3926 ปีที่แล้ว +2

    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

  • @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 TH-cam. :-)

  • @steevenkenny9791
    @steevenkenny9791 ปีที่แล้ว +1

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

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว +2

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

  • @ndttechnicaluniversityute-3246
    @ndttechnicaluniversityute-3246 ปีที่แล้ว +1

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

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว +1

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

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

  • @zakariaeelkalai
    @zakariaeelkalai 6 หลายเดือนก่อน

    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  6 หลายเดือนก่อน

      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 6 หลายเดือนก่อน

      @@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  6 หลายเดือนก่อน

      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 6 หลายเดือนก่อน

      @@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  6 หลายเดือนก่อน +1

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

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

  • @GosnSvetolik
    @GosnSvetolik 6 หลายเดือนก่อน

    You are the best! Thanks a lot.

  • @akithjayalath8686
    @akithjayalath8686 ปีที่แล้ว

    Finally! Thank you very much

  • @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.

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

  • @raoufbouattour555
    @raoufbouattour555 3 หลายเดือนก่อน +2

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

  • @lawniczakjohn
    @lawniczakjohn 11 หลายเดือนก่อน

    Love it.

  • @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! 🙂

  • @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 ปีที่แล้ว +1

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

  • @Historyhub41
    @Historyhub41 2 ปีที่แล้ว

    Tqs

  • @eliga3954
    @eliga3954 2 ปีที่แล้ว

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