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

Поделиться
HTML-код
  • Опубликовано: 22 янв 2025

Комментарии • 143

  • @bbennyjoseph8589
    @bbennyjoseph8589 4 года назад +109

    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 9 месяцев назад +15

    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 года назад +37

    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 Год назад

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

    • @herohera3497
      @herohera3497 Год назад

      what r u learning now?

    • @hallucinat0ry
      @hallucinat0ry Год назад

      @@herohera3497 i know nothin.

  • @RaviShankar-ow9pu
    @RaviShankar-ow9pu 4 года назад +13

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

  • @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 года назад +50

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

    • @indian1747
      @indian1747 8 месяцев назад +2

      But coding part is not asked in gate

  • @vasilvasilev6225
    @vasilvasilev6225 4 года назад +2

    this DS course is simply ingenious ... admirations

  • @Yokie05
    @Yokie05 Месяц назад

    This is way easier to understand than whatever diagram my textbook provided, thanks sir

  • @leteky8698
    @leteky8698 Месяц назад

    WHY IS YOUR EXPLAINING SO GOOOD

  • @hiteshmahi
    @hiteshmahi 3 года назад +7

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

  • @PayalKeshari-k4s
    @PayalKeshari-k4s 24 дня назад

    I really loved the way in which you are teaching sir.

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

  • @vishalprajapati6154
    @vishalprajapati6154 5 месяцев назад +1

    Thanks for uploading this master piece ❤

  • @dipeshsamrawat7957
    @dipeshsamrawat7957 3 года назад +2

    I love Neso Academy. 💝

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

  • @faizzamir3965
    @faizzamir3965 Год назад

    Please don't stop this ❤❤

  • @gamingharsh21
    @gamingharsh21 3 года назад +1

    This is fantastic video your video solve my doubt thank you

  • @aniketjagtap2143
    @aniketjagtap2143 Год назад +1

    sir you make the dsa more interested !👍

  • @srikantabanerjee1547
    @srikantabanerjee1547 2 года назад +3

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

  • @muaazulhassan1721
    @muaazulhassan1721 4 года назад +2

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

  • @guitarkeen4256
    @guitarkeen4256 3 года назад +6

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

  • @domrajdanny7416
    @domrajdanny7416 3 года назад +2

    Really helpful

  • @punky6320
    @punky6320 4 года назад +1

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

  • @Shaziya_786
    @Shaziya_786 11 месяцев назад

    just wonderful explanation

  • @mayuritare5511
    @mayuritare5511 4 года назад +1

    Thank u sir, you explained this topic very easily👏

  • @babysuyash
    @babysuyash 4 года назад +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?

  • @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 4 года назад +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 .

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

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

  • @draganjelic3549
    @draganjelic3549 3 года назад +1

    Thank you very much, you helped me a lot!

  • @TahaQuddus
    @TahaQuddus 28 дней назад

    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

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

  • @anniamatthews6803
    @anniamatthews6803 2 года назад +1

    this was great thank you

  • @kumarishivangi3792
    @kumarishivangi3792 3 года назад +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

  • @GaneshKumar-vh4it
    @GaneshKumar-vh4it 3 года назад +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 3 года назад +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
      }

  • @arunkumarr2302
    @arunkumarr2302 4 года назад +1

    Amazing 👍🏻

  • @saketkumar7335
    @saketkumar7335 4 года назад +1

    Sir, very eager for upcoming videos on data structure.

  • @tarunjoshi9532
    @tarunjoshi9532 9 месяцев назад

    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.

  • @dominiquefortin5345
    @dominiquefortin5345 3 года назад +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);};

  • @raghavaulli437
    @raghavaulli437 8 месяцев назад

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

  • @sudeepmulumudi
    @sudeepmulumudi 3 года назад +1

    do more videos ,,,,very helpful to me

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

  • @indanadeevena4597
    @indanadeevena4597 Год назад +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

  • @mdikbalhosen1711
    @mdikbalhosen1711 3 года назад +1

    what is the condition for inserting at position 1?

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

  • @nithyashree6746
    @nithyashree6746 9 месяцев назад

    Thanks bro❤

  • @debuti
    @debuti Год назад +1

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

    • @Doumauppermoon
      @Doumauppermoon 4 дня назад

      If you want it at pos 1 , u can juste insert it at the beggining

  • @harshalkumar4538
    @harshalkumar4538 4 года назад +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;
      }

  • @rajendrasarswat9127
    @rajendrasarswat9127 Год назад

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

  • @nimishsahu9902
    @nimishsahu9902 Год назад

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

  • @anishgupta8938
    @anishgupta8938 4 года назад

    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?

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

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

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

    • @Shalinity
      @Shalinity 4 года назад +1

      @@punky6320 thanks:)

    • @educationalonly5941
      @educationalonly5941 4 года назад

      Yes

  • @stephanomagingamwita
    @stephanomagingamwita Год назад

    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

  • @luckytiwari878
    @luckytiwari878 4 года назад

    code given in video are showing a lot of error after compilation..? is given code is correct??

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

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

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

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

  • @ishtiaqurrahman-fc9go
    @ishtiaqurrahman-fc9go Год назад

    why u don't use return function??

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

  • @IM4fLEX
    @IM4fLEX 3 года назад

    Thanks bhai

  • @junwooabbei487
    @junwooabbei487 2 года назад

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

  • @rinilkr3684
    @rinilkr3684 4 года назад

    Nice class

  • @prodiptaghosh6809
    @prodiptaghosh6809 4 года назад +2

    Sir next video is private why ??

  • @adarshsingh1823
    @adarshsingh1823 4 года назад

    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 4 года назад

      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

  • @shankarpange695
    @shankarpange695 3 месяца назад

    This is call by value

  • @reverbism
    @reverbism 2 года назад

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

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

  • @dhairyashah5886
    @dhairyashah5886 3 года назад

    what if we want to instert at position 1?

    • @followthewhiterabbit7559
      @followthewhiterabbit7559 3 года назад +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.

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

  • @_txt_7398
    @_txt_7398 Год назад +1

    My exam is today :)

  • @ShivarajNallagorla
    @ShivarajNallagorla 10 месяцев назад

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

  • @mehmedkukavica8076
    @mehmedkukavica8076 3 года назад +1

    I only miss comments in code to say perfect!

  • @anirudhbukka5413
    @anirudhbukka5413 3 года назад

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

  • @IShowwSpeedsui
    @IShowwSpeedsui 4 года назад +2

    Better than Jenny's

  • @ayoubferdjani8097
    @ayoubferdjani8097 8 месяцев назад

    I liked

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

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

    • @educationalonly5941
      @educationalonly5941 4 года назад

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

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

  • @alaaelnagar2410
    @alaaelnagar2410 2 месяца назад

    double free detected in tcache 2

  • @Mrmokuplays
    @Mrmokuplays Год назад

    Thanku sir acha smjhaya.

  • @muskan-l8b4m
    @muskan-l8b4m 26 дней назад

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