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(" "); }
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++.
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.
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.
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.
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 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.
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);
this the c++ code, i implemented it and sure it's working ============================= #include using namespace std; struct Node{ int data; Node* next; }; Node* head; void Insert(int x){ Node* temp = new Node; temp->data = x; temp->next = head; head = temp; } void print(){ Node* temp = head; cout
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.👍
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));
the c++ code implementation is: class linkedlist{ private: struct node{ int data; node*next; }; node*head; public: linkedlist(){ head=0; } void add(int value){ node*newnode;//declare a new node newnode=new node();//free some memory for the new node newnode->data=value;//insert it newnode->next=head; head=newnode;//the head now is pointing the element that we added to the linked list } void print(){ node*newnode=head; cout
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
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!!
what is problem in my code help my sir #include #include struct node { int data; node *next; }; struct node *head; int insert(int x) { struct node* temp=(node*)malloc(sizeof(struct node)); temp->data=x; temp->next=head; head=temp; } int Print() { struct node* temp=head; printf("list is:"); while(temp !=NULL) { printf(" %d",temp->data); temp=temp->next; } printf(" "); } int main() { head=NULL; printf("enter how many no "); int n,i,x; scanf("%d",&n); for(i=0;i
Salam Mohammed, I had the same question and I tried to free up memory in the insert function but it made my code run in over drive. it keeps printing random numbers. Let me know if you're able to free up dynamically allocated memory. Thanks!
there is no need to use free. It will delete the node from the memory. But we are doing program for "inserting" and not for "deleting". Hence, free is not used.
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.
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.
I have question that in the last case when you passed &head as an argument in Insert why we don't need to return the value of head and how the argument in Print function gets updated.
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
5:45 , since global variable head value is being changed inside of the function then head = temp only while inside the function right? Once the function is over, wouldn't head = NULL again? I thought you couldn't permanently change the value of a global variable inside of a function unless you pass by reference? I've just started programming on my own so sorry for the ignorance if I'm wrong.
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.
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.
why do we need to collect the head 11:26 . isn't it call by reference. doesnt the values updated in its address by function itself. someone clarify my doubt
@line head=temp .... is it the address of the first node (100) stored in the head? or is it the copy of temp stored to head which is pointing to null??
This programme is not being compiled in both TURBO C & CODEBLOCKS..its giving an error for that line where u used the malloc func...what should i do??plz suggest me.....
bro everything else is working fine...but I am not getting the list of numbers I have inserted in output..I am using TURBO C compiler....can u plz help me out with that......
Since head is also a pointer for struct node, won't it also have a field for data? Also why didn't we use the malloc function while creating temp inside print().
One professor at my college used this function to insert=> void addbeg(node ** start,int x)... can u please explain why pointer to pointer,or how it works?
We can simply insert the elements without using the if statement struct node* head=NULL; void print(int x) { struct node *temp=(struct node*)malloc(sizeof(struct node)); temp->data=x; temp->next=head;//here the temp will be pointed to NULL and then to the next element. head=temp;//The head is again updated here. }
sir at '7:57'... i hav a doubt that what is temp variable containing.....? Is it adderess of head node or that that adderess which head node is containing. please answr sir thankyou for reading .
Pointer variable Temp is defined inside functions only i.e Insert and Print functions. They remain alive for a short duration i.e. memory for function variables is assigned in Stack when a function call is made & de-allocated as soon as function call is over. Though the nodes (which Temp pointer variables are pointing to) are allocated memory in Heap section of memory. These nodes in heap memory have to be manually de-allocated. Check his another video on Stack vs Heap memory.
after 6 years of creating this videos ... no one created something better ! thanks alot teacher
Sadly we dont have the founder of this channel
@@bipulkumar6637 yes he is no more
Is he Lord Harsha?
@@Fabios-br The one teaching in this video is Animesh Nayan. Harsh also has a video on this channel - the one about GCD algorithm.
Hi Siddharth,
Yes, we will continue creating more videos in data structures series. :)
sir, please make video series on dynamic programming, greedy Algorithms,divide and conquer etc and some advance data structures like segment trees !!
Numbers inputs from users display on List: are joint and no space between them why?
@@convolutionalnn2582 use endl for c++ or newline character
for C
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("
");
}
Akshay Kumar Thanks a ton buddy
Akshay Kumar i
thanks bro it was helpful for me
thanks bro
thanks brother
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++.
He assumes that people are fairly new to c because of which he wants to make the code as simple as possible
Ahaa!!!
That's great man. Thanks a lot
Just as an FYI for anyone looking into doing this, this polutes the global namespace and is not recommended.
Is there a way to do this without malloc? Because I am fairly new and I have no idea what malloc is.
The most AMAZING course explained brilliantly! So grateful!
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.
Thank you for your patience, I have always had doubts here
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.
35 dislikes are those people who use ARRAY all the time XD
hahaha maybe
Well linked list is not efficient to use at all. We should avoid using linked list as much as possible
Why is that, and if so what is the most efficient way?
For RTOS (Real Time Operating Systems) linked lists are fundamental
Umm i think it depends on what you're working with
Complete code for: Insertion, Deletion and Print working:
#include
using namespace std;
struct Node{
int data;
Node* next;
};
Node* Insert(Node* head, int x)
{
Node* newN=new Node();
newN->data=x;
newN->next=NULL;
if(head!=NULL) newN->next=head;
head=newN;
return head;
}
Node* InsertNth(Node* head, int x, int nt)
{
Node* temp1=new Node();
temp1->data=x;
temp1->next=NULL;
if(nt==1)
{
temp1->next=head;
head=temp1;
return head;
}
Node* temp2=head;
for(int i=0; inext;
}
temp1->next=temp2->next;
temp2->next=temp1;
return temp2;
}
Node *DeleteNth(Node* head, int nt)
{
Node* temp1=head;
if(nt==1){
head=temp1->next;
delete temp1;
return head;
}
for(int i=0; inext;
}
Node *temp2=temp1->next;
temp1->next=temp2->next;
delete temp2;
return temp1;
}
void Print(Node* head)
{
while(head!=NULL)
{
cout
After 9:50 , No thanx I'll keep my head global.
OO BHAI....
😂
Tru that bruh!😂😂
yes bro, i m with you
Lol..
better than books,teachers,those udemy courses. You are the best. These tutorials will never get outdated.
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.
quality of content is amazing ,I am just started learning from this playlist.
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.
This was my most daunting concept. It took me 3 hours to understand this but at last I did it.
Watch this one , it is the best
seedha dimag mein ghusegaa
sara doubt clear
ua-cam.com/play/PLbRk-vKGcVtbyctZWP9SUYewwUfx75CpZ.html
@@NitishKumarTutorials thankyou sir
Thank you so much.
I finally understood this after watching it so many times.
You guys are doing great work.👍🏽
Lockdown ka poora productive utilization ho raha hai bhai.
Ek number bhai aise hi laga reh.
@@abhishekagarwal3852 😁😁
same to you bro🙏🏼
i think you're the best when it comes to c-related material, dude. thanks.
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.
Amazing, this is the first time that I realize the beauty of pointer!
Best content on Internet for learning data structures. Great work. Thanks a lot.
Exceptional video man. I was in a complete blank about how to do a linked list, but this video explained everything perfectly. Thank you.
void Insert(int x){
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
...
Error: Node undefined symbol
Correct super struct key is not mention in this program
Best data structure course on earth.
After all happy to see a video in english
-a non hindispeaker
YOU, sir, are a life savior !!!!!!!!!!!!!!!!!!
You code for work or as a hobby🤔😂
No joke he is the best. He makes everything so clear. Thank you so much sir
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.
I love your lessons! Can you please also upload c++ code implementation?
#include
using namespace std;
struct Node
{
int data;
Node* next;
};
Node* head;
void insert(int ind_input)
{
Node* temp = new Node;
temp -> data = ind_input;
temp -> next = head;
head = temp;
};
void print()
{
Node* temp = head;
while(temp != nullptr)
{
cout data next;
}
cout user_input;
int ind_input;
for(int i = 0; i < user_input; i++)
{
cout > ind_input;
insert(ind_input);
print();
};
};
@@kanicagoddard6330 thank youu
The best video of Linked List I've ever seen, a good way to explain programming :) .
best teacher ever . you healed my pointers trauma ;-)
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);
this the c++ code, i implemented it and sure it's working
=============================
#include
using namespace std;
struct Node{
int data;
Node* next;
};
Node* head;
void Insert(int x){
Node* temp = new Node;
temp->data = x;
temp->next = head;
head = temp;
}
void print(){
Node* temp = head;
cout
why you used struct instead of class??
who would be scared of programming or hate it if someone is teaching so well like this..
Thanks Jason !
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.👍
You have saved my grade in college. Thank you so much
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));
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
@@ranasikder5497 I was getting same error but now wonder how his code worked yet mine dodn't.🤔
1. mycodeschool
2. thenewboston
3. Derek Banas
you make great videos sire.
Thank you for all your efforts.
I love your desire to help people.
you're a real man.
your explanation is just awesome sir..keep it doing
the c++ code implementation is:
class linkedlist{
private:
struct node{
int data;
node*next;
};
node*head;
public:
linkedlist(){
head=0;
}
void add(int value){
node*newnode;//declare a new node
newnode=new node();//free some memory for the new node
newnode->data=value;//insert it
newnode->next=head;
head=newnode;//the head now is pointing the element that we added to the linked list
}
void print(){
node*newnode=head;
cout
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
12:08 I was taking a drink when he said it, I almost drowned! Lol
very stupid
lmaooooo good one
Lmaoo
In DeV, while programming in C language, for any declaration or manipulation of structures, you should type "struct NameStructure".
awesome ...this is what i was in search of ......very helpful to clear basic understanding ....thanks buddy....
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!!
Miss U legend !
Thank you ,this clarified a lot of things for me..
Thank you so much ....😁.... It helped me lot .... I am gonna rock my lab test ....💙💙💙💙💙💙💙💙💙💙💙💙💙💙💙💙💙💙💙
One question. Isn't this program leaking memory? Where in the code should we be deallocating with free?
Yeah same question
Thank you for these tutorials! They make the concepts of coding way easier to grasp.
Working code:
#include
#include
void Insert(int x);
void Print();
struct Nodes{
int data;
struct Nodes *next;
}Node;
struct Nodes *head;
int main(){
head = NULL;
printf("Enter number of components:
");
int n, i, x;
scanf("%d", &n);
for(i=0; i < n; i++){
printf("Enter the number: ");
scanf("%d",&x);
Insert(x);
Print();
}
}
void Insert(int x){
struct Nodes *temp = (struct Nodes *)malloc(sizeof(struct Nodes));
temp -> data = x;
temp -> next = NULL;
if(head != NULL){
temp -> next = head;
}
head = temp;
}
void Print(){
struct Nodes *temp = head;
while(temp != NULL){
printf("%d", temp-> data);
temp = temp -> next;
}
printf("
");
}
what is problem in my code help my sir
#include
#include
struct node
{
int data;
node *next;
};
struct node *head;
int insert(int x)
{
struct node* temp=(node*)malloc(sizeof(struct node));
temp->data=x;
temp->next=head;
head=temp;
}
int Print()
{
struct node* temp=head;
printf("list is:");
while(temp !=NULL)
{
printf(" %d",temp->data);
temp=temp->next;
}
printf("
");
}
int main()
{
head=NULL;
printf("enter how many no
");
int n,i,x;
scanf("%d",&n);
for(i=0;i
(struct node *)malloc ....
your videos are excellent. Your videos are very much useful to me, Many thanks for that. My warm regards to you.
where are you using "free" to deallocate the memory?
Salam Mohammed,
I had the same question and I tried to free up memory in the insert function but it made my code run in over drive. it keeps printing random numbers. Let me know if you're able to free up dynamically allocated memory.
Thanks!
sir, I use another Pointer, ex: *p and p = &head, head = head->next and i free p, this is loop ulti p point NULL
there is no need to use free. It will delete the node from the memory. But we are doing program for "inserting" and not for "deleting". Hence, free is not used.
@@gurtej5821 Wrong check your code with valgrind.
sir thaxxx a lot ,your efforts are are priceless
@mycodeschool in 2:50, we can use for(int i=0,..........) in C too
can you provide the actual file of the program for further analysis like a downloadable one? it will be a great add on to your tutorials
www.mycodeschool.com/problems/insert-a-node-in-a-linked-list-at-head
It's been 8 years. No other video on DSA is better than this.
Sad that he stopped uploading videos.
Hi is no more brother
@@mrboon8856 i'm sorry to hear about that ,what a pity
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.
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.
I have question that in the last case when you passed &head as an argument in Insert why we don't need to return the value of head and how the argument in Print function gets updated.
Hi,head is also of type node,if I am correct, we are just writing head=temp,is the data part of the head null or any garbage?
Null
bro head is a pointer variable which stores adress. so if we write like head=temp, the adress of temp node would be stored in head node.
Love u Sir Best teacher ever
Thanks.... Great way of teaching.
🙏🙏🙏🙏
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
5:45 , since global variable head value is being changed inside of the function then head = temp only while inside the function right? Once the function is over, wouldn't head = NULL again? I thought you couldn't permanently change the value of a global variable inside of a function unless you pass by reference? I've just started programming on my own so sorry for the ignorance if I'm wrong.
Shouldn't there be a free() call somewhere after the malloc call? If so, where should it be placed?
Do I need to use delete() while I practice the sample??
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.
The perils of C!
My memories of high school C are rather less convoluted.
which compiler are you using....?
If you typedef the struct Node, then you can skip the struct on Node *next right?
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.
shouldn't we use free function here?
Yes you should delete all temps and the whole linked list at the end of the program.
why do we need to collect the head 11:26 . isn't it call by reference. doesnt the values updated in its address by function itself. someone clarify my doubt
You should see the lectures for *variable* *scope* .....
Once you understand it , you won't ask it again!
@line head=temp .... is it the address of the first node (100) stored in the head? or is it the copy of temp stored to head which is pointing to null??
Can I get the source code?
is it necessary to initialize head inside the main or can we do it outside the main?
please upload boolean algebra if posiible as soon as possible....... and your videos are just awesome..thanks a lot....
Sir, which compiler you have used in this video?
Thank you so much for the video. Very nicely explained.
Thanks for the lesson its really helping me to usderstand the concept of linked list
keep doing man ......your videos are just awesome.......and much much helpfull
This programme is not being compiled in both TURBO C & CODEBLOCKS..its giving an error for that line where u used the malloc func...what should i do??plz suggest me.....
OMG.
Are you GOD of pointer?
How can you understand pointer so fucking well...
It was so cooooooooooooooooooooolllllllllllllllll.
BTW, thank you ....
Which IDE is used in the video to write the c code ????
What are the advantages and disadvantages of declaring struct node * head locally? Is it bad in terms of memory?
which compiler(software) do u use???
bro everything else is working fine...but I am not getting the list of numbers I have inserted in output..I am using TURBO C compiler....can u plz help me out with that......
Since head is also a pointer for struct node, won't it also have a field for data? Also why didn't we use the malloc function while creating temp inside print().
One professor at my college used this function to insert=> void addbeg(node ** start,int x)... can u please explain why pointer to pointer,or how it works?
4:51
We can simply insert the elements without using the if statement
struct node* head=NULL;
void print(int x)
{
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp->data=x;
temp->next=head;//here the temp will be pointed to NULL and then to the next element.
head=temp;//The head is again updated here.
}
sir at '7:57'... i hav a doubt that what is temp variable containing.....? Is it adderess of head node or that that adderess which head node is containing.
please answr sir
thankyou for reading .
What IDE are you using?
at 6:56 why is it written "temp->next = head;"? what does it do?
great explanation
Where are you compiling your code? i am getting multiple errors for this code
plz send a link for your platform
www.mycodeschool.com/problems/insert-a-node-in-a-linked-list-at-head
Do I need to clear memory which you allocated to temp. I need to make sure no memory is lost. Please tell what I can do?
phkkk auff
Pointer variable Temp is defined inside functions only i.e Insert and Print functions. They remain alive for a short duration i.e. memory for function variables is assigned in Stack when a function call is made & de-allocated as soon as function call is over.
Though the nodes (which Temp pointer variables are pointing to) are allocated memory in Heap section of memory. These nodes in heap memory have to be manually de-allocated.
Check his another video on Stack vs Heap memory.
sir is the address of head in memory will be differnt or same as that of node