Best lecture on internet about programming methodology...i havent seen ever such a clear explanation about each topics..really you all are doing such a great jobs..thank u so much sir....
I don't usually go around praising yt tutorial makers but this visualization process is very cool. You have an unique way of teaching. Thanks and Good luck man
Wouldn't have been able to perform as well as I am on my C programming course without your videos! You are awesome! Thank you for taking the time to do this.
// Code in C with slightly clearer variable names in the main part of the program // To compile with gcc run this: "gcc -o linked_list2 linked_list2.c ; ./linked_list2" assuming that your file name is named: "linked_list2.c" #include #include struct Node { int data; struct Node* next; }; struct Node* head; void Print () { struct Node* temp = head; while (temp != NULL){ printf ("%d", temp->data); temp = temp->next; } printf(" "); } // The meat of the program void Insert (int data, int position) { struct Node* newNode = malloc(sizeof(struct Node)); newNode->data = data; newNode->next = NULL; if (position == 1) { newNode->next = head; head = newNode; return; } struct Node* previousNode = head; for (int i = 0; i < position - 2; i++) { previousNode = previousNode->next; } newNode->next = previousNode->next; previousNode->next = newNode; } int main () { head = NULL; Insert (2, 1); Insert (3, 2); Insert (4, 1); Insert (5, 2); Print (); }
@@shyamsundarjha5913 Because We have Started taking Node 1 as position 1 not Position 0 and we have started for loop from i=0 and at When i will be at Position-2 Our temp2 will be at position-1 Which we need.If You will take i=1 then it will be upto pos-1.
@@shyamsundarjha5913 because you always start on the head node and not before it. So if we do n-2 then we are eliminating a iteration to arrive at the n-1 node
I'm studying for my finals right now and I'm just now learning my professor literally copied most of your videos to teach us C. Like the code in his slides are IDENTICAL from this tutorial. So funny yet sad I'm paying thousands of dollars when its free here
I just want to say thank you so much. These videos are amazing. I understand linked lists so well thanks to these, and I've never seen a programming concept explained so well, not even from my teachers. You are the best!
It is arguably one of the best quality programming video I have ever seen. Thank you so much for making this video. It really helped me out from link list! God bless you sir :)
Big thanks from Indonesia, this is no doubt one of the best programming tutorial in youtube, you explained things clearly! You deserve more subscribers!
Thank you for the video. We should also take care of invalid position scenario... Eg.if list has just 3 nodes and user does insert( data=1 , n= 65 ), Inside for(i=0;inext; } will crash.. correct way is for(i=0;inext!=NULL ;i++) and node can be inserted at end. Similarly for empty list and invalid position , code can be changed to if (pos == 1 || (head == NULL && pos != 1) ) { newNode->next = head; head = newNode; return; }
for those who are confused about "n-2", just start your loop with i=1 (which means head n=1) then you can run your loop until "n-1" to avoid confusion.
I have a question. About changing 1st item second time! Clearly that code is wrong, if i want to insert something at first position, this code will point my new node to head "temp1=>Next = head" and then assign head to my new node "head = temp1", which means that "head=>Next == head"(head points to itself), and if we had a data in our list then we would lose it and also have a unnecessary occupied space in our memory, cause C/C++ do not have something like garbage collector. Why do this pal? Something like "//empty list" means nothing in logic, you make your program work properly and you do not make comments asking to not do that second time,while in your example you do that for second time and show that code is working just fine. (i mean in this comment he didn't made that mistake but video author did)
I got an exam in a weeks time but the way this guy is explaining,it has given me confidence that i can take the exam today,,,,,thumps high & fuck haters
First of all , amazing explanation. Doubt: 1. Why use the if statement at all when n=1, why not just use a while loop and do n--; 2. I am aware that it has something to do with not giving a position that does not exist in the list but i am still not clear as to why we are giving only 1 and 2 as the positions where we want to enter the numbers
if your program is going to infinity loop or not working. Just make these Changes in the for loop in the insert function : for (int i = 1; i < n - 1; i++) { temp2=temp2->next; }
WHO IS THE GUY BEHIND THIS AMAZING CHANNEL ?? (voice seems to be from) Indian English accent ) thanks a lot brother for making this channel and for explaining each and every possible query in detail
I'm very grateful for all these videos and I think it's hilarious that you put subtitles, even tho honestly I think it's better they are there. cheers :)
Great explanation. Just one thing about process layout - It is ".code -> .data/.bss -> heap -> stack" rather than ".code -> .data/.bss -> stack -> heap" :)
Sir can i ask a personal question from where u have studied computer because your lectures are too good i want to download each and every lectures but it is taking much time so i m downloading one by one it is helping me a lot I am a first year btech Computer science student...
First Thank you so much for this great explaining , second I'd like to ask some question please, why this program can't deal when n > 2 ? I've tried to deal with third or force place or any place greater than 2 it doesn't work still put the data in the second place ??
It will work Just add temp1->next=NULL; in the last line of insertNode.For Some Reason this Program Was Going in Infinite Loop but it is okay with this addition.
because i=0 corresponds to node1, i=1 to node2....... i=n-3 to node n-2. within the for loop, node n-2 carries address of node n-1. hence , at the end of the execution, temp2 stores address of node n-1. cheers!
this confusion is because he is counting the first node as 1 instead of 0. so if you start counting from 1 and do : for(i=1;inext; } you should be fine :)
Your code is not even compiling. Do not use conio.h and getch().. its not needed. And you have not written the case n==1 correctly. Check the code again.
Liked your video but can u tell me if we assign address of head node to the node we want in d place of head why are we doin temp->next=head and not simply temp=head;..Thankyou:)
how come head doesn't have a data field to it? when it is declared as a Node structure - or actually is it people it is a pointer to a node structure so all this is is a variable holding the address of a node and its set to NULL?
What is the use of giving for loop as if the termination ends upto n-2 nly.... u have already passed only 1 or 2 value in variable n ..so always when it checks it will going to take only false value as variable i can not be less than -1(n is 1) or 0(n is 2) and for any values of I....
before IF condition in insert function ... do we really need that " temp1->next =null "? because it's just an extra step we don't need to perform I think.
If condition checks if the new entry is in the first position of the linked list. whereas temp1->next =null is placed outside the if block if in case the list is empty.
the explanation was good. But I did not fully understand the part of insertion function. please explain the part in function insert() where you make a loop and set temp=temp->next.
You gotta display the whole linked list right? What that line does is traverse thorough the list,it changes the address stored in temp and then in the next line you print temp -> data (whatever data that new address stored in temp is pointing to) for example if next address of temp or node is 100 temp = temp->next will set it to that address and then you can print that data via simple cout statment
Your videos are really helpful bro. Keep up the good work i m not sure that if you have another you tube channel called "NESO Academy" because last semester i was taking logic design and the videos really helped me to get the grade i wanted. But you guys sound alike. Let me know if that's your channel.
Posting an implementation which uses the while loop as well as checks boundary conditions: /* Insert Node at a given position in a linked list head can be NULL First element in the linked list is at position 0 Node is defined as struct Node { int data; struct Node *next; } */ Node* InsertNth(Node *head, int data, int position) { Node *node = new Node; node->data = data; node->next = NULL; Node *prev = NULL; Node *current = head; int n = 0; while (n < position && current != NULL) { prev = current; current = current->next; n++; } if (prev == NULL) { node->next = head; return node; } prev->next = node; node->next = current; return head; }
how to write code to insert at the end i wrote this code void insert_end(T x) { node* temp1 = new node; temp1->data = x; temp1->next = NULL; if (head == NULL) {
at 12:17 what if we write delete temp at the end of insert function, when I coded it with writing delete temp it worked perfectly but my question is that when the control goes back it automatically deletes the temp pointer as I am doing manually in my code, so when its deleted it should also delete the data and next pointer assocaited with temp in heap but its not deleted here when the control goes back, Please tell why??
this is bad example for n-2 for loop... the next video explains it better. Rather than inserting at position 2 this video should have shown inserting at position 3. It would be more clear.
I am using codeblock compiler to run this program and there is zero warning and zero error but if I try to run the program I get a message saying that the data structure has stopped working. Please help.
Mr. Animesh Nayan! You're gem of an instructor man, seriously. ❤️
good to see another Aswani learning to code haha
brother these content was not by animesh nayan but by lord harsha suryanaryaana known as humble fool
this guy is so badass. Thorough, precise, not watered down, and still entertaining...dope.
for (i=1;inext;
}
this would have avoided unnecessary confusions for beginner.
thx
I must say im not confused at all and not a beginner... by watching this video i feel like an expert! Congrats to the team behind it..love you India
your comment helped a lot
Actually you would get a segmentation fault (core dumped) due to not properly allocating memory.
if i want to insert my node at second position. Then n=2, the loop wont work for this since n-1=1 here. Please help
Best lecture on internet about programming methodology...i havent seen ever such a clear explanation about each topics..really you all are doing such a great jobs..thank u so much sir....
I don't usually go around praising yt tutorial makers but this visualization process is very cool. You have an unique way of teaching. Thanks and Good luck man
Wouldn't have been able to perform as well as I am on my C programming course without your videos! You are awesome! Thank you for taking the time to do this.
// Code in C with slightly clearer variable names in the main part of the program
// To compile with gcc run this: "gcc -o linked_list2 linked_list2.c ; ./linked_list2" assuming that your file name is named: "linked_list2.c"
#include
#include
struct Node {
int data;
struct Node* next;
};
struct Node* head;
void Print () {
struct Node* temp = head;
while (temp != NULL){
printf ("%d", temp->data);
temp = temp->next;
}
printf("
");
}
// The meat of the program
void Insert (int data, int position) {
struct Node* newNode = malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if (position == 1) {
newNode->next = head;
head = newNode;
return;
}
struct Node* previousNode = head;
for (int i = 0; i < position - 2; i++) {
previousNode = previousNode->next;
}
newNode->next = previousNode->next;
previousNode->next = newNode;
}
int main () {
head = NULL;
Insert (2, 1);
Insert (3, 2);
Insert (4, 1);
Insert (5, 2);
Print ();
}
Thanks a million for your code.
@@shyamsundarjha5913 Because We have Started taking Node 1 as position 1 not Position 0 and we have started for loop from i=0 and at When i will be at Position-2 Our temp2 will be at position-1 Which we need.If You will take i=1 then it will be upto pos-1.
@@shyamsundarjha5913 because you always start on the head node and not before it. So if we do n-2 then we are eliminating a iteration to arrive at the n-1 node
you did not typecast the malloc part buddy
I'm studying for my finals right now and I'm just now learning my professor literally copied most of your videos to teach us C. Like the code in his slides are IDENTICAL from this tutorial. So funny yet sad I'm paying thousands of dollars when its free here
The simulation is excellent! It definitely has helped me visualize the algorithm better. Thank you!
one of the best tutorial on list list... good work!!!!! first time i understand whats this shit is
I just want to say thank you so much. These videos are amazing. I understand linked lists so well thanks to these, and I've never seen a programming concept explained so well, not even from my teachers. You are the best!
It is arguably one of the best quality programming video I have ever seen. Thank you so much for making this video. It really helped me out from link list! God bless you sir :)
Big thanks from Indonesia, this is no doubt one of the best programming tutorial in youtube, you explained things clearly! You deserve more subscribers!
You have explained things very well. I had to watch it 4 to 5 times but it really worth it. Thank you so much.....!
Thank you for the video.
We should also take care of invalid position scenario...
Eg.if list has just 3 nodes and user does insert( data=1 , n= 65 ),
Inside for(i=0;inext; } will crash..
correct way is for(i=0;inext!=NULL ;i++) and node can be inserted at end.
Similarly for empty list and invalid position , code can be changed to
if (pos == 1 || (head == NULL && pos != 1) )
{
newNode->next = head;
head = newNode;
return;
}
if(temp2->link == NULL)
{
cout
You guys are amazing. Very detailed and clear in explaining. Keep up the good work.
for those who are confused about "n-2", just start your loop with i=1 (which means head n=1) then you can run your loop until "n-1" to avoid confusion.
Thank you!! 🙏🙏🙏🙌
@@bhaswardutta8493 if we wanna insert in 2 position n=2,so i
@@convolutionalnn2582 IN THAT CASE the loop will not run because we require temp2 to be at head position which it already is
here's the source code for c:
#include
#include
void Insert(int, int);
void print();
struct Node
{
int data;
struct Node* next;
};
struct Node* head;
void main()
{
head=NULL;
Insert(3,1);
Insert(2,2);
Insert(4,2);
print();
}
void Insert(int data, int n)
{
struct Node* temp1=(struct Node*)malloc(sizeof(struct Node*));
int i;
temp1->data=data;
temp1->next=NULL;
if(n==1)//empty list
{
temp1->next=head;
head=temp1;
return;
}
struct Node* temp2=head;
for(i=0;inext;//to point temp2 to (n-1)th node after loop
}
temp1->next=temp2->next;// point new (nth)node to next node
temp2->next=temp1;// to point the previous( n-1 th) node to new node
}
void print()
{
struct Node* temp=head;
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->next;
}
puts("");
}
The same logic in c++ is not working
I have a question. About changing 1st item second time! Clearly that code is wrong, if i want to insert something at first position, this code will point my new node to head "temp1=>Next = head" and then assign head to my new node "head = temp1", which means that "head=>Next == head"(head points to itself), and if we had a data in our list then we would lose it and also have a unnecessary occupied space in our memory, cause C/C++ do not have something like garbage collector.
Why do this pal? Something like "//empty list" means nothing in logic, you make your program work properly and you do not make comments asking to not do that second time,while in your example you do that for second time and show that code is working just fine.
(i mean in this comment he didn't made that mistake but video author did)
@@ankanmaiti9864 do you face the same problem as i described? ^
it's very helpful and amazing for me,thank you so much,GOOD LUCK
Well, in that case - you will not have to traverse the list from head. You will only adjust some links in constant time.
Thanks Prashant :)
one of the best explained
I got an exam in a weeks time but the way this guy is explaining,it has given me confidence that i can take the exam today,,,,,thumps high & fuck haters
will this code work if try to insert at 3 rd position
cause u have only done insertions at first and last node
Literally the best
thank you sir.initially felt data structures very difficult but gained more confidence after watching these videos.
First of all , amazing explanation.
Doubt:
1. Why use the if statement at all when n=1, why not just use a while loop and do n--;
2. I am aware that it has something to do with not giving a position that does not exist in the list but i am still not clear as to why we are giving only 1 and 2 as the positions where we want to enter the numbers
you are like a god for me I have never seen this type explanation very nice
Should i share my code about linklist in C++. So i like the way you explain the things and i want to learn more from you.
if your program is going to infinity loop or not working.
Just make these Changes in the for loop in the insert function :
for (int i = 1; i < n - 1; i++)
{
temp2=temp2->next;
}
still not working
me too facing issue here,can any one help me?
WHO IS THE GUY BEHIND THIS AMAZING CHANNEL ?? (voice seems to be from) Indian English accent )
thanks a lot brother for making this channel and for explaining each and every possible query in detail
wow! really amazing i learned inserting a node in linked list.thank you:)
I'm very grateful for all these videos and I think it's hilarious that you put subtitles, even tho honestly I think it's better they are there. cheers :)
THank you for making this gem videos , i will definitely reach to my destination by watching this playlist.
Great explanation. Just one thing about process layout - It is ".code -> .data/.bss -> heap -> stack" rather than ".code -> .data/.bss -> stack -> heap" :)
Your videos are so clear ...thanks !! Which is that software u r using to run the code ? Can u link it or tel abt it ?
THAT👉 temp2 = head 👈IS MY SAVIOR, THE REASON WHY IT ALWAYS "SEGMENTATION FAULT" great thanks to you man 👍
Very good tutorial. I wish he continued teaching like this.
this is easy to understand and explain :)
void insert(int pos,int val) {
//declaration
Node *temp1=head;
Node *temp=new Node;
//inserting value
temp->data=val;
temp->next=NULL;
if(pos==1){
temp->next=head;
head=temp;
return;
}
else{
for (int i=2;inext;
}
temp->next=temp1->next;
temp1->next=temp;
}
What about using a typedef to declare the structure? thanks
Very very clear lecturing. Thanks a lot.
which compiler are used in above videos?? it looks pretty good than turbo c..
wow this was confusing, but it was fun, because when u understand after confusion, it is understood better, thanxxxxxxxxxx
your videos are great !!!!! its a shame u don;t make them any more :(
Yeah, he died before completing his playlist :(
Great series. I love your page. In the end however the value of head should be the address of 4 (which is 50 not 150) since head has changed.
Sir can i ask a personal question from where u have studied computer because your lectures are too good i want to download each and every lectures but it is taking much time so i m downloading one by one it is helping me a lot I am a first year btech Computer science student...
he has done his engineering from Netaji Subhash Institute of Technology, New Delhi.
@@himanshu6489 no. He has studied from IIIT, Allahabad.
how does for loop works in insertion???please explain
Best possible way to learn datastructure is through this playlist...
Well done sir
My favouite quote in this video is "Head will not be accessible everywhere!", such is life lol! Great work though, really good video.
salute lord harsha suryanaryana you will always remembered
First Thank you so much for this great explaining , second I'd like to ask some question please, why this program can't deal when n > 2 ? I've tried to deal with third or force place or any place greater than 2 it doesn't work still put the data in the second place ??
It will work Just add
temp1->next=NULL;
in the last line of insertNode.For Some Reason this Program Was Going in Infinite Loop but it is okay with this addition.
why you take i=n-2 ?
thanks for such an amazing playlist
I could not understand the for loop used. can you explain it
why not (n-1) instead of (n-2) there ?
struct Node* temp2 = (struct Node*)malloc(sizeof(struct Node*));
for(i=0;inext;
}
because i=0 corresponds to node1, i=1 to node2....... i=n-3 to node n-2.
within the for loop, node n-2 carries address of node n-1.
hence , at the end of the execution, temp2 stores address of node n-1.
cheers!
how????
for n = 1 there is a separate condition
but for n = 2 the loop will not work because i
You actually don't want to run the loop for n=2. Because the pointer is already at n-1 that is, 1st position.,
this confusion is because he is counting the first node as 1 instead of 0.
so if you start counting from 1 and do :
for(i=1;inext;
}
you should be fine :)
Do you think this is odd? For a series on data structures in C, shouldn't we start counting at 0 for an Insert implementation?
This video made my day..really awesome video to understand
Sir,if we ask user to enter position for data and initially user enter position greater than 1 lets say 10. so how can we handle this situation???
Can you show how this can work on a doubly linked list? I don't know how that one works.
Your code is not even compiling. Do not use conio.h and getch().. its not needed. And you have not written the case n==1 correctly. Check the code again.
Best lecture i have ever seen
Liked your video but can u tell me if we assign address of head node to the node we want in d place of head why are we doin temp->next=head and not simply temp=head;..Thankyou:)
how come head doesn't have a data field to it? when it is declared as a Node structure - or actually is it people it is a pointer to a node structure so all this is is a variable holding the address of a node and its set to NULL?
we will write struct Node* temp1 = (struct Node*)malloc(sizeof(struct Node*)) or
struct Node* temp1 = (struct Node*)malloc(sizeof(struct Node))
thanks for your help. I finally find out why the code doesn't run. phewww!!!
What is the reason that memory is not freed even though nodes are allocated memory dynamically on the heap?
Explanation is great. Could you tell how are you writing this code? I mean what compiler? Xcode ?
What is the use of giving for loop as if the termination ends upto n-2 nly.... u have already passed only 1 or 2 value in variable n ..so always when it checks it will going to take only false value as variable i can not be less than -1(n is 1) or 0(n is 2) and for any values of I....
In the case for insertion at n== 1, why are we setting temp1->next = head ?
Sir, thank you very much for your help, i really appreciate it
before IF condition in insert function ... do we really need that " temp1->next =null "? because it's just an extra step we don't need to perform I think.
If condition checks if the new entry is in the first position of the linked list. whereas temp1->next =null is placed outside the if block if in case the list is empty.
yes we do need it in case if list is empty!
the explanation was good. But I did not fully understand the part of insertion function.
please explain the part in function insert() where you make a loop and set
temp=temp->next.
action ka'men that's a step to traverse a linked list.. Watch previous lectures or trace the whole loop yourself
You gotta display the whole linked list right? What that line does is traverse thorough the list,it changes the address stored in temp and then in the next line you print temp -> data (whatever data that new address stored in temp is pointing to)
for example if next address of temp or node is 100 temp = temp->next will set it to that address and then you can print that data via simple cout statment
awesome, what tool you are using for whiteboarding?
wonderful ......this is some special type of lacture......
Won't it be if (n == 0){} if you want to insert at the first position? Are you counting from 1, 2.... instead of 0, 1..... ?
Your videos are really helpful bro. Keep up the good work i m not sure that if you have another you tube channel called "NESO Academy" because last semester i was taking logic design and the videos really helped me to get the grade i wanted. But you guys sound alike. Let me know if that's your channel.
Shouldn't we delete the dynamically allocated memory before the function returns to avoid memory leak ?? Please suggest I am right?
why is temp != NULL required?
Posting an implementation which uses the while loop as well as checks boundary conditions:
/*
Insert Node at a given position in a linked list
head can be NULL
First element in the linked list is at position 0
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
Node* InsertNth(Node *head, int data, int position)
{
Node *node = new Node;
node->data = data;
node->next = NULL;
Node *prev = NULL;
Node *current = head;
int n = 0;
while (n < position && current != NULL) {
prev = current;
current = current->next;
n++;
}
if (prev == NULL) {
node->next = head;
return node;
}
prev->next = node;
node->next = current;
return head;
}
how if you are not given any position(n) and just given a reference value and you have to insert the node after the reference valued node??
I think the heap illustration in 14:53 is incorrect . 150 is written under head but it shown to point to 50.
what If want to make the function with 3 parameters: data, position, and a pointer to the pointer of the header?
Working code in C++ :
#include
using namespace std;
struct node
{
int data;
node* next;
};
node* head;
void Insert(int data,int n)
{
node* temp=new node();
temp->data=data;
temp->next=NULL;
if(n==1)
{
temp->next=head;
head=temp;
}
else
{
node* temp1=head;
for(int i=0;inext;
}
temp->next=temp1->next;
temp1->next=temp;
}
}
void Print()
{ cout
If n=2...then. i
how to write code to insert at the end
i wrote this code
void insert_end(T x)
{
node* temp1 = new node;
temp1->data = x;
temp1->next = NULL;
if (head == NULL)
{
temp1->next = head;
head = temp1;
return;
}
node* temp2 = head;
while (temp2 != NULL)
{
temp2 = temp2->next;
}
temp1->next = temp2->next;
temp2->next = temp1;
}
but it gives me error derefrencing null pointer
Thanks for the great explanation !
but the code doesn't work although it compiles...have you tried it yourself?
What you faced? It works pretty fine
Why did you use n-2 instead of n-1 and why does it breaks when i choose n-1?
marvbasbas68 Their program's base is 1 instead of 0 which is by default.
Is it possible to code link list without using the pointer where only the array can be used? if it is possible to tell me how
why does the for loop not run for (3,2) but runs for (5,2) ??? conditions are the same are they not?
it will not work when n = 2 because in the the i
at 12:17 what if we write delete temp at the end of insert function, when I coded it with writing delete temp it worked perfectly but my question is that when the control goes back it automatically deletes the temp pointer as I am doing manually in my code, so when its deleted it should also delete the data and next pointer assocaited with temp in heap but its not deleted here when the control goes back, Please tell why??
Great tutorial👍.
Doubt: why didn't you deallocate memory..from heap..???
great video, extremely helpful and understandable
this is bad example for n-2 for loop... the next video explains it better. Rather than inserting at position 2 this video should have shown inserting at position 3. It would be more clear.
best tutorial.Thanks for this.I want to know the ide u r using
I am using codeblock compiler to run this program and there is zero warning and zero error but if I try to run the program I get a message saying that the data structure has stopped working. Please help.
Krishna Dogney do you get solution of this problem i am suffering frm same one
Add #include
May be it can help.
Because of malloc
BEST LECTURE EVER
what is the need of return ; in if(n==1) block ?
Sir u could go little slow, yet great explanation.
but of in arguement i put n not equals to 1 in strting ..like(2,3)...than will it give any error??
why the loop will run till (i < n-2) can anyone tell
Iam doing... for(int i=8/9/10...(any value); i
My code ends with segmentation fault but it seems I wrote it correctly what is the problem then?