Yes, that's true. You should free any memory allocated using malloc once you are done using it. In this lesson, the intent was to show the implementation of insert function. But despite that, not having an explicit free is not doing any bad here. As soon as we are done printing all the elements, we are anyway done in the main function and the program execution will finish. And once program finishes, all of its memory is freed anyway.
For anybody getting the error "node not defined" -> after initial definition of the node and before the semicolon just put Node it looks something like this struct Node { int data; struct Node* next; }Node; If anybody wants the whole code to the one in this lesson: //INSERTION OF NODES AT THE BEGINNING AND IT'S DISPLAY #include #include struct Node { int data; struct Node* next; }Node; struct Node* head; void insert(int x); void print(); void main() { head=NULL; printf("how many numbers? "); int n,i,x; scanf("%d",&n); for(i=0;idata=x; temp->next=head; head=temp; } void print() { struct Node* temp=head; printf("List is:"); while(temp!=NULL) { printf("%d ",temp->data); temp=temp->next; } printf(" "); }
the way you changed the implementation, one using global variable, others using return and pointer to pointer, dope sir. now I know how proficient I need to be to just participate in competitive programming. thanks again.
Even if you are programming in C you can avoid to write "struct Node ..." every time by declaring the struct like this: typedef struct Node{ int data; struct Node* next; }Node; This way you will be able to declare Node variables the same way you do in C++.
Your insertion logic is correct. You may have a bug in main or print functions. Try to debug. Print your list every-time you insert a new element. So like after every call to insert_node, make a call to print. You should be able to nail down the exact issue.
THANK YOU, JUST, THANK YOU! Your work is amazing, your videos are great and the way you teach is just incredibly good. I really appreciate what you have done and have been doing.
I was using a C++ compiler. You do not need to write struct keyword in C++. Moreover, this thing is testing whether someone running this code in C can debug simple compilation errors or not. :) Anyway, thanks for noticing.
I just wanted to point it our for someone who would have the same problems as me. Btw very good tutorials and this one is the first that I'll have to watch a couple more times to completly understood.
Loved the call by reference used in insert function by you at the end where you change the value of memory reference of insert var by passing address of insert
thank you very much for this video,I was struggling a lot to understand how lists were implemented in c Even my teachers failed to make me Understand thanks a lot. you know what Iam posting this comment after implementing the lists successfully thank a lot.👍
I always struggle to understand about linked list and pointer. I got my aha moment because of this video and the previous one about linked list in c/c++. Thank you
thanks a lot for your videos! they helped me a great deal!!! can you please continue making more ? like for stacks, queues, double linked list! it would be really helpful thanks!!
Thanks for quick response..I resolved the issue. I forgot to initialize the struct to the starting point before printing it.. I spend several hours on ths but havent care abt it earlier.
Thank you for the video. Print function can also have null check for empty list if (head == NULL) { cout next = head; head = newNode; } InsertBeginning(head, 300);
Thankyou so much for this video.. i have finally found everything in a video as i was very confused with this head variable being declared different everywhere.
There are a lot of compilation errors in your code. You are writing "prinf" in your print function instead of "printf" ( t is missing) ... your functions are not returning anything even though their return type is not void. You need to read the compilation errors and try to fix the issues.
sir please do a playlist regarding python programming so it will be helpful for people like me .. as your explanation is very clear it will be understood by everyone so please do a playlist regarding python
satish sahu We have already published some videos on trees, see our complete series on data structures ru-vid.com/group/PL2_aWCzGMAwI3W_JlcBbtYTwiQSsOTa6P
Thank you. However, I am getting an error, when I compile: link.c:13:21: error: ‘Node’ undeclared (first use in this function) struct Node* temp = Node* malloc(sizeof(struct Node));
few queries... 1. Why do we require head? Can't we store and use the address of 1st node directly? 2. Why does the new node gets added at the beginning of the list?
1) To clarify you 1st node does not has address of 1st node. 1st node will have address of 2nd node, 2nd node will have address of 3rd node.......etc. Head has the address of 1st node. S0 if you use address of 1st node you will get elements from node 2 onwards and you are missing 1st node itself. I hope you understood now. 2) Node can be added to any location.
There is no need for freeing at the end of the program but someone who is learning now should learn early to free resources for the future. At my university teachers are insisting on that. After all that shows that you know better what you are doing with memory and you have better control. I hadn't googled that before to be honest but this is my humble opinion.