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

Поділитися
Вставка
  • Опубліковано 5 лис 2024

КОМЕНТАРІ • 141

  • @bbennyjoseph8589
    @bbennyjoseph8589 4 роки тому +102

    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.

  • @miss_queen-yf6bv
    @miss_queen-yf6bv 6 місяців тому +8

    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

  • @binduaradhya3514
    @binduaradhya3514 4 роки тому +35

    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 4 роки тому

      hey! can you tell me why sir has done pos--, before using while loop

    • @nikitamittal3559
      @nikitamittal3559 3 роки тому +2

      @@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 Рік тому +8

    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 11 місяців тому

      With a blank mind, that got me🤣🤣. I still continuing in blank..wishing all at once would trigger at some point.

    • @herohera3497
      @herohera3497 11 місяців тому

      what r u learning now?

    • @hallucinat0ry
      @hallucinat0ry 11 місяців тому

      @@herohera3497 i know nothin.

  • @steventalik4782
    @steventalik4782 2 роки тому +5

    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.

  • @kajalmondal9745
    @kajalmondal9745 4 роки тому +43

    Now it's time solve Gate previous year questions. ....

    • @indian1747
      @indian1747 6 місяців тому +1

      But coding part is not asked in gate

  • @RaviShankar-ow9pu
    @RaviShankar-ow9pu 4 роки тому +13

    Thanku. Sir, U r god for me, who has made my concept so clear...

  • @vasilvasilev6225
    @vasilvasilev6225 3 роки тому +2

    this DS course is simply ingenious ... admirations

  • @nikitamittal3559
    @nikitamittal3559 3 роки тому +8

    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;
    }

  • @Drill-ing
    @Drill-ing 4 роки тому +14

    Great job and i am expecting from you complete data structure Series like Trees Stack Queue
    U R the Great Explainer 👍👍👍

    • @omkarbhanushali1658
      @omkarbhanushali1658 4 роки тому +1

      Yes plz upload the video lectures of queue stack and tree also ...plz it's great request from us 🙏

    • @Drill-ing
      @Drill-ing 4 роки тому

      @@omkarbhanushali1658
      Yes budy🥂🥂

  • @hiteshmahi
    @hiteshmahi 3 роки тому +7

    at 6:11 , In second node the link part should contain address 4000. Otherwise very nice explanation.

  • @vishalprajapati6154
    @vishalprajapati6154 2 місяці тому +1

    Thanks for uploading this master piece ❤

  • @srikantabanerjee1547
    @srikantabanerjee1547 2 роки тому +3

    Sir what if the pos is 1.. that means we need to insert at beginning of the ll?

  • @aniketjagtap2143
    @aniketjagtap2143 11 місяців тому +1

    sir you make the dsa more interested !👍

  • @dipeshsamrawat7957
    @dipeshsamrawat7957 3 роки тому +2

    I love Neso Academy. 💝

  • @gamingharsh21
    @gamingharsh21 3 роки тому +1

    This is fantastic video your video solve my doubt thank you

  • @Shaziya_786
    @Shaziya_786 8 місяців тому

    just wonderful explanation

  • @faizzamir3965
    @faizzamir3965 9 місяців тому

    Please don't stop this ❤❤

  • @punky6320
    @punky6320 3 роки тому +1

    Thank you so much sir! You have good teaching skill !
    You deserved for likes and subscribes!

  • @domrajdanny7416
    @domrajdanny7416 3 роки тому +2

    Really helpful

  • @muaazulhassan1721
    @muaazulhassan1721 3 роки тому +2

    This video helped me a lot, Thank you very much!

  • @_txt_7398
    @_txt_7398 Рік тому +3

    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

  • @guitarkeen4256
    @guitarkeen4256 3 роки тому +5

    Isnt there a mistake? 5:57 node with data 98 should contain link to 4000

  • @shashankkumar851
    @shashankkumar851 3 роки тому +1

    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. 🤝🏻

  • @raghavaulli437
    @raghavaulli437 5 місяців тому

    instead using while we can use for loop it is easy to traverse

  • @samikshabijwe7403
    @samikshabijwe7403 Рік тому +1

    if we generalize this code for any other linked list, then can we write while(pos!=pos-1) and then the remaining code?

  • @pranathikodicherla3283
    @pranathikodicherla3283 4 роки тому +5

    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 роки тому +4

      #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 роки тому +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;
      }

    • @user-cd8cg3yr1q
      @user-cd8cg3yr1q 2 роки тому +1

      @@punky6320 thanks man , code was really helpfull .

  • @kumarishivangi3792
    @kumarishivangi3792 2 роки тому +1

    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

  • @babysuyash
    @babysuyash 3 роки тому +5

    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 3 роки тому

      @@gamar1226 so how to fix it?

  • @mayuritare5511
    @mayuritare5511 3 роки тому +1

    Thank u sir, you explained this topic very easily👏

  • @dominiquefortin5345
    @dominiquefortin5345 2 роки тому +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);};

  • @draganjelic3549
    @draganjelic3549 3 роки тому +1

    Thank you very much, you helped me a lot!

  • @debuti
    @debuti Рік тому +1

    The code has an obvious flaw. What if I want to insert into the pos 1?

  • @_txt_7398
    @_txt_7398 Рік тому +1

    My exam is today :)

  • @anniamatthews6803
    @anniamatthews6803 Рік тому +1

    this was great thank you

  • @alikoohi4821
    @alikoohi4821 Рік тому

    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) ?

  • @saketkumar7335
    @saketkumar7335 4 роки тому +1

    Sir, very eager for upcoming videos on data structure.

  • @GaneshKumar-vh4it
    @GaneshKumar-vh4it 2 роки тому +1

    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 роки тому +1

      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
      }

  • @fadi551000
    @fadi551000 Рік тому +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 ?

  • @___vijay___
    @___vijay___ 4 роки тому +2

    Another Logic :
    for (i = 0; i < pos - 2; i++)
    {
    ptr = ptr->link;
    }

    • @prateekjain7785
      @prateekjain7785 3 роки тому +1

      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

  • @indanadeevena4597
    @indanadeevena4597 11 місяців тому +1

    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

  • @sudeepmulumudi
    @sudeepmulumudi 3 роки тому +1

    do more videos ,,,,very helpful to me

  • @shrutimagdum9685
    @shrutimagdum9685 3 роки тому +1

    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 ?

  • @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?

  • @stephanomagingamwita
    @stephanomagingamwita 11 місяців тому

    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

  • @mdikbalhosen1711
    @mdikbalhosen1711 3 роки тому +1

    what is the condition for inserting at position 1?

  • @thereisnoname0859
    @thereisnoname0859 3 роки тому +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?

  • @nithyashree6746
    @nithyashree6746 7 місяців тому

    Thanks bro❤

  • @_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 Рік тому +1

      Clear function will not get executed...add a call function to clear at the end of main function

  • @arunkumarr2302
    @arunkumarr2302 4 роки тому +1

    Amazing 👍🏻

  • @reverbism
    @reverbism 2 роки тому

    thank you 8/04/2022 at 1:08 am

  • @harshalkumar4538
    @harshalkumar4538 3 роки тому +1

    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 роки тому +5

      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;
      }

  • @shankarpange695
    @shankarpange695 Місяць тому

    This is call by value

  • @nimishsahu9902
    @nimishsahu9902 Рік тому

    what about inserting at position = 0;
    how to insert at begining using this metho ?

  • @mallinaprudhvi4066
    @mallinaprudhvi4066 2 роки тому +1

    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

  • @IM4fLEX
    @IM4fLEX 3 роки тому

    Thanks bhai

  • @rinilkr3684
    @rinilkr3684 4 роки тому

    Nice class

  • @junwooabbei487
    @junwooabbei487 2 роки тому

    there's an issue if we want to insert at position 1

  • @tarunjoshi9532
    @tarunjoshi9532 7 місяців тому

    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.

  • @ishtiaqurrahman-fc9go
    @ishtiaqurrahman-fc9go 10 місяців тому

    why u don't use return function??

  • @mehmedkukavica8076
    @mehmedkukavica8076 3 роки тому +1

    I only miss comments in code to say perfect!

  • @IShowwSpeedsui
    @IShowwSpeedsui 3 роки тому +2

    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 роки тому +1

      @@punky6320 thanks:)

    • @educationalonly5941
      @educationalonly5941 3 роки тому

      Yes

  • @rajendrasarswat9127
    @rajendrasarswat9127 Рік тому

    don't I need to return head from add_at_pos function?

  • @anirudhbukka5413
    @anirudhbukka5413 3 роки тому

    A slower paced and lesser hurried explanation would be great, otherwise the content is good

  • @raviprakashmishra2860
    @raviprakashmishra2860 4 роки тому +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

  • @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.

  • @___vijay___
    @___vijay___ 4 роки тому +1

    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

  • @ShivarajNallagorla
    @ShivarajNallagorla 8 місяців тому

    At some positions of video making is not in a clear view please retify it

  • @prodiptaghosh6809
    @prodiptaghosh6809 4 роки тому +2

    Sir next video is private why ??

  • @mattmen
    @mattmen 4 роки тому +2

    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 4 роки тому +1

      **by adding the new link i meant the new node sorry for that haha. Cheers!

    • @mattmen
      @mattmen 4 роки тому +1

      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 роки тому +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

  • @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 роки тому +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;
      }

  • @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

  • @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;
      }

  • @ayoubferdjani8097
    @ayoubferdjani8097 6 місяців тому

    I liked

  • @dhairyashah5886
    @dhairyashah5886 3 роки тому

    what if we want to instert at position 1?

    • @followthewhiterabbit7559
      @followthewhiterabbit7559 2 роки тому +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.

  • @merajbaig283
    @merajbaig283 4 роки тому +3

    T̤h̤a̤n̤k̤s̤ f̤o̤r̤ h̤a̤r̤d̤ w̤o̤r̤k̤s̤ s̤i̤r̤

  • @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)
    }
    }

  • @s_4589
    @s_4589 4 роки тому

    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....❤💖✌

  • @alaaelnagar2410
    @alaaelnagar2410 12 днів тому

    double free detected in tcache 2

  • @Mrmokuplays
    @Mrmokuplays 11 місяців тому

    Thanku sir acha smjhaya.