Single Linked List (Inserting a Node at a Certain Position)

  Рет қаралды 302,160

Neso Academy

Neso Academy

Күн бұрын

Data Structures: Inserting a Node at a certain position in a Singly Linked List
Topics discussed:
1) Adding a node in a Singly Linked List to a certain position.
2) C program for inserting a node at a certain position in case of the Singly Linked List.
C Programming Lectures: goo.gl/7Eh2SS
Follow Neso Academy on Instagram: @nesoacademy(bit.ly/2XP63OE)
Follow me on Instagram: @jaspreetedu(bit.ly/2YX26E5)
Contribute: www.nesoacademy...
Memberships: bit.ly/2U7YSPI
Books: www.nesoacademy...
Website ► www.nesoacademy...
Forum ► forum.nesoacade...
Facebook ► goo.gl/Nt0PmB
Twitter ► / nesoacademy
Music:
Axol x Alex Skrindo - You [NCS Release]
#DataStructuresByNeso #DataStructures #LinkedList #SingleLinkedList

Пікірлер: 137
@bbennyjoseph8589
@bbennyjoseph8589 3 жыл бұрын
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.
@user-zo9ye6de5e
@user-zo9ye6de5e 3 жыл бұрын
uploading speed is super slow
@kumarishivangi3792
@kumarishivangi3792 2 жыл бұрын
Same ❤️🤞
@miss_queen-yf6bv
@miss_queen-yf6bv 4 ай бұрын
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
@suvamoyghorai5247
@suvamoyghorai5247 4 күн бұрын
What if I use While (i
@binduaradhya3514
@binduaradhya3514 4 жыл бұрын
I have been struggling so much for the past few hours understanding this topic....n u explained it so well......thank you so much
@RaviSingh-fr2qh
@RaviSingh-fr2qh 3 жыл бұрын
hey! can you tell me why sir has done pos--, before using while loop
@nikitamittal3559
@nikitamittal3559 3 жыл бұрын
@@RaviSingh-fr2qh To traverse the list & to update the ptr->link value till the desired address after which node needs to be inserted.
@Ne0n425
@Ne0n425 10 ай бұрын
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!
@hallucinat0ry
@hallucinat0ry 10 ай бұрын
With a blank mind, that got me🤣🤣. I still continuing in blank..wishing all at once would trigger at some point.
@herohera3497
@herohera3497 9 ай бұрын
what r u learning now?
@hallucinat0ry
@hallucinat0ry 9 ай бұрын
@@herohera3497 i know nothin.
@RaviShankar-ow9pu
@RaviShankar-ow9pu 4 жыл бұрын
Thanku. Sir, U r god for me, who has made my concept so clear...
@kajalmondal9745
@kajalmondal9745 4 жыл бұрын
Now it's time solve Gate previous year questions. ....
@indian1747
@indian1747 4 ай бұрын
But coding part is not asked in gate
@steventalik4782
@steventalik4782 2 жыл бұрын
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.
@vishalprajapati6154
@vishalprajapati6154 15 күн бұрын
Thanks for uploading this master piece ❤
@muhammadshahab5749
@muhammadshahab5749 4 жыл бұрын
Great job and i am expecting from you complete data structure Series like Trees Stack Queue U R the Great Explainer 👍👍👍
@omkarbhanushali1658
@omkarbhanushali1658 4 жыл бұрын
Yes plz upload the video lectures of queue stack and tree also ...plz it's great request from us 🙏
@muhammadshahab5749
@muhammadshahab5749 4 жыл бұрын
@@omkarbhanushali1658 Yes budy🥂🥂
@nikitamittal3559
@nikitamittal3559 3 жыл бұрын
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; }
@rahmansaif4087
@rahmansaif4087 Жыл бұрын
Brother,thanks a lottt ❤
@shobanajohn9743
@shobanajohn9743 5 ай бұрын
Thank you
@user-wi8zd8xv5d
@user-wi8zd8xv5d 25 күн бұрын
"Hey bro, you're the real OG! Thanks a lot!"
@vasilvasilev6225
@vasilvasilev6225 3 жыл бұрын
this DS course is simply ingenious ... admirations
@hiteshmahi
@hiteshmahi 3 жыл бұрын
at 6:11 , In second node the link part should contain address 4000. Otherwise very nice explanation.
@gamingharsh21
@gamingharsh21 2 жыл бұрын
This is fantastic video your video solve my doubt thank you
@domrajdanny7416
@domrajdanny7416 3 жыл бұрын
Really helpful
@aniketjagtap2143
@aniketjagtap2143 9 ай бұрын
sir you make the dsa more interested !👍
@muaazulhassan1721
@muaazulhassan1721 3 жыл бұрын
This video helped me a lot, Thank you very much!
@srikantabanerjee1547
@srikantabanerjee1547 2 жыл бұрын
Sir what if the pos is 1.. that means we need to insert at beginning of the ll?
@dipeshsamrawat7957
@dipeshsamrawat7957 3 жыл бұрын
I love Neso Academy. 💝
@guitarkeen4256
@guitarkeen4256 3 жыл бұрын
Isnt there a mistake? 5:57 node with data 98 should contain link to 4000
@lighthouse18
@lighthouse18 3 жыл бұрын
yes
@anniamatthews6803
@anniamatthews6803 Жыл бұрын
this was great thank you
@Shaziya_786
@Shaziya_786 6 ай бұрын
just wonderful explanation
@draganjelic3549
@draganjelic3549 3 жыл бұрын
Thank you very much, you helped me a lot!
@babysuyash
@babysuyash 3 жыл бұрын
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 ?
@gamar1226
@gamar1226 3 жыл бұрын
yes, you will have a segmentaion fault
@annayatsenko379
@annayatsenko379 2 жыл бұрын
@@gamar1226 so how to fix it?
@punky6320
@punky6320 3 жыл бұрын
Thank you so much sir! You have good teaching skill ! You deserved for likes and subscribes!
@nithyashree6746
@nithyashree6746 5 ай бұрын
Thanks bro❤
@faizzamir3965
@faizzamir3965 7 ай бұрын
Please don't stop this ❤❤
@dominiquefortin5345
@dominiquefortin5345 2 жыл бұрын
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);};
@mayuritare5511
@mayuritare5511 3 жыл бұрын
Thank u sir, you explained this topic very easily👏
@sudeepmulumudi
@sudeepmulumudi 3 жыл бұрын
do more videos ,,,,very helpful to me
@raghavaulli437
@raghavaulli437 3 ай бұрын
instead using while we can use for loop it is easy to traverse
@kumarishivangi3792
@kumarishivangi3792 2 жыл бұрын
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
@pranathikodicherla3283
@pranathikodicherla3283 3 жыл бұрын
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?
@punky6320
@punky6320 3 жыл бұрын
#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
@gamingwithindian8492
@gamingwithindian8492 2 жыл бұрын
#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; }
@user-cd8cg3yr1q
@user-cd8cg3yr1q 2 жыл бұрын
@@punky6320 thanks man , code was really helpfull .
@samikshabijwe7403
@samikshabijwe7403 Жыл бұрын
if we generalize this code for any other linked list, then can we write while(pos!=pos-1) and then the remaining code?
@saketkumar7335
@saketkumar7335 4 жыл бұрын
Sir, very eager for upcoming videos on data structure.
@fadi551000
@fadi551000 Жыл бұрын
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 ?
@arunkumarr2302
@arunkumarr2302 4 жыл бұрын
Amazing 👍🏻
@debuti
@debuti Жыл бұрын
The code has an obvious flaw. What if I want to insert into the pos 1?
@shashankkumar851
@shashankkumar851 3 жыл бұрын
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; }
@chs649
@chs649 2 жыл бұрын
Thanks man. 🤝🏻
@indanadeevena4597
@indanadeevena4597 9 ай бұрын
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
@thereisnoname0859
@thereisnoname0859 2 жыл бұрын
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?
@alikoohi4821
@alikoohi4821 10 ай бұрын
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) ?
@shrutimagdum9685
@shrutimagdum9685 3 жыл бұрын
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 ?
@GaneshKumar-vh4it
@GaneshKumar-vh4it 2 жыл бұрын
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 .
@followthewhiterabbit7559
@followthewhiterabbit7559 2 жыл бұрын
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 }
@_txt_7398
@_txt_7398 Жыл бұрын
My exam is today :)
@_txt_7398
@_txt_7398 Жыл бұрын
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; }
@nguyenan9181
@nguyenan9181 Жыл бұрын
can I use this function to insert at the beginning?
@_nabin_8848
@_nabin_8848 Жыл бұрын
@@nguyenan9181 no u can't
@user-eg3yh6yk8e
@user-eg3yh6yk8e 9 ай бұрын
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
@IM4fLEX
@IM4fLEX 3 жыл бұрын
Thanks bhai
@mdikbalhosen1711
@mdikbalhosen1711 3 жыл бұрын
what is the condition for inserting at position 1?
@rinilkr3684
@rinilkr3684 4 жыл бұрын
Nice class
@anishgupta8938
@anishgupta8938 3 жыл бұрын
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?
@mallinaprudhvi4066
@mallinaprudhvi4066 2 жыл бұрын
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.
@mohd.saifshaikh2028
@mohd.saifshaikh2028 Жыл бұрын
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
@harshalkumar4538
@harshalkumar4538 3 жыл бұрын
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?
@nikitamittal3559
@nikitamittal3559 3 жыл бұрын
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; }
@ayoubferdjani8097
@ayoubferdjani8097 4 ай бұрын
I liked
@reverbism
@reverbism 2 жыл бұрын
thank you 8/04/2022 at 1:08 am
@raviprakashmishra2860
@raviprakashmishra2860 4 жыл бұрын
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
@harshalkumar4538
@harshalkumar4538 3 жыл бұрын
boht hawsi ho yaar tum
@rajendrasarswat9127
@rajendrasarswat9127 Жыл бұрын
don't I need to return head from add_at_pos function?
@nimishsahu9902
@nimishsahu9902 11 ай бұрын
what about inserting at position = 0; how to insert at begining using this metho ?
@___vijay___
@___vijay___ 4 жыл бұрын
Another Logic : for (i = 0; i < pos - 2; i++) { ptr = ptr->link; }
@prateekjain7785
@prateekjain7785 3 жыл бұрын
Thanks bro
@hetaeramancer
@hetaeramancer 2 жыл бұрын
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
@mehmedkukavica8076
@mehmedkukavica8076 3 жыл бұрын
I only miss comments in code to say perfect!
@Mrmokuplays
@Mrmokuplays 9 ай бұрын
Thanku sir acha smjhaya.
@ishtiaqurrahman-fc9go
@ishtiaqurrahman-fc9go 8 ай бұрын
why u don't use return function??
@_nabin_8848
@_nabin_8848 Жыл бұрын
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); }
@lawrencemuthui
@lawrencemuthui 11 ай бұрын
Clear function will not get executed...add a call function to clear at the end of main function
@___vijay___
@___vijay___ 4 жыл бұрын
What if the user entered position 1! Will it work?
@anishgupta8938
@anishgupta8938 3 жыл бұрын
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
@tarunjoshi9532
@tarunjoshi9532 5 ай бұрын
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.
@IShowwSpeedsui
@IShowwSpeedsui 3 жыл бұрын
Better than Jenny's
@rimpinag6346
@rimpinag6346 4 жыл бұрын
Sir here you use that code ,is that code full program of insert a node at any position??? Plz reply that
@punky6320
@punky6320 3 жыл бұрын
#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
@Shalinity
@Shalinity 3 жыл бұрын
@@punky6320 thanks:)
@educationalonly5941
@educationalonly5941 3 жыл бұрын
Yes
@prodiptaghosh6809
@prodiptaghosh6809 4 жыл бұрын
Sir next video is private why ??
@nesoacademy
@nesoacademy 4 жыл бұрын
It was scheduled for 8 PM, now it is public.
@prodiptaghosh6809
@prodiptaghosh6809 4 жыл бұрын
@@nesoacademy OOH THANK YOU SIR
@mattmen
@mattmen 3 жыл бұрын
I have a question respecting the update of the links (step 3) Why would this won't work? I have checked it and it doesn't even add the new link in any position :( ptr2 -> link = ptr ; ptr = ptr2 ; Thks in advance!!!
@mattmen
@mattmen 3 жыл бұрын
**by adding the new link i meant the new node sorry for that haha. Cheers!
@mattmen
@mattmen 3 жыл бұрын
I'm having trouble understanding why is necessary to reference the link of some node instead of the node itself Like we did in the lecture with the function "addAtTheBeginning"
@punky6320
@punky6320 3 жыл бұрын
#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
@user-mx9fq5kq3p
@user-mx9fq5kq3p 6 ай бұрын
At some positions of video making is not in a clear view please retify it
@junwooabbei487
@junwooabbei487 2 жыл бұрын
there's an issue if we want to insert at position 1
@adarshsingh1823
@adarshsingh1823 3 жыл бұрын
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?
@nikitamittal3559
@nikitamittal3559 3 жыл бұрын
To traverse the list & to update the ptr->link value till the desired address after which node needs to be inserted.
@abhishekshankar6305
@abhishekshankar6305 3 жыл бұрын
what if we want to insert the node at position 1 (just after head)
@ihateracistandblackpeople4272
@ihateracistandblackpeople4272 3 жыл бұрын
If you mean position 2 then it'll be fine
@carlossantamaria1820
@carlossantamaria1820 3 жыл бұрын
@@ihateracistandblackpeople4272 but what if i want to insert a node at the beggining? i can't
@ihateracistandblackpeople4272
@ihateracistandblackpeople4272 3 жыл бұрын
@@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.
@carlossantamaria1820
@carlossantamaria1820 3 жыл бұрын
@@ihateracistandblackpeople4272 oh,ty, I thought this was going to work in this code as well
@ihateracistandblackpeople4272
@ihateracistandblackpeople4272 3 жыл бұрын
@@carlossantamaria1820 np
@anirudhbukka5413
@anirudhbukka5413 3 жыл бұрын
A slower paced and lesser hurried explanation would be great, otherwise the content is good
@luckytiwari878
@luckytiwari878 3 жыл бұрын
code given in video are showing a lot of error after compilation..? is given code is correct??
@punky6320
@punky6320 3 жыл бұрын
#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
@nikitamittal3559
@nikitamittal3559 3 жыл бұрын
@@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; }
@gamingwithindian8492
@gamingwithindian8492 2 жыл бұрын
#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; }
@dhairyashah5886
@dhairyashah5886 2 жыл бұрын
what if we want to instert at position 1?
@followthewhiterabbit7559
@followthewhiterabbit7559 2 жыл бұрын
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.
@accessdenied9393
@accessdenied9393 3 жыл бұрын
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) } }
@sonaliverma5406
@sonaliverma5406 2 жыл бұрын
This program is not working properly when I take pos=1
@gamingwithindian8492
@gamingwithindian8492 2 жыл бұрын
#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; }
@merajbaig283
@merajbaig283 4 жыл бұрын
T̤h̤a̤n̤k̤s̤ f̤o̤r̤ h̤a̤r̤d̤ w̤o̤r̤k̤s̤ s̤i̤r̤
@s_4589
@s_4589 3 жыл бұрын
this doesn't work if position is 1
@punky6320
@punky6320 3 жыл бұрын
#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
@educationalonly5941
@educationalonly5941 3 жыл бұрын
Yes I also tried but it was giving infinite loop
@nikitamittal3559
@nikitamittal3559 3 жыл бұрын
@@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; }
@aeroabrar_31
@aeroabrar_31 3 жыл бұрын
@@nikitamittal3559 Thanks a lot buddy ,this covers all the previous lectures....❤💖✌
Single Linked List (Inserting a Node at the End)
5:48
Neso Academy
Рет қаралды 373 М.
Electric Flying Bird with Hanging Wire Automatic for Ceiling Parrot
00:15
PEDRO PEDRO INSIDEOUT
00:10
MOOMOO STUDIO [무무 스튜디오]
Рет қаралды 26 МЛН
Men Vs Women Survive The Wilderness For $500,000
31:48
MrBeast
Рет қаралды 59 МЛН
you will never ask about pointers again after watching this video
8:03
Low Level Learning
Рет қаралды 2,2 МЛН
Single Linked List (Deleting the Node at a Particular Position)
9:43
Premature Optimization
12:39
CodeAesthetic
Рет қаралды 803 М.
Inserting the Data at the Beginning (Singly Linked List vs. Array)
7:28
Single Linked List (Deleting the First Node)
3:47
Neso Academy
Рет қаралды 172 М.
Fastest Way to Learn ANY Programming Language: 80-20 rule
8:24
Sahil & Sarra
Рет қаралды 843 М.
Linked List in C/C++ - Insert a node at nth position
15:15
mycodeschool
Рет қаралды 730 М.
Learn Linked Lists in 13 minutes 🔗
13:24
Bro Code
Рет қаралды 291 М.
Electric Flying Bird with Hanging Wire Automatic for Ceiling Parrot
00:15