This is the best site to learn Data Structures. I've been searching many online sites to get this topic clear But I couldn't find any. But this is the best site to learn. Thank you Sir you are explaining in a very understandable way.
if you get confused in the while loop then can follow these steps pos - - ; while(i < pos) { ptr = ptr-> next ; i++ ; } // initialize i at beginning where i = 1
I have been attending my DSA lectures for the past two weeks with a blank mind. However, after watching your videos, this topic seems much easier and more interesting. Performing different operations on it now feels achievable. You nailed it bro!
Thank you so much for making your videos. My C program professor did a terrible job teaching this concept and I paid for the class. I understand it so much more with your videos so thank you.
I think the code fails if we insert the position as 1 cause pos-- will be 0 and then it missed the position or goes on infinte loop... correct me if I am wrong ?
Sir, why does add at end function have head as parameter? The one you wrote in last class took another variable ptr which was updated each time. In this program the link between the first and second nodes is lost right?
Sir you have used add_at_end(head,98) but where is the code for updating address of first node because initially it will be null .I have seen previous video where you have written code for inserting a t end but there was a pointer temp so could update address of first node but here there is nothing such ,what we need to do ?
But there is one issue with this that when there is only one node then there is no node ahead so what will you do then ? Because when ever we insert a node between two nodes the code given in the video works but I it will have issues when there is only one node and one only
sir, if you help me, I will be appreciated . I tried to do it with a counter. For example, when counter == position, add insert node but it doesn't work. Can u help me?
Can you please upload more videos.. covering all topics of dsuc... I promise I will share this channel and videos among my friends 🤞❤️ please please... Apke alawa meko kahi ni samajh aya.. please
is it necessary to know position well in advance or it can be given by the user during run time?? if position is known we can implement the function based on that position ,if position is given by the user then how to implement a generalised code .
You will read the position from the user and then you will traverse between the nodes until you reach the place you want to put your new node. Let's say, the input is given as 4, 4th node to be particular, then you will traverse 4 - 1 times but not including the last node so the code will traverse 2 times for the fourth node for (int i = 1; i < pos - 1; i++) // { // check if the current temp is not null and move until you find the node to be linked your newly created node }
sir isme apne 98 k link main 3000 dala hai jbki uske next ka address 4000 hai 67 pr 3000 hai ar uske next k link ka address 3000 hai sir so its conflict i think that can you recheck that 6:12 min.
Add at end function calling has been changed ...in previous lecture you have called ptr and data..I.e you returned the ptr valu to main function 🧐could you show me the whole program
void inseertPos(node* head, int value, int pos) { node* new_node = new node; new_node-> data = value; node* ptr = pos - 1; now_node -> next = ptr -> next; ptr -> next = new_node; } //how is that ?
Is it possible to calculate the insert position in the main function itself, and pass ptr as the reference node while not having to pass the additional positional parameter to the insert function?
Another logic if anyone is unable to get the head around in the above video:- void add_at_pos(struct node *head, int pos, int value) { struct node *temp = (struct node *)malloc(sizeof(struct node)); struct node *ptr = head; temp->data = value; int p = 1; while (p != (pos - 1)) { ptr = ptr->next; p++; } temp->next = ptr->next; ptr->next = temp; }
Why are we not returning the values from the functions to the main function, even though we are using call by value....... Plz respond if anyone aware of it.
Basically it is calling by value but that value which is being passed is the adress only so that's why we are not returning . ...take an example we are passing head means the value of head i .e the adress only
Sorry sir,Incase I have a new node like yours let say node 5and I want to join it somewhere at,the end,the beginning,and after second node. Mention pointers which will be involved in each
@@carlossantamaria1820 Then u have to redirect the code to another function for this specific condition containing command to insert a node at first location which neso academy have made a video on.
Sir what is the logic behind decrementing pos i.e pos--; I couldn't understand why you are decreasing this value.? we are given a node to be inserted at pos 3. Then y u are decreasing it?
The loop: while(pos != 1) { ptr = ptr -> next; pos--; } will not be executed since the condition does not match on the first iteration. Therefore, no changes will be made to ptr, which is, by default, on reference head. Thus it will append to the beginning of the list
my logic: int count = 2; while((*temp).link != NULL) { temp = (*temp).link; count++; if(count == position) break; } haha i know it's rushed and not that well-written
then the newly created node's linker variable (next) will point to the previous head address and the previous head address will be set to the newly created array's address.
I think that we just need one pointer (double pointer); void insert_at_pos(node** traverse, int pos, int data) { while (pos--) { if (!pos) { node *newnode = malloc(sizeof(node)); newnode->data = data; newnode->ptr = NULL; newnode->ptr = *traverse; *traverse = newnode; } else traverse = &(*traverse)->ptr; // &(head->ptr) } }
typedef struct node{ int data; struct node* link; }s; void insert_node_certain_position(s* head){ s* temp=(s*)malloc(sizeof(s)); int d,n;s* ptr=head;s* p=head; printf("enter the data you want to insert at cetain position"); scanf("%d",&d); temp->data=d; temp->link=NULL; printf("enter the node no. after which you want to insert the node"); scanf("%d",&n); for(int i=1;ilink; } temp->link=ptr; p->link=temp; }
This is the best site to learn Data Structures. I've been searching many online sites to get this topic clear But I couldn't find any. But this is the best site to learn. Thank you Sir you are explaining in a very understandable way.
uploading speed is super slow
Same ❤️🤞
if you get confused in the while loop then can follow these steps
pos - - ;
while(i < pos)
{ ptr = ptr-> next ;
i++ ;
}
// initialize i at beginning where i = 1
What if I use
While (i
I was thinking to use for loop with
For (I=0;i
I have been struggling so much for the past few hours understanding this topic....n u explained it so well......thank you so much
hey! can you tell me why sir has done pos--, before using while loop
@@RaviSingh-fr2qh To traverse the list & to update the ptr->link value till the desired address after which node needs to be inserted.
I have been attending my DSA lectures for the past two weeks with a blank mind. However, after watching your videos, this topic seems much easier and more interesting. Performing different operations on it now feels achievable. You nailed it bro!
With a blank mind, that got me🤣🤣. I still continuing in blank..wishing all at once would trigger at some point.
what r u learning now?
@@herohera3497 i know nothin.
Thanku. Sir, U r god for me, who has made my concept so clear...
Thank you so much for making your videos. My C program professor did a terrible job teaching this concept and I paid for the class. I understand it so much more with your videos so thank you.
Now it's time solve Gate previous year questions. ....
But coding part is not asked in gate
this DS course is simply ingenious ... admirations
This is way easier to understand than whatever diagram my textbook provided, thanks sir
WHY IS YOUR EXPLAINING SO GOOOD
at 6:11 , In second node the link part should contain address 4000. Otherwise very nice explanation.
I really loved the way in which you are teaching sir.
Great job and i am expecting from you complete data structure Series like Trees Stack Queue
U R the Great Explainer 👍👍👍
Yes plz upload the video lectures of queue stack and tree also ...plz it's great request from us 🙏
@@omkarbhanushali1658
Yes budy🥂🥂
Thanks for uploading this master piece ❤
I love Neso Academy. 💝
Below is the Universal Code that works for all the positions including the position=1. We need to give separate condition for 1st position.
#include
#include
struct node{
int data;
struct node *link;
};
struct node *add_at_end(struct node *ptr, int data){
struct node *temp= malloc(sizeof(struct node));
temp->data = data;
temp->link = NULL;
ptr->link = temp;
return temp;
}
void add_at_pos(struct node **head, int data, int pos){
struct node *ptr = *head;
struct node *ptr2 = malloc (sizeof(struct node));
ptr2->data = data;
ptr2->link = NULL;
if(pos==1)
{
ptr2->link=ptr;
*head=ptr2;
}
else {
pos--;
while(pos != 1){
ptr = ptr->link;
pos--;
}
ptr2->link = ptr->link;
ptr->link = ptr2;
}
}
int main()
{
struct node *head = malloc(sizeof(struct node));
head->data = 45;
head->link = NULL;
struct node *ptr = head;
ptr= add_at_end(ptr, 98);
ptr= add_at_end(ptr, 3);
ptr= add_at_end(ptr, 88);
ptr = head;
int data = 76, position =1
;
add_at_pos(&head, data, position);
struct node *ptr1 = head;
while(ptr1 != NULL){
printf("%d ", ptr1->data);
ptr1=ptr1->link;
}
return 0;
}
Brother,thanks a lottt ❤
Thank you
"Hey bro, you're the real OG! Thanks a lot!"
Please don't stop this ❤❤
This is fantastic video your video solve my doubt thank you
sir you make the dsa more interested !👍
Sir what if the pos is 1.. that means we need to insert at beginning of the ll?
This video helped me a lot, Thank you very much!
Isnt there a mistake? 5:57 node with data 98 should contain link to 4000
yes
Really helpful
Thank you so much sir! You have good teaching skill !
You deserved for likes and subscribes!
just wonderful explanation
Thank u sir, you explained this topic very easily👏
I think the code fails if we insert the position as 1 cause pos-- will be 0 and then it missed the position or goes on infinte loop...
correct me if I am wrong ?
yes, you will have a segmentaion fault
@@gamar1226 so how to fix it?
Sir, why does add at end function have head as parameter? The one you wrote in last class took another variable ptr which was updated each time. In this program the link between the first and second nodes is lost right?
#include
using namespace std;
struct node{
int data;
node *next;
};
void add_at_end(node *head, int d){
node *ptr, *temp;
temp = new node;
temp->data = d;
temp->next = NULL;
ptr = head;
while(ptr->next != NULL){
ptr = ptr->next;
}
ptr->next = temp;
}
void add_at_pos(node *head, int data, int pos){
node *ptr = head;
node *ptr2 = new node;
ptr2->data = data;
ptr2->next = NULL;
pos--;
while(pos != 1){
ptr = ptr->next;
pos--;
}
ptr2->next = ptr->next;
ptr->next = ptr2;
}
int main()
{
node *head = new node;
head->data = 45;
head->next = NULL;
add_at_end(head, 98);
add_at_end(head, 3);
int data = 67, position = 3;
add_at_pos(head, data, position);
node *ptr = head;
while(ptr != NULL){
cout
#include
#include
struct node
{
int data;
struct node *link;
};
void add_at_end(struct node *head, int d)
{
struct node *ptr, *temp;
temp = (struct node*)malloc(sizeof(struct node));
temp->data = d;
temp->link= NULL;
ptr = head;
while(ptr->link != NULL)
{
ptr = ptr->link;
}
ptr->link = temp;
}
void add_at_pos(struct node* head,int data,int pos)
{
struct node *ptr = head;
struct node *ptr2 = malloc(sizeof(struct node));
ptr2->data = data;
ptr2->link = NULL;
pos--;
while(pos != 1)
{
ptr=ptr->link;
pos--;
}
ptr2->link = ptr->link;
ptr->link = ptr2;
}
int main()
{
struct node *head = malloc(sizeof(struct node));
head->data = 45;
head->link = NULL;
add_at_end(head,98);
add_at_end(head,3);
int data = 67;
int position = 3;
add_at_pos(head, data, position);
struct node *ptr = head;
while(ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->link;
}
return 0;
}
@@punky6320 thanks man , code was really helpfull .
Sir you have used add_at_end(head,98) but where is the code for updating address of first node because initially it will be null .I have seen previous video where you have written code for inserting a t end but there was a pointer temp so could update address of first node but here there is nothing such ,what we need to do ?
if we generalize this code for any other linked list, then can we write while(pos!=pos-1) and then the remaining code?
Thank you very much, you helped me a lot!
But there is one issue with this that when there is only one node then there is no node ahead so what will you do then ?
Because when ever we insert a node between two nodes the code given in the video works but I it will have issues when there is only one node and one only
sir, if you help me, I will be appreciated . I tried to do it with a counter. For example, when counter == position, add insert node but it doesn't work. Can u help me?
this was great thank you
Can you please upload more videos.. covering all topics of dsuc... I promise I will share this channel and videos among my friends 🤞❤️ please please... Apke alawa meko kahi ni samajh aya.. please
is it necessary to know position well in advance or it can be given by the user during run time??
if position is known we can implement the function based on that position ,if position is given by the user then how to implement a generalised code .
You will read the position from the user and then you will traverse between the nodes until you reach the place you want to put your new node. Let's say, the input is given as 4, 4th node to be particular, then you will traverse 4 - 1 times but not including the last node so the code will traverse 2 times for the fourth node
for (int i = 1; i < pos - 1; i++) //
{
// check if the current temp is not null and move until you find the node to be linked your newly created node
}
Amazing 👍🏻
Sir, very eager for upcoming videos on data structure.
sir isme apne 98 k link main 3000 dala hai jbki uske next ka address 4000 hai 67 pr 3000 hai ar uske next k link ka address 3000 hai sir so its conflict i think that can you recheck that 6:12 min.
The use of a caboose is another way and it simplifies all the code. struct node *createList() {struct node *nd = malloc(sizeof(struct node)); nd->link = nd; return nd;}; void insertBefore(struct node *nd, int new_data) {struct node *new_nd = malloc(sizeof(struct node)); new_nd->link = nd->link; new_nd->data = nd->data; nd->link = new_link; nd->data = new_data;}; Now all the other functions get simplified : void add_beg(struct node *head, int new_data) {insertBefore(head, new_data);}; void add_at_end(struct node *head, int new_data) {struct node *nd = head; while (nd->link != nd) {nd = nd->link;}; insertBefore(nd, new_data);}; void add_at_position(struct node *head, int new_data, in pos) {struct node *nd = head; --pos; while (nd->link != nd && pos > 0) {nd = nd->link; --pos;}; insertBefore(nd, new_data);};
instead using while we can use for loop it is easy to traverse
do more videos ,,,,very helpful to me
thank you for the explanation. Can I ask if I want it to be in second position the while loop condition should be while( pos != 0) ?
Add at end function calling has been changed ...in previous lecture you have called ptr and data..I.e you returned the ptr valu to main function 🧐could you show me the whole program
what is the condition for inserting at position 1?
void inseertPos(node* head, int value, int pos)
{
node* new_node = new node;
new_node-> data = value;
node* ptr = pos - 1;
now_node -> next = ptr -> next;
ptr -> next = new_node;
}
//how is that ?
Thanks bro❤
The code has an obvious flaw. What if I want to insert into the pos 1?
If you want it at pos 1 , u can juste insert it at the beggining
what if the user decide to add the node at the beginning of the linked list? wouldn't the head of the linked list get messed up?
Below is the Universal Code that works for all the positions including the position=1. We need to give separate condition for 1st position.
#include
#include
struct node{
int data;
struct node *link;
};
struct node *add_at_end(struct node *ptr, int data){
struct node *temp= malloc(sizeof(struct node));
temp->data = data;
temp->link = NULL;
ptr->link = temp;
return temp;
}
void add_at_pos(struct node **head, int data, int pos){
struct node *ptr = *head;
struct node *ptr2 = malloc (sizeof(struct node));
ptr2->data = data;
ptr2->link = NULL;
if(pos==1)
{
ptr2->link=ptr;
*head=ptr2;
}
else {
pos--;
while(pos != 1){
ptr = ptr->link;
pos--;
}
ptr2->link = ptr->link;
ptr->link = ptr2;
}
}
int main()
{
struct node *head = malloc(sizeof(struct node));
head->data = 45;
head->link = NULL;
struct node *ptr = head;
ptr= add_at_end(ptr, 98);
ptr= add_at_end(ptr, 3);
ptr= add_at_end(ptr, 88);
ptr = head;
int data = 76, position =1
;
add_at_pos(&head, data, position);
struct node *ptr1 = head;
while(ptr1 != NULL){
printf("%d ", ptr1->data);
ptr1=ptr1->link;
}
return 0;
}
don't I need to return head from add_at_pos function?
what about inserting at position = 0;
how to insert at begining using this metho ?
Is it possible to calculate the insert position in the main function itself, and pass ptr as the reference node while not having to pass the additional positional parameter to the insert function?
Another logic if anyone is unable to get the head around in the above video:-
void add_at_pos(struct node *head, int pos, int value)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
struct node *ptr = head;
temp->data = value;
int p = 1;
while (p != (pos - 1))
{
ptr = ptr->next;
p++;
}
temp->next = ptr->next;
ptr->next = temp;
}
Thanks man. 🤝🏻
Why are we not returning the values from the functions to the main function, even though we are using call by value....... Plz respond if anyone aware of it.
Basically it is calling by value but that value which is being passed is the adress only so that's why we are not returning .
...take an example we are passing head means the value of head i .e the adress only
Sir here you use that code ,is that code full program of insert a node at any position???
Plz reply that
#include
using namespace std;
struct node{
int data;
node *next;
};
void add_at_end(node *head, int d){
node *ptr, *temp;
temp = new node;
temp->data = d;
temp->next = NULL;
ptr = head;
while(ptr->next != NULL){
ptr = ptr->next;
}
ptr->next = temp;
}
void add_at_pos(node *head, int data, int pos){
node *ptr = head;
node *ptr2 = new node;
ptr2->data = data;
ptr2->next = NULL;
pos--;
while(pos != 1){
ptr = ptr->next;
pos--;
}
ptr2->next = ptr->next;
ptr->next = ptr2;
}
int main()
{
node *head = new node;
head->data = 45;
head->next = NULL;
add_at_end(head, 98);
add_at_end(head, 3);
int data = 67, position = 3;
add_at_pos(head, data, position);
node *ptr = head;
while(ptr != NULL){
cout
@@punky6320 thanks:)
Yes
Sorry sir,Incase I have a new node like yours let say node 5and I want to join it somewhere at,the end,the beginning,and after second node.
Mention pointers which will be involved in each
code given in video are showing a lot of error after compilation..? is given code is correct??
#include
using namespace std;
struct node{
int data;
node *next;
};
void add_at_end(node *head, int d){
node *ptr, *temp;
temp = new node;
temp->data = d;
temp->next = NULL;
ptr = head;
while(ptr->next != NULL){
ptr = ptr->next;
}
ptr->next = temp;
}
void add_at_pos(node *head, int data, int pos){
node *ptr = head;
node *ptr2 = new node;
ptr2->data = data;
ptr2->next = NULL;
pos--;
while(pos != 1){
ptr = ptr->next;
pos--;
}
ptr2->next = ptr->next;
ptr->next = ptr2;
}
int main()
{
node *head = new node;
head->data = 45;
head->next = NULL;
add_at_end(head, 98);
add_at_end(head, 3);
int data = 67, position = 3;
add_at_pos(head, data, position);
node *ptr = head;
while(ptr != NULL){
cout
@@punky6320 Below is the Universal Code that works for all the positions including the position=1. We need to give separate condition for 1st position.
#include
#include
struct node{
int data;
struct node *link;
};
struct node *add_at_end(struct node *ptr, int data){
struct node *temp= malloc(sizeof(struct node));
temp->data = data;
temp->link = NULL;
ptr->link = temp;
return temp;
}
void add_at_pos(struct node **head, int data, int pos){
struct node *ptr = *head;
struct node *ptr2 = malloc (sizeof(struct node));
ptr2->data = data;
ptr2->link = NULL;
if(pos==1)
{
ptr2->link=ptr;
*head=ptr2;
}
else {
pos--;
while(pos != 1){
ptr = ptr->link;
pos--;
}
ptr2->link = ptr->link;
ptr->link = ptr2;
}
}
int main()
{
struct node *head = malloc(sizeof(struct node));
head->data = 45;
head->link = NULL;
struct node *ptr = head;
ptr= add_at_end(ptr, 98);
ptr= add_at_end(ptr, 3);
ptr= add_at_end(ptr, 88);
ptr = head;
int data = 76, position =1
;
add_at_pos(&head, data, position);
struct node *ptr1 = head;
while(ptr1 != NULL){
printf("%d ", ptr1->data);
ptr1=ptr1->link;
}
return 0;
}
#include
#include
struct node
{
int data;
struct node *link;
};
void add_at_end(struct node *head, int d)
{
struct node *ptr, *temp;
temp = (struct node*)malloc(sizeof(struct node));
temp->data = d;
temp->link= NULL;
ptr = head;
while(ptr->link != NULL)
{
ptr = ptr->link;
}
ptr->link = temp;
}
void add_at_pos(struct node* head,int data,int pos)
{
struct node *ptr = head;
struct node *ptr2 = malloc(sizeof(struct node));
ptr2->data = data;
ptr2->link = NULL;
pos--;
while(pos != 1)
{
ptr=ptr->link;
pos--;
}
ptr2->link = ptr->link;
ptr->link = ptr2;
}
int main()
{
struct node *head = malloc(sizeof(struct node));
head->data = 45;
head->link = NULL;
add_at_end(head,98);
add_at_end(head,3);
int data = 67;
int position = 3;
add_at_pos(head, data, position);
struct node *ptr = head;
while(ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->link;
}
return 0;
}
if you want to insert element at first position you can check the code:
#include
#include
typedef struct node{
int data;
struct node* link;
}node;
void out(node* p){
while(p!=NULL)
{
printf("%d\t",p->data);
p=p->link;
}
}
void add_node(node** p, int data)
{
node* temp= (node*)malloc(sizeof(node));
temp->link=NULL;
temp->data=data;
(*p)->link=temp;
*p=temp;
}
void clear(node* p){
node* ptr=p;
while(p!=NULL)
{
p=p->link;
free(ptr);
ptr=p;
}
}
void add_node_certain(node** ptr, int pos,int data){
node* temp=(node*)malloc(sizeof(node));
temp->link=NULL;
temp->data=data;
if(pos!=1)
{
node* p=(*ptr);
int i;
for(i=1;ilink;
temp->link=p->link;
p->link=temp;
}
else
{
temp->link=*ptr;
*ptr=temp;
}
}
int main()
{
node* head=(node*)malloc(sizeof(node));
head->link=NULL;
head->data=10;
node* ptr=head;
add_node(&ptr,20);
add_node(&ptr,30);
add_node(&ptr,40);
ptr=head;
add_node_certain(&head,1,999);//second argument=position and third argument =value;
ptr=head;
out(ptr);
}
Clear function will not get executed...add a call function to clear at the end of main function
what if we want to insert the node at position 1 (just after head)
If you mean position 2 then it'll be fine
@@ihateracistandblackpeople4272 but what if i want to insert a node at the beggining? i can't
@@carlossantamaria1820 Then u have to redirect the code to another function for this specific condition containing command to insert a node at first location which neso academy have made a video on.
@@ihateracistandblackpeople4272 oh,ty, I thought this was going to work in this code as well
@@carlossantamaria1820 np
why u don't use return function??
My logic :
void Add_at_pos( struct node *head,int data,int pos){
struct node *temp = malloc(sizeof(struct node));
temp -> data = data;
temp -> next = NULL;
struct node *ptr = head;
for(int i=1 ; i next;
}
temp -> next = ptr -> next;
ptr -> next = temp;
}
can I use this function to insert at the beginning?
@@nguyenan9181 no u can't
Thanks bhai
there's an issue if we want to insert at position 1
Nice class
Sir next video is private why ??
It was scheduled for 8 PM, now it is public.
@@nesoacademy OOH THANK YOU SIR
Sir what is the logic behind decrementing pos i.e pos--;
I couldn't understand why you are decreasing this value.? we are given a node to be inserted at pos 3.
Then y u are decreasing it?
To traverse the list & to update the ptr->link value till the desired address after which node needs to be inserted.
What if the user entered position 1!
Will it work?
The loop:
while(pos != 1) {
ptr = ptr -> next;
pos--;
} will not be executed since the condition does not match on the first iteration. Therefore, no changes will be made to ptr, which is, by default, on reference head. Thus it will append to the beginning of the list
This is call by value
thank you 8/04/2022 at 1:08 am
Another Logic :
for (i = 0; i < pos - 2; i++)
{
ptr = ptr->link;
}
Thanks bro
my logic:
int count = 2;
while((*temp).link != NULL) {
temp = (*temp).link;
count++;
if(count == position) break;
}
haha i know it's rushed and not that well-written
what if we want to instert at position 1?
then the newly created node's linker variable (next) will point to the previous head address and the previous head address will be set to the newly created array's address.
This program is not working properly when I take pos=1
#include
#include
struct node
{
int data;
struct node *link;
};
void add_at_end(struct node *head, int d)
{
struct node *ptr, *temp;
temp = (struct node*)malloc(sizeof(struct node));
temp->data = d;
temp->link= NULL;
ptr = head;
while(ptr->link != NULL)
{
ptr = ptr->link;
}
ptr->link = temp;
}
void add_at_pos(struct node* head,int data,int pos)
{
struct node *ptr = head;
struct node *ptr2 = malloc(sizeof(struct node));
ptr2->data = data;
ptr2->link = NULL;
pos--;
while(pos != 1)
{
ptr=ptr->link;
pos--;
}
ptr2->link = ptr->link;
ptr->link = ptr2;
}
int main()
{
struct node *head = malloc(sizeof(struct node));
head->data = 45;
head->link = NULL;
add_at_end(head,98);
add_at_end(head,3);
int data = 67;
int position = 3;
add_at_pos(head, data, position);
struct node *ptr = head;
while(ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->link;
}
return 0;
}
My exam is today :)
At some positions of video making is not in a clear view please retify it
I only miss comments in code to say perfect!
A slower paced and lesser hurried explanation would be great, otherwise the content is good
Better than Jenny's
I liked
jaldi jaldi dalo sir pls pls
jaldi jaldi dalo sir pls pls
jaldi jaldi dalo sir pls pls
jaldi jaldi dalo sir pls pls
jaldi jaldi dalo sir pls pls
boht hawsi ho yaar tum
I think that we just need one pointer (double pointer);
void insert_at_pos(node** traverse, int pos, int data)
{
while (pos--)
{
if (!pos)
{
node *newnode = malloc(sizeof(node));
newnode->data = data;
newnode->ptr = NULL;
newnode->ptr = *traverse;
*traverse = newnode;
}
else
traverse = &(*traverse)->ptr; // &(head->ptr)
}
}
this doesn't work if position is 1
#include
using namespace std;
struct node{
int data;
node *next;
};
void add_at_end(node *head, int d){
node *ptr, *temp;
temp = new node;
temp->data = d;
temp->next = NULL;
ptr = head;
while(ptr->next != NULL){
ptr = ptr->next;
}
ptr->next = temp;
}
void add_at_pos(node *head, int data, int pos){
node *ptr = head;
node *ptr2 = new node;
ptr2->data = data;
ptr2->next = NULL;
pos--;
while(pos != 1){
ptr = ptr->next;
pos--;
}
ptr2->next = ptr->next;
ptr->next = ptr2;
}
int main()
{
node *head = new node;
head->data = 45;
head->next = NULL;
add_at_end(head, 98);
add_at_end(head, 3);
int data = 67, position = 3;
add_at_pos(head, data, position);
node *ptr = head;
while(ptr != NULL){
cout
Yes
I also tried but it was giving infinite loop
@@educationalonly5941 Below is the Universal Code that works for all the positions including the position=1. We need to give separate condition for 1st position.
#include
#include
struct node{
int data;
struct node *link;
};
struct node *add_at_end(struct node *ptr, int data){
struct node *temp= malloc(sizeof(struct node));
temp->data = data;
temp->link = NULL;
ptr->link = temp;
return temp;
}
void add_at_pos(struct node **head, int data, int pos){
struct node *ptr = *head;
struct node *ptr2 = malloc (sizeof(struct node));
ptr2->data = data;
ptr2->link = NULL;
if(pos==1)
{
ptr2->link=ptr;
*head=ptr2;
}
else {
pos--;
while(pos != 1){
ptr = ptr->link;
pos--;
}
ptr2->link = ptr->link;
ptr->link = ptr2;
}
}
int main()
{
struct node *head = malloc(sizeof(struct node));
head->data = 45;
head->link = NULL;
struct node *ptr = head;
ptr= add_at_end(ptr, 98);
ptr= add_at_end(ptr, 3);
ptr= add_at_end(ptr, 88);
ptr = head;
int data = 76, position =1
;
add_at_pos(&head, data, position);
struct node *ptr1 = head;
while(ptr1 != NULL){
printf("%d ", ptr1->data);
ptr1=ptr1->link;
}
return 0;
}
@@nikitamittal3559 Thanks a lot buddy ,this covers all the previous lectures....❤💖✌
T̤h̤a̤n̤k̤s̤ f̤o̤r̤ h̤a̤r̤d̤ w̤o̤r̤k̤s̤ s̤i̤r̤
double free detected in tcache 2
Thanku sir acha smjhaya.
typedef struct node{
int data;
struct node* link;
}s;
void insert_node_certain_position(s* head){
s* temp=(s*)malloc(sizeof(s));
int d,n;s* ptr=head;s* p=head;
printf("enter the data you want to insert at cetain position");
scanf("%d",&d);
temp->data=d;
temp->link=NULL;
printf("enter the node no. after which you want to insert the node");
scanf("%d",&n);
for(int i=1;ilink;
}
temp->link=ptr;
p->link=temp;
}