Adding elements to a linked list

  Рет қаралды 38,556

CodeVault

CodeVault

4 жыл бұрын

Check out our Discord server: / discord

Пікірлер: 68
@victorsenaxD
@victorsenaxD 3 ай бұрын
The fact that you get excited talking about this concept really helps me to try to understand, it is a quality that great teachers have
@ultracycling_vik
@ultracycling_vik 4 жыл бұрын
I must compliment your great explanations. You really can convey the complexities of the C Language in an understandle manner. I am surprised that your content is not ranked higher, because , at least in my opinion, you have produced the best tutorial series about C on KZbin. Thank you very much for your work!
@AKGamersLite
@AKGamersLite 3 жыл бұрын
I agree, I been watching all his C videos for past few months and have to say he explains it in such a simple and enjoyable manner. He explains the same concepts 1000 times better than my professors at university.
@anabrr
@anabrr 2 жыл бұрын
I also agree. I just come to him every time I want to learn about a new C concept, because I know there is no better explanation than his
@Safwan.Hossain
@Safwan.Hossain 5 ай бұрын
This is by far the best Linked List series I've seen on KZbin specifically for C because you went over many quirks of C programming that I was confused about (like the deferencing NULL pointer warning given when you use malloc) as well as how pointers work play into creating Linked Lists
@kapellimestari0078
@kapellimestari0078 2 жыл бұрын
I was trying to apply and understand linked lists fully about 3 weeks by now, and your videos are so clear and so so good, thank you very much
@thatianamoreira9636
@thatianamoreira9636 2 ай бұрын
You're an excellent teacher! Thanks so much!!! I'm a brazilian Software Engineering student and your videos are helping me a lot!
@michalbotor
@michalbotor Жыл бұрын
you're a brilliant teacher. you not only explain c in a remarkably easy to understand way, but also anticipate all the errors that a noobie will probably encounter. you introduced malloc, and of course i had a memory leak, because i forgot to call the free function. you introduced two mallocs, and of course i had a memory leak again, because i called free function twice, but in the wrong order. you introduced the insert_end function, and of course i had a segmentation fault, because i forgot that an empty linked list is an edge case. simply outstanding learning experience.
@m4rcus411
@m4rcus411 Ай бұрын
Thank you a lot for these videos. You explain C very well
@tonykososki3016
@tonykososki3016 3 жыл бұрын
man i its the next video im watching from you and i must say you are very good at explaining things, i hope you gonna have more views soon and the channel will rise! good luck!
@turuus5215
@turuus5215 2 жыл бұрын
This guy is an excellent teacher.
@Jarvx
@Jarvx Жыл бұрын
One of the best C series of all time
@campeanpetru-razvan9995
@campeanpetru-razvan9995 Жыл бұрын
Amazing video , Thank You!!
@vambans2412
@vambans2412 4 жыл бұрын
Very good explantion best course for c program and linked list. I want to come back after one month and will see 100K subcribers
@tbatana
@tbatana 4 ай бұрын
Best gesticulation in C world! respect!
@Gamerssassin
@Gamerssassin 2 жыл бұрын
What an amazing video.
@captainpupz7271
@captainpupz7271 7 ай бұрын
Thank you, you saved my life!!!
@fluffyweird
@fluffyweird Жыл бұрын
It took me about 3 days but you will get it just keep on rewriting the code and NEVER GIVE UP, and thank you sir for this amazing video
@fluffyweird
@fluffyweird Жыл бұрын
although I still don't understand the application of it I think linkedlists are something I should understand and play around
@CodeVault
@CodeVault Жыл бұрын
You really have to look at time and memory complexities when talking about usefulness of linked lists. When adding at the beginning of an array, the operation is O(n) but when adding at the beginning of a linked list the operation is O(1). So, this is an advantage. If this operation is something that needs best performance in your program, then it's worth considering using (same applies with other operations, although for some the array is better) Another consideration is data. If you need the data to be stored sequentially then, of course, a simple array is good enough but, if you need it scattered around the memory, then a linked list is a good candidate (linked lists are what are used in databases to create the B-tree indexes)
@anabrr
@anabrr 2 жыл бұрын
Your explanation is very very good, but still this isn't easy stuff!!
@ruscyber9765
@ruscyber9765 2 жыл бұрын
Can a memory leak (if I don't use free() in a simple program) cause a serious troubles with my computer such as: data corruption, or will games or other my projects work badly?
@CodeVault
@CodeVault 2 жыл бұрын
Nonono, usually the operating system takes care of freeing the memory after the program finishes its execution. But in a long running process, this can cause issues (for example, if there was a memory leak in a game then it might end up using up all the RAM available on the system)
@szymonxf
@szymonxf 3 жыл бұрын
12:35 how did you commented all 5 rullers of code at one time? What buttons combination is this?
@CodeVault
@CodeVault 3 жыл бұрын
You can add a new cursor above/below the current one with Ctrl + Alt + UP(or DOWN) in VSCode. Note that you might have to change that shortcut as some graphics drivers default to it as well sometimes.
@h_ipon
@h_ipon 3 ай бұрын
im sorry but where exactly did the free function go since you used malloc? and thank you so much btw you made it way much easier for me to understand linked lists!!
@CodeVault
@CodeVault 3 ай бұрын
At a later video I explain how to dealloc the linked list: code-vault.net/lesson/ggiva1mn2n:1603732279985
@luandkg
@luandkg 4 жыл бұрын
Great !
@puripatratanaarpa4066
@puripatratanaarpa4066 2 жыл бұрын
I'm very confuse why we use double pointer points to the first node. Why don't we use the first node pointer directly?
@CodeVault
@CodeVault Жыл бұрын
Because, sometimes, we need to change the root node. Since the address to the root node is in the main function's stack memory, we have to have a pointer to that, otherwise modifying the root node won't modify it in the main function. Since the root node is a pointer to Node, to modify that we need a pointer to pointer to Node (the double pointer you're talking about)
@user-hs5ge6mn1v
@user-hs5ge6mn1v 8 ай бұрын
Hi, why do you decide to use a pointer to a pointer in your insert_end function for the root. It seems like it would just add another step when trying to use root because you would have to always put * in front of it once?
@CodeVault
@CodeVault 8 ай бұрын
In case the linked list is empty. If it's empty we actually have to create the root node and, since the root pointer is in the main function, we need to change that. We need a pointer to the root pointer for that
@Safwan.Hossain
@Safwan.Hossain 5 ай бұрын
I think it is because if you do not use pointer to pointer (node**), then we are passing a node pointer (node*) by value instead of by reference, so the changes we make to `root` will not take place when we return to main, only done in the insert() function and lost.
@medam9042
@medam9042 2 жыл бұрын
Arigato !
@ukaszkiepas5605
@ukaszkiepas5605 3 жыл бұрын
thanks
@wassimboussebha2561
@wassimboussebha2561 2 жыл бұрын
in insert function , why we can't do that directly without using the pointer curr ?
@CodeVault
@CodeVault 2 жыл бұрын
Well, you can, but you'd only be able to insert the element at the beginning of the list (not at the end)
@andyan6776
@andyan6776 Жыл бұрын
Thanks a lot for the wonderful video! I am wondering why the program you write won’t have a memory leaks? Shouldn’t we free the new_node each time?
@CodeVault
@CodeVault Жыл бұрын
Yes! That's what I cover in one of the next videos: code-vault.net/lesson/ggiva1mn2n:1603732279985
@andyan6776
@andyan6776 Жыл бұрын
@@CodeVault thanks! But I couldn’t open this website.
@andyan6776
@andyan6776 Жыл бұрын
@@CodeVault do you have the link for that video on KZbin?
@andyan6776
@andyan6776 Жыл бұрын
@@CodeVault the website works! I think that might be my internet problem. I have watched that video and I am wondering what if I just remove several nodes from the linked list in a removed function, how do I free those several removed nodes?
@andyan6776
@andyan6776 Жыл бұрын
@@CodeVault my current situation is: I got a linked list and set up using the way you teach in this video, and through the whole program, there is a function that will remove specific nodes from this linked list, so at the end of the program, when I free this linked list starting from the head, I won’t be able to free those specific nodes that had been removed from the linked list, so there is a memory leaks… I have thought about this for nearly 4 hours and couldn’t think about a solution for freeing those removed nodes…
@Alex-uz3dq
@Alex-uz3dq Жыл бұрын
thank u a lot
@whatever6223
@whatever6223 3 жыл бұрын
Why not simply pass the root pointer by value? It will still point to the same place in memory. Am I missing something?
@CodeVault
@CodeVault 3 жыл бұрын
I explain at the end of the video. If the list is empty then *root is NULL, which needs to be set to the new node that is being inserted. So the root does change in some cases
@whatever6223
@whatever6223 3 жыл бұрын
@@CodeVault I see. Thank you very much for the explanation. You've got an excellent channel!
@onaecO
@onaecO Жыл бұрын
dear sir, at 11:25 why we cannot send the pointer to root directly ,but instead a **root?
@CodeVault
@CodeVault Жыл бұрын
Remember when I said that we consider an empty list (with no elements) to just have the root pointer be NULL. That means that, if there are no elements in the linked list we will have to change this pointer from NULL to something else. But since root is inside the main function and we want to change it from another function we need a pointer to THAT. A pointer to a pointer to a node, that's just a double pointer You can design the linked list in such a way that you don't need to pass a double pointer.
@duarteponce4
@duarteponce4 Жыл бұрын
love u
@ib1664
@ib1664 4 жыл бұрын
Absolutely awesome explanation, can you also implement trees and binary trees
@CodeVault
@CodeVault 4 жыл бұрын
Sure thing. It's already in the planning stage ;)
@zimablue2664
@zimablue2664 2 жыл бұрын
I don't really understand why we need a "node** root", is a "node* root" also ok for this application?
@CodeVault
@CodeVault 2 жыл бұрын
The thing is, the pointer to the root node of the linked list is stored in the main function. (as a node*). This is fine for insertion, unless the list is empty. If it's empty, the pointer to the root node is NULL. So you'll need to modify that to be the added node that you want to insert. The thing is, that pointer is in the main function so, to change it you need a pointer to that pointer. Hence the double pointer (node**)
@sup3rs566
@sup3rs566 3 жыл бұрын
Sorry i don't understand why you used a double pointer to the root to create a new list, i tried to use only a pointer and it works the same. I'm surely missing something.
@sup3rs566
@sup3rs566 3 жыл бұрын
Well i tried it with better functions now and it doesnt work without double pointer, but i still don't understand why we need that
@CodeVault
@CodeVault 3 жыл бұрын
It's because we're changing the root itself if there are no elements in the list. And, since the root is a pointer inside main, we need something that points to that aka a pointer to that pointer, aka a double pointer.
@sup3rs566
@sup3rs566 3 жыл бұрын
@@CodeVault ok, now i understand. Thanks!
@pauloskalyvas1590
@pauloskalyvas1590 3 жыл бұрын
hi.... i am trying to add elements in a linked list in a fuction .... When i print the values into the fuction everything is fine... When i am trying to print them into the main i dont get nothing...why doesn't return when i am writing *mylst_ptr=tmp; ?? I created tmp just to keep the first node of list.... Can you please help me ? typedef struct nod{ int value; struct nod *next; }Node; typedef Node * List; void createList ( List *mylst_ptr, int elements){ Node *tmp=*mylst_ptr; for(int i=0;ivalue=rand()%50; curr->next=NULL; if ( (*mylst_ptr) == NULL ){ (*mylst_ptr)=curr; printf("%d ",(*mylst_ptr)->value); } else{ while ( (*mylst_ptr) != NULL ){ (*mylst_ptr) = (*mylst_ptr)->next; } (*mylst_ptr)=curr; printf("%d ",(*mylst_ptr)->value); } } *mylst_ptr=tmp; printf(" final=%d",(*mylst_ptr)->value); //// i get nothing here } int main () { Node *root=NULL; createList(&root,10); while (root!=NULL){ printf("%d ",root->value); root=root->next; } }
@pauloskalyvas1590
@pauloskalyvas1590 3 жыл бұрын
sorry for my English ...
@CodeVault
@CodeVault 3 жыл бұрын
Problem is with the double pointers you're using. First you have to change (*mylst_ptr) = (*mylst_ptr)->next; to mylst_ptr = &(*mylst_ptr)->next; since you want to have a double pointer to the next spot of that node. Then you also won't need the *mylst_ptr = tmp; since you set (*mylst_ptr) = curr; with curr being the first node.
@stephensagarinojr.4170
@stephensagarinojr.4170 Жыл бұрын
ahhhhhhhhh. i'm just a beginner and double pointer is giving me a real hard time
@stephensagarinojr.4170
@stephensagarinojr.4170 Жыл бұрын
it took me two days to understand the poinsters as a beginner, Finally now I understand it. Don't give up guys! just try to visualize if you're having a hard time to understand.
@CodeVault
@CodeVault Жыл бұрын
There is this video I made about double pointers: code-vault.net/lesson/tapcyuvuo6:1603733527497
@user-wz1tm5dg8y
@user-wz1tm5dg8y 6 ай бұрын
why every programer has a shirt like yours
Deallocating a linked list
11:39
CodeVault
Рет қаралды 15 М.
Add to the beginning of a doubly linked list
12:23
CodeVault
Рет қаралды 3,4 М.
Me: Don't cross there's cars coming
00:16
LOL
Рет қаралды 8 МЛН
FOOLED THE GUARD🤢
00:54
INO
Рет қаралды 64 МЛН
Reading/Writing structs to files (aka Serialization)
14:41
CodeVault
Рет қаралды 73 М.
Learn Linked Lists in 13 minutes 🔗
13:24
Bro Code
Рет қаралды 264 М.
Removing an element from a linked list
13:15
CodeVault
Рет қаралды 13 М.
you will never ask about pointers again after watching this video
8:03
Low Level Learning
Рет қаралды 2,1 МЛН
Linked List in C/C++ - Insert a node at nth position
15:15
mycodeschool
Рет қаралды 727 М.
100+ Linux Things you Need to Know
12:23
Fireship
Рет қаралды 56 М.
Me: Don't cross there's cars coming
00:16
LOL
Рет қаралды 8 МЛН