I was struggling to understand swapping logic today. Your explanation and drawing helped me understand. You do not assume anything and you explain from scratch. Thanks a lot for your videos. You are one of my teachers to master datastructures.
After a year and a half of learning and practicing pointers and linked lists, one would expect to be able to seamlessly understand these kinds of things, but to my surprise it's still really hard to grasp. Pointers are confusing alone, but when swapping nodes, it's another story. And it can get even crazier: just imagine doing this on doubly linked lists... Or in circular lists! OR IN TREES!!! Anyways, I needed this to make a sorting function for linked lists, but didn't get it right until I saw this video. You kept all the details and mantained a slow pace in your explanation, just what I needed. Many teachers could learn from this... Thank you very much sir!
For adjacent nodes the swapping logic is slightly different: temp = pY -> next pY -> next = pX (without next) pX -> next = temp prevX -> next = pY You don't need to modify prevY
//This is the code which can handle two adjacent nodes either but the function assumes that first node occurs first and second node there after struct node *Swapnodes(struct node *head, struct node *first, struct node *second) { if (head == NULL) { //printf(" You are given a null header to swap nodes "); return NULL; } if (first == second) return head; //printf(" Can u see us I am head:%u I am first first:%u, I am second:%u ", head, head, second); struct node *temp; struct node *prev1 = NULL; struct node *prev2 = NULL; int found = 0, flag = 0; if (head == first) found = 1; for (temp = head; temp != NULL && !found; temp = temp->next) { printf(" I am searching second, temp is :%u", temp); if (temp->next == first) { found = 1; prev1 = temp; } } if (!found) { printf(" First not found "); return NULL; } found = 0; for (temp = head; temp != NULL; temp = temp->next) { printf(" I am searching second, temp is :%u", temp); if (temp->next == second) { found = 1; prev2 = temp; break; } } if (!found) { printf(" Second is not found "); return NULL; } if (!prev2) { printf("Unfortunately I found prev2 as null"); return NULL; } if (prev2 == first) { flag = 1; } if (prev1 != NULL) { temp = second->next; prev1->next = second; if (flag) { second->next = first; } else { second->next = first->next; prev2->next = first; } first->next = temp; return head; } temp = second->next; if (flag) { second->next = first; } else { second->next = first->next; prev2->next = first; } first->next = temp; return second; }
there is a problem . you have told the search function is a void type so it will return nothing.so if it returns nothing then how will those pointers prevX,prevY,pX,pY will work in the swap function. please explain
sir, if you want to increase your viewer you must add some background sound in video cause, it will help to motivate while we watch your videos ....i think you apply my suggestion in you'r videos thank for you'r video it help a lot..
I was struggling to understand swapping logic today. Your explanation and drawing helped me understand. You do not assume anything and you explain from scratch. Thanks a lot for your videos. You are one of my teachers to master datastructures.
After a year and a half of learning and practicing pointers and linked lists, one would expect to be able to seamlessly understand these kinds of things, but to my surprise it's still really hard to grasp.
Pointers are confusing alone, but when swapping nodes, it's another story. And it can get even crazier: just imagine doing this on doubly linked lists... Or in circular lists! OR IN TREES!!!
Anyways, I needed this to make a sorting function for linked lists, but didn't get it right until I saw this video. You kept all the details and mantained a slow pace in your explanation, just what I needed. Many teachers could learn from this... Thank you very much sir!
the way you say hello friends to till end of this video is really wonderful
I really enjoy your way of explaining the concepts of swapping nodes in. I have subscribed and will be watching all your lectures. Thank you sir.
This topic was really tricky and tough to understand but u made it very easy!
Thank you very much sir. You're a very good teacher, the step-by-step drawing really helpmed me.
For adjacent nodes the swapping logic is slightly different:
temp = pY -> next
pY -> next = pX (without next)
pX -> next = temp
prevX -> next = pY
You don't need to modify prevY
This is the best explanation I have seen. But this solution will not work if Y > X or if the two nodes is next to each other.
yeah! i already checked that
that's a swap function. You have to check that in your handler function not in Swap()
What if X and Y are adjuscent to each other like a->b or c->d will this logic works
It doesn't work in such case
@vivekanand please reply
We need your help!!!
//This is the code which can handle two adjacent nodes either but the function assumes that first node occurs first and second node there after
struct node *Swapnodes(struct node *head, struct node *first, struct node *second)
{
if (head == NULL)
{
//printf("
You are given a null header to swap nodes
");
return NULL;
}
if (first == second)
return head;
//printf("
Can u see us I am head:%u I am first first:%u, I am second:%u
", head, head, second);
struct node *temp;
struct node *prev1 = NULL;
struct node *prev2 = NULL;
int found = 0, flag = 0;
if (head == first)
found = 1;
for (temp = head; temp != NULL && !found; temp = temp->next)
{
printf("
I am searching second, temp is :%u", temp);
if (temp->next == first)
{
found = 1;
prev1 = temp;
}
}
if (!found)
{
printf("
First not found
");
return NULL;
}
found = 0;
for (temp = head; temp != NULL; temp = temp->next)
{
printf("
I am searching second, temp is :%u", temp);
if (temp->next == second)
{
found = 1;
prev2 = temp;
break;
}
}
if (!found)
{
printf("
Second is not found
");
return NULL;
}
if (!prev2)
{
printf("Unfortunately I found prev2 as null");
return NULL;
}
if (prev2 == first)
{
flag = 1;
}
if (prev1 != NULL)
{
temp = second->next;
prev1->next = second;
if (flag)
{
second->next = first;
}
else
{
second->next = first->next;
prev2->next = first;
}
first->next = temp;
return head;
}
temp = second->next;
if (flag)
{
second->next = first;
}
else
{
second->next = first->next;
prev2->next = first;
}
first->next = temp;
return second;
}
@
Vivekanand Khyade - Algorithm Every Day
In case of (prevY=null) should it be prevX->next=pY ( not prevX->next=prevY)?
yes
Yes , right
I think you are right. Where can I have the complete code?
@@sayantaniguha8519 Unfortunately, I don't have it, but you can code it yourself since the author explained the concept quite good
@@parampampam4477 ha woh toh sir ka dekh ke ban jayega...
Par phir bhi sir ka code samjhe ni hum
Really good class! Make it really clear!
u solved my problem ,explained very well thanks alot🙏🙏🙏
there is a problem . you have told the search function is a void type so it will return nothing.so if it returns nothing then how will those pointers prevX,prevY,pX,pY will work in the swap function. please explain
very nicely explained..Thanks
Сижу в 3 часа ночи и делаю по этому видосу лабу для Париматча и попутно смотрю танец сортировок. Всем советую, хороший видос.
Ушастым привет!
Very good explanation
Very well explained 🙏🏻
Nice sirrrrrr. Very good explanation
Nicely done. Much Appreciation for you !
if *a* is replaced by *b* ? Or pY == pX.next?
Your code is wrong if you want to swap to corresponding nodes then it will not work
can we make a program which work for all three cases
Yes
There is a mistake if (pervY==NULL) Than it will be prevX->next=pY not prevX->next=prevY
I think head will also be
Head=pX right?
@@sriharshasamayamanthula2692 yes
Nice 👍 I Understand Thanks 😊
sir please upload video on the topic "code explaination of how will you make binary tree using inorder and preorder ,inorder and postorder".
It is not working to swapping of A node and B node so Check it once
I don't think it would work for adjacent nodes. Correct me someone if I am not.
Nice explanation.. but the solution will not work if we have a linked list as 1-2-3-4-null and your x = 4 and y = 1 instead of x= 1 and y =4
Exactly
Just swap x and y before any operation
Nice explanation
dear please help me implementation code for sorting double linked list
Suppose we have given pointers to the nodes we want to swap and our linked list is doubly linked list
How would our code look like
I am in the same condition bro
Amazing Teaching
Too good..thnku❤
Thank you very much! everything is perfect
does this work for adjacent nodes?
just amazing ..
sir but their can be duplicates nodes
Dear sir, it doesn't work for adjacent nodes
Sir can u upload a video on sorting a linked list
Do you have source code?
You're just awesome
Good job :) thx
sir your code is not working please give source code
sir, if you want to increase your viewer you must add some background sound in video cause, it will help to motivate while we watch your videos ....i think you apply my suggestion in you'r videos thank for you'r video it help a lot..
man u teach good but please don't do fake English excent
cool english
Supamdeep Bains, What is excent?
@@tommystephen9146 dammn !
Jus to remember me i disliked video.Sorry