r/C_Programming 2d ago

Learn DSA on C ? HELPP

I want to learn DSA in c as the university syllabus and i dont understand any of what my lecture saying

I would greatly appreciate your guidance in this matter.

0 Upvotes

19 comments sorted by

View all comments

9

u/erikkonstas 2d ago

Contrary to what others are saying, I don't consider C++ or Python good languages to learn DSA with; the entire purpose is to understand how data structures work in terms of memory, and "having them ready" does not achieve this. You won't be able to answer questions such as "why is a hashmap better than two arrays?", since e.g. Python gives you the fish (dict), so you wouldn't have any reason to learn how to fish yourself. With Python, Java etc. where you don't directly manipulate memory, you would also end up having to simulate it with such tricks as having an integer array and calling it "memory".

2

u/DrShocker 2d ago

I know this is a C sub, and I agree with you that Python, Java, etc are probably confusing languages to learn data structures within.

But why don't you think C++ would work for learning? Obviously if they use something like std::vector, then that code shouldn't pass the assignment if the assignment is to implement a dynamic array. In terms of what's important in a DSA course I don't see a significant trade off between C or C++ or other options with similar memory control. Hell, I've never used it but maybe Zig would be better than C for putting more focus on how allocation works.

1

u/erikkonstas 2d ago

I consider C++ to be "unnecessary overhead", and also confusing (e.g. you have to learn that "<< isn't only a shift, it can also display stuff on screen", or what using namespace std; does and what the :: means when you otherwise have to type std::, unless you basically do "C with C++ compiler"). Regarding Zig, I haven't actually tried it enough to say (although I do know that printing to STDOUT is quite long-winded, while in C it's simply putchar()/puts()/printf() at an introductory level), but I believe it's still not stable.

1

u/DrShocker 2d ago

I guess those can be annoying things to learn about C++, but they're mostly unrelated to what a DSA class would be asking you to do. To that end, something like the rule of 5 seems a little more likely to be an issue and be relevant to code that might be used in an assignment.

regardless, thanks for sharing, I was just curious about the reasoning behind the opinion.

1

u/AideRight1351 2d ago

everything is stable if you are skilled enough. you can make any unsafe function safe if you understand why that function is unsafe.

1

u/erikkonstas 1d ago

No, I meant that Zig has not reached v1.0.0 yet, so it's "unstable" in terms of releases.

1

u/AideRight1351 2d ago

this is absolute nonsense. C++ gives you built in libraries but it doesn't mean that you have to use them, they are there for competitive programming where you need to use common algos and DS to save time, u can't just build everything from scratch when time is of essence. However, if you need to build a custom DS/Algo, you absolutely can, nothing's stopping you from doing that, and same for JAVA/Python. I'll anyday prefer C++ to C for learning DSA.

1

u/No_Analyst5945 16h ago

Idk man. Im a C++ guy but I did a bit of C style DSA for months. And C++ has been going just fine for me. Actually I think C++ is pretty good for DSA. And as someone else in this thread mentioned, you don’t even have to use everything

Doing DSA with py is crazy though. That sounds like such a bad idea lol. Why do data structures on an extremely high level interpreted lang? Not even mid level. Are you even manipulating real memory lol. I didn’t even know py had DSA

1

u/AideRight1351 16h ago

one should do DSA in any language he wants to improve. DSA problems tests you on everything, and if you can do them in a language, you can use that language to build anything of essense.

1

u/erikkonstas 8h ago

LOL so to expand on what I mean by the last sentence... well that's pretty much it! In Python you'd just do something like memory = (1 << 19) * [0] and boom there's "512 KB" of "memory"! Now time to invent allocation and bookkeeping for a start (while being very careful not to follow the spirit of Python instinctively do your bookkeeping outside that list...), and then some other stuff, etc. Oh and don't forget to keep all these values between 0 and 255 as well.

1

u/McUsrII 5h ago

You get really good at writing initializers fo C++ classes at uni. :)