r/learnprogramming • • 14h ago

I want to study Data Structure and Algorithms efficiently for my exams and logical building, among C and C++ what should i choose?

I've learnt both of these languages in earlier semesters and felt comfortable, but for DSA, i can't decide what to choose...

2 Upvotes

5 comments sorted by

1

u/plastikmissile 14h ago

Whichever one you're most comfortable with, since DSA is language agnostic.

1

u/NimbuBantaSequel2 14h ago

I guess cpp as it supports oops and oops is needed for advanced dsa topicss

1

u/Ansh_Singh_37 13h ago

Yea i believe that too. I think I'll go with cpp only. Thanks

1

u/plastikmissile 11h ago

What structure is better done with OOP classes that can't be done just as well by a C struct?

2

u/mredding 9h ago

As I know both, having used them both for ~37 years, I'd go with C.

And I'm principally a C++ engineer, always have been.

C is a smaller, simpler language. This will get a lot of syntax and boilerplate out of the way. You're not trying to learn a language or implement a product or solution, you're just trying to learn concepts, bring them to life. This is an academic exercise. That simplicity is it's own utility. With C, you will see the parts each moving in simple, almost mechanical steps, because C is imperative and procedural and thus FEELS close to the machine.

But as u/plastikmissile said, DSA itself is language agnostic. Graph theory and lambda calculus don't care what language you use. And yet, you don't get too many DSA classes in Lisp or Python, because some of these structures are language natives - so all you'd see is (first second third) and you're supposed to know that's a sequence of cons cells? How about this: (first (second (third))). Same thing, just more parenthesis? Does that help? And car returns the first element and cdr returns the rest of the elements? No? Doesn't do it for you?

Not for most others, either. So C it is:

struct node {
  int value;
  struct node *next;
};

node *head, **tail = &head;

void push_back(int value) {
  *head = malloc(sizeof(struct node));
  (*head)->value = value;
  head = &(*head)->next;
}

int main() {
  push_back(1);
  push_back(2);
  push_back(3);
}

Do you see it more clearly? In C, we call them node what Lisp would call cons. Lisp is SLIGHTLY different in that it would look more like this in C:

struct cons {
  void *first, *rest;
};

Or:

struct cons {
  void *cells[2];
};

Each cell is two parts, and it references anything... And anything. It's a dynamic language so in C you'd make some overlapping structure that could contain some identifying metadata type info so you know how to cast it properly.

Getting down to the machine really helps for some reason.

C++ is only a flourish:

template<typename T>
class list {
  struct node {
    T value;
    node *next;

And frankly, almost everything is the same verbatim, just with an additional brace for scope. But what did this gain us? It abstracts away the value type we're storing, because that's actually not important, but this is an academic exercise! Who cares? Just stick an int data in there and move on. This is about DSA, not about implementation details. In C, you'd write generator macros in place of templates, but that's all boilerplate! You don't write this stuff yourself if you don't have to - anything you learn in class is so bog standard there's entire libraries out there done to death by everyone that all do the same bog standard thing. You use those in production.

The reason to understand DSA is to just get familiar with the principles, because graph theory is fundamental to computation. Most business software doesn't care enough to fine tune their data structures to a mathematically proven best fit, but sometimes, JUST sometimes... Holy fuck it really matters, and maybe that's exactly what you need to do. For the rest of business, you just need to understand what's going on. For the sake of your academics, this is just an introduction anyway. If you really want to get deep, you can pursue a PhD in graph theory.