[c] Implementing a Generic Linked List in C

preview_player
Показать описание
Two Ex is the right speed.
Рекомендации по теме
Комментарии
Автор

> Generic C
> Look inside
> void*
It's always like this, I swear.

mariosshadow
Автор

These videos are very underrated for sure.

friedrichmyers
Автор

this made cpp look comfortable to write :3 thanks

akirami
Автор

Please don't stop making videos please.

imranhasan
Автор

I learned generic linked lists last year at college. I had a great teacher so it was pretty easy to understand, but later on I started to realise some limitations.
One doubt I had was "How do I make a printList function if I don't know what my data is?"
I thought "Ideally my data is primitive (int, char), so I'll just have to change that %d to %something and stuff on the printf..." but that'd still imply making another printList function for each of those somethings, even with custom types, like persons or products (structs).

In every single video I've seen on the internet about linked lists, all they'd do is create the node structure just for ints and the head would just be a double pointer to node. Apart from that, they'd do some basic functions and that'd be it. Not even once I've found a video about generic linked lists up until now.

So the fact that not only you happen to implement generic lists in the same way I was taught, but found a way to make a GENERIC PRINTLIST FUNCTION, IN BARE C, just blows my mind. There are too many coincidences, my friend. This has got to be the God of Programming helping me with my college assignments. It's like I've finally unlocked the next level in depth of understanding of C syntax and it's possibilities!

Putting jokes aside, not even watched the whole video, but I know for sure it'll be of great help. Ever since I learned generic lists I wanted to do this, so thank you tons man.

FrancoNSosa
Автор

Hiya, a bit of advice on debugging your initial segfault: I would always advocate for using a debugger like GDB or tools like Valgrind (you added the -g flag for a reason), but if you choose to debug using printf, keep in mind that stdout might be buffered. This could mislead you on your search for the bug because a printf could be called, but then the segfault occurs before stdout is flushed, so you get no output! Therefore, you probably want to either print to stderr using fprintf(stderr, …) or manually flush stdout using fflush(stdout)

killermonkey
Автор

Don't store a data pointer in node, store the data itself, let user alloc nodes with enough space

mithrandirek
Автор

Great video, but a better generic in C is to use uintptr_t, you can still contain any pointer you want, but you get the added benefit of also using it directly to store any value, not that you would want it, but it's more ergonomic than void * in my humble opinion, plus it makes for more explicit code. as when you use it you need to cast your pointer on your way in, and cast it back on your way out. Anyway great video a again :)

pierreollivier
Автор

What I would do is make generic functions that can operate on a linked list with any data type by using macros and such and let the user create the struct with the datatype he wants.

abdullah
Автор

Cool video, but structs without typedef is hurting my soul 😅
You can do something like this:

```
typedef struct MyStruct {
// fields
} MyStruct;

MyStruct *var = NULL;
```

If you need to reference type in itself, you must declare typesef separatly from declaring struct:

```
typedef struct Node Node;
struct Node {
Node *next;
};
```

Also naming struct is not nessecary (if you don't nest this struct):

```
typedef struct {
// fields...
} MyStruct;
```

strongleongch
Автор

When do we use Macros and when do we use Void pointers?

I can create a macro and it will create things for me as well. But it will be different everytime.

friedrichmyers
Автор

Which is editor used for typing with git included ?

JoeRandom-xb
Автор

I made one version inspired from the video but it led to a cursed syntax where everything should be array or allocated on the heap.
So I changed it a bit by adding a union and now is way cleaner without everything allocated on the heap.
/*For example*/
typedef struct {
union {
float f32;
int32_t i32;
char str[STR_SIZE];
} data;
void* next;
void (*print)(void*);
} NODE;


add_front(&list, "World", str);
float f32_num = 3.14;
add_back(&list, &f32_num, f32);

marka
Автор

hello sir, thank you for the video, i have a question, why we add data_size to GenericLinkedList and we didnt use it later.

abrh