Set Data Structure And Operations | C Programming Example

แชร์
ฝัง
  • เผยแพร่เมื่อ 28 ธ.ค. 2021
  • How to implement a set data structure in C, including related operations such as union, intersection, difference, membership, etc. Source code: github.com/portfoliocourses/c.... This implementation is intended to help learners understand how to implement data structures and related algorithms, it is not built for high performance. See the Wikipedia article on Sets here: en.wikipedia.org/wiki/Set_(ma.... Check out www.portfoliocourses.com to build a portfolio that will impress employers!
  • แนวปฏิบัติและการใช้ชีวิต

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

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

    We can implement a delete set function to delete sets from memory like this:
    void delete_set(Set *set)
    {
    free(set->members);
    free(set);
    }
    This will prevent memory leaks from occurring, so we should use this function when we are done with sets. 🙂

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

    This is by far my favourite tutorial series on TH-cam :D

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

    The best video on sets 🙌

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

      I'm glad you enjoyed it Sid, thank you for the positive feedback! :-)

  • @MyName-bw1kz
    @MyName-bw1kz 2 ปีที่แล้ว

    Thank you so much for this! I love your videos, you explain them so well. I was wondering if you offer online 1-on-1 tutoring?

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

      Thank you so much for the kind words! Right now I don't offer 1-on-1 online tutoring, but it's something that I'm thinking about doing in the future. :-)

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

    first, thank you very much for taking the time in making this tutorial. I have been looking how a set efficiently checks whether there is an element. Am I correct to assume a set basically checks linearly if there is an element? With an ordered set, which way would you recommend ordering a new entry? cheers and thank you

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

      If it's an unordered set and the data is stored "internally" by the data structure in an unordered way, then a linear search would be done. The data could be stored in an ordered way though, even if the set itself is "unordered". Because we could just insert each new element in order, even if that involves more work. It could make the "is member" operation more efficient because than binary search could be applied if say the values are stored in an ordered array. With an ordered set, I would store the data in an ordered/sorted array, and just insert each new value in order. :-)

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

    Thanks a lot

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

    similar to the insert function how would you create the corresponding delete function or remove function?

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

      Great question Rohan! :-) I think something like this should work:
      remove(Set *set, int member)
      {
      for (int i = 0; i < set->length; i++)
      {
      if (set->members[i] == member)
      {
      for (int j = i; j < set->length - 1; j++)
      {
      set->members[j] = set->members[j + 1];
      }
      set->members =
      realloc(set->members, sizeof(int) * (set->length + -1));
      set->length = set->length - 1;
      break;
      }
      }
      }

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

    should we free up the allocated memory on some where?
    I used this set in leetcode problem, their last input was 1 00 000 numbers and got memory limit ex. error

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

      Yes, this code does not include a delete function but we could build one like this:
      void delete_set(Set *set)
      {
      free(set->members);
      free(set);
      }
      We could then use the function to delete sets we no longer use. :-) This might help with the situation with leetcode depending on what the algorithm looks like, if we actually need 1,000,000 numbers maybe we could run out of space in memory anyways depending on how their system is setup too.

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

    Sir, Could you upload the full course of data structures and algorithms, please?

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

      I don’t have a full course on data structures and algorithms. One day it would be nice to have one though!

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

      @@PortfolioCourses Thank you so much.

  • @ahmedqasem7651
    @ahmedqasem7651 3 หลายเดือนก่อน

    font name please ?

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

    thanks your tutorial save me ass.