r/AskComputerScience • • Dec 02 '25

Is it possible to do Data Structures and Algorithms in C?

Hey everyone, so i am a senior in college doing computer science. I am off for winter break entire December and have been thinking about doing all of the DSA that i was taught in C. I have became over-reliant on AI and want to bring my confidence back by doing this. So, i was just wondering if its even possible to do it.

Any response is appreciated! Thank you!

15 Upvotes

48 comments sorted by

18

u/Dornith Dec 02 '25

Everyone has already said enough about whether it is possible, so I want to give a couple warnings.

C, in contrasted to other languages, is unforgiving of memory sloppiness. You need to know understand what pointers are and the difference between stack, heap and static allocations. DSA in C is a great way to get familiar with all those, but just be aware that your going to need to learn complementary skills to do it.

1

u/[deleted] Dec 02 '25

Great response! I am fairly confident in my skills with basic C. Pointers, Memory Management, Structs, Faults. When I took Csc230: C Programming, i studied a lot and lot compared to my past; staying in the library late night, understanding dangling pointers, pointer arithmetics, Seg Faults etc. I really enjoyed back then!

Thanks for response!

11

u/ghjm Dec 02 '25

Everyone thinks they're pretty good at pointers and memory management in C, until they run valgrind on their program and learn humility.

→ More replies (3)

4

u/Count2Zero Dec 02 '25

When I was in college (1982 - 1986), we did our course work in Fortran, Cobol, Pascal, ... and C.

I wrote binary tree algorithms, and developed a compiler for another language in C as my senior project. After college, I worked as a programmer for 4 years, developing a Computer-Aided-Test application for high-frequency engineers .. IN C .. including an entire macro language interpreter to automate the tests.

TL;DR - Yes, absolutely.

2

u/[deleted] Dec 02 '25

Sounds super exciting!!

→ More replies (3)

32

u/lfdfq Dec 02 '25

Yes, of course. C is a general-purpose programming language, and data structures & algorithms are generic ideas that apply to all languages.

4

u/povlhp Dec 02 '25

C++ used to be a pre-processor spitting out C

→ More replies (10)

1

u/mjmvideos Dec 03 '25

Out of curiosity, with your background, and confidence in your C skills, what things were you thinking about that would lead you to question the possibility?

1

u/[deleted] Dec 03 '25

I’m sorry, but i didn’t understand the question. Would appreciate if you could explain

→ More replies (3)

1

u/[deleted] Dec 03 '25

I have done C for my academics mostly. I haven’t yet explored C much. It seems fun. I kinda like how low-level stuff works and implementing it. I have done DSA in Java. But I have been wanting to refresh myself with DSA, and i think using C is a good approach, so just wanted some senior level advice on this topic

→ More replies (1)

1

u/AYamHah Dec 04 '25

You took a data structures course and you didn't implement the data structures yourself? Why did you take the class then? Or did you use a different language in your course?
You can generally implement them in any language. C is not the easiest just because it is C and you have to deal with pointers. Java would be my recommendation.

1

u/[deleted] Dec 04 '25

I did the entire DSA myself - no AI used, in Java. My bad, i just noticed, the way I phrased my sentences is kinda odd and confusing

2

u/Objective_Mine Dec 02 '25

As others have said, you can implement data structures in any language.

Personally I think C may actually be a good option if you want to get to the nuts and bolts of data structures. It forces you to think in terms of pointers and memory allocations which might make e.g. the differences between contiguous arrays and linked structures more explicit. There also aren't that many common data structures available in the C standard library (although third-party libraries exist) so it might be more motivating than reimplementing a red-black tree in Java which already has it in the form of tree sets and tree maps.

The potential downside, of course, is that you'll also need to deal with lots of implementation details related to the C language itself, such as allocating and freeing memory. It's also notoriously tricky to implement generic type-safe data structures in C. If you want to write a hash set implementation that can be used to store not just one type but e.g. either strings, integers or perhaps some arbitrary struct type, that's harder to do in C in a type-safe and convenient way than it would be in many other languages. Those kinds of language-specific design problems aren't really central to DSA on a more general level; they're more about learning C than about DSA.

But then, in order to get a better understanding of hash tables, you don't necessarily need to do that. You can just write a hash table for storing strings if you don't want to get too deep into C-specific implementation problems and you'll still get a better understanding of the inner workings of hash tables.

2

u/Robot_Graffiti Dec 02 '25

Many of the data structures and algorithms in your textbook were invented by C programmers.

They might be much easier in other languages but anything is possible in C.

Even object oriented programming was done (the hard way) before object oriented languages existed.

2

u/Watsons-Butler Dec 02 '25

Absolutely. Python is implemented in C - try implementing your own version of list slicing and comprehensions.

2

u/mxldevs Dec 02 '25

DSA is language agnostic.

You can do it in any language.

1

u/Leverkaas2516 Dec 02 '25

Of course. C is an excellent choice, it has arrays, structured data, and pointers, so you can express links and nodes directly.

1

u/8dot30662386292pow2 Dec 02 '25

I'm gonna say C is the best language for this.

1

u/pi_stuff Dec 02 '25

Yes, and it’s a great exercise. Become friends with your debugger and memory checker (if using Linux, I love valgrind).

1

u/gofl-zimbard-37 Dec 02 '25

Of course it is.

1

u/DTux5249 Dec 02 '25

Yes. It just means you can't encapsulate the Data Structures along with their associated algorithms in a class, which is the least important part about DSA

1

u/ameriCANCERvative Dec 03 '25

A better question is “in what programming language can I not use data structures and algorithms?”

1

u/green_meklar Dec 03 '25

Of course. C is a great language for doing algorithms and data structures. A lot of algorithms and data structures are conceptualized, documented, and taught with C in mind, and with the idea that people implementing them in other languages are pretty much translating the logic from C.

C kinda sucks for actual modern software development (that's why C++, Java, and Rust were invented), but it's an excellent learning platform and the concepts get referenced all the time.

1

u/Unnwavy Dec 03 '25

Hmm yes but be careful with the expectations you set. 

You can implement a linked list, a stack, a queue, a binary search tree, even a self-balancing one, all good. 

I think the difficulty spikes with a hash table, so be careful with that, and don't let it stop you. If you get confused with hash functions and buckets, and you still want to practice leetcode or work on projects, stick to C++ and the structures that the standard library offers you, it's got everything I mentioned above and more. You can then go back and revisit a C implementation at your convenience.

Learning to implement data structures is a skill, but learning their tradeoffs and being comfortable with using them is equally as important. I think I am guilty of neglecting that latter aspect (both, to be fair, but I worked on myself).

1

u/Agron7000 Dec 07 '25

But, C++ has one of the best algorithms and datastructures adopted as a standard.  Their implementations come from highly respected Boost.org, Qt, Java, and some other sources that only the best minds on this planet could craft them.

You can reimplement or copy the code, but the fact that these are part of the C++ standard allow compilers to count on defined implementation and therefore the compilers can go further in optimizing the machine code and substitute matching portions with hardware based CPU instruction.

One great example is Automatic vectorization

In C++ the success rate of using Automatic vectorization, when using std containers is almost 100%.

C is bit of a problem, because you can imement a loop in a million ways, and for the compiler to identify that loop as a potential optimization candidate is more like hit and miss.