Single Linked List (Inserting a Node at the End)

Поділитися
Вставка
  • Опубліковано 5 лис 2024
  • Data Structures: Inserting a Node at the End of a Singly Linked List
    Topics discussed:
    1) C program for inserting a node at the end of a 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 #InsertingANode

КОМЕНТАРІ • 127

  • @vijaysinghchauhan7079
    @vijaysinghchauhan7079 4 роки тому +77

    Your way of teaching and presenting makes the subject easy to grasp ✊ and the best 🌟 thing is that you do all this under 6-8 minutes(speaking generally).

  • @-saltless
    @-saltless 3 роки тому +31

    I've tried to figure this out all day. This video is straight to the point and goes thru line by line

  • @isteak.0023
    @isteak.0023 Рік тому +8

    for those who are confused about the part " while (ptr->link != NULL) " and (ptr!=NULL):
    it would't be (ptr!=NULL) cause during node 3(3000) ,it satisfies the condition (ptr!=NULL ) ,so ptr becomes ptr->link =NULL .
    So in next part to assign temp in ptr->link first we need ptr .but as it has become NULL (mentioned earlier) simply ptr->link doen't exist anymore.
    It was my understanding . pardon my english.

  • @aspiredifferent8085
    @aspiredifferent8085 2 роки тому +2

    Kitna aasani se samjha diya sab kuch NESO ACADEMY ke aage koi nahi hai .

  • @ajmalkhaniit
    @ajmalkhaniit 4 роки тому +9

    Sir you explain each and everything in such a manner we go to automatically deep of the subject
    Amazing

  • @AishLovesSpaghetti
    @AishLovesSpaghetti 3 роки тому +18

    Great Explanation. The function add_at_end can also be created without creating pointer ptr. We can just use head variable in the loop. Some might say that it's not possible because this will change the value in the head variable; however, this won't happen because address in head variable can only be changed inside main function where it was created. Only the copy of the value stored in head (in add_at_end function) is enough to traverse the list. This can be used to reduce the space complexity.

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

    Jindagi me pahli bar esa teacher dekha Jo enta difficult chapter ko easy bana Diya thank you so much sir ❤❤❤❤

  • @abdulbaseermahmood1591
    @abdulbaseermahmood1591 4 роки тому +13

    An in-depth explanation of the linked list concept. Great Work!! I hope you are going to make videos on other data structures too.

  • @dudefx8971
    @dudefx8971 4 роки тому +48

    Hey neso academy! Are you guys fine? You haven't uploaded videos. Hope u guys are doing well!

    • @nesoacademy
      @nesoacademy  4 роки тому +19

      Yes, we're fine. Thank you for asking. We will upload the next lecture today.

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

    Amazing lectures! Thanks a lot Neso Academy and the instructors for making such good content and that too for free! :)

  • @SatyaPrakash-ol9gv
    @SatyaPrakash-ol9gv 4 роки тому +8

    Sir plzz upload stack queue and other parts of ds videos....ur way of explianing and visualization is speechless

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

    great explaination since morning 8 am im not understanding this inserting and linked list now its 10 pm finally all doubts are clear and now im doing on my vs code

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

    this is the best channel on youtube , please keep uploading the other data structures after linked list also

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

    First of all,
    Thanks for such an easy explanation. Can you please upload video lectures on Microprocessors(8085,8086,8251,8259,etc) since we are facing problem in that subject too.

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

    You make difficult concepts sound so easy.

  • @iqbalahmed9128
    @iqbalahmed9128 4 роки тому +6

    Im d frst student for the lecture.!! Yayyy way to go neso... very well done

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

    So great explanaition that raisded a question for me: shouldn't we use a double pointer in "add_at_end" function?
    The head seems to be lockal in the function.

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

    Thank you soooo much for sharing this,making learning programming in C much easier(also teaches me to understand indean accent lol

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

    You guys are giving way too good content.thank you so much these lectures are helping me a lot.🙇‍♀🙇‍♀

  • @lipsa-rameshsamal8097
    @lipsa-rameshsamal8097 2 місяці тому

    No 1 playlist...neso academy ❤

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

    Create a variable which indicate the last node of list named "tail" tail contain adress of the last node of linked list using this approach we don't need to travsel whole list to add the node in the last.

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

    Isn't that sooo.....🤩😇
    For your teaching...👏👏

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

    These videos make excitement to learn DSA .🎉👍

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

    finally someone who explains algorithm

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

    You and your teaching skill is awesome sir

  • @photolab6712
    @photolab6712 10 місяців тому

    superb playlist... best videos on linkedlist

  • @alexander-ud9bs
    @alexander-ud9bs Рік тому

    you are the best bro thank you for this course

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

    Very easy to understand
    Thanks sir
    U r best 😁😁😁😁😁😁

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

    Thanks a ton team for making these videos :). In fact thanks is a small word for the kind of content you guys produce.

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

    Thank you so much , for explaining very clearly. By using pictorial representation ..

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

    This video is an epiphany. Thank you so much.

  • @AbhishekSingh-cu1fe
    @AbhishekSingh-cu1fe 3 роки тому

    Awesome Teaching Sir!! Superb 😍

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

    sir please course complete pdha do sir bhut acha smjhya h apne

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

    Thanku I was struggling doing the same in python but this made it lot easier though in C.

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

    I love Neso Academy. 💝

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

    Sir please provide the video lectures of design and analysis of algorithm for preparation of gate

  • @MrCEO-jw1vm
    @MrCEO-jw1vm 5 місяців тому

    Brilliant! Thanks so much!

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

    love you bro

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

    thank u it's easy to understand

  • @Krishna-ek9jk
    @Krishna-ek9jk 3 роки тому

    Your presentation and explanation good but what about remaining topics in neso app

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

    Big fan of ur voice sir🥰😁

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

    Thank You 4/04/2022 2:47 am

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

    thank you so fucking much, after struggling with this for a few days, now i can easily understand what is happening

  • @GousePeera-r4r
    @GousePeera-r4r 2 місяці тому

    Thanks bro 😊

  • @GAaron-uc9nm
    @GAaron-uc9nm Місяць тому +1

    why can we use head->link->link = temp .....?

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

    First, thanks for your knowledge sir!
    For those who got error or something else, you may try this code:
    #include
    using namespace std;
    struct Node{
    int data;
    struct Node *next;
    };
    void add_at_end(Node *head, int data)
    {
    Node *ptr, *temp;
    ptr = head;
    temp = new Node;

    temp->data = data;
    temp->next = NULL;

    while(ptr->next != NULL)
    {
    ptr = ptr->next;
    }
    ptr->next = temp;
    }
    int main()
    {
    // Create a first node with data '45'.
    Node *head = new Node;
    head->data = 45;
    head->next = NULL;

    add_at_end(head, 98); // add second node.
    add_at_end(head, 3); // add third node.

    add_at_end(head, 67); // add forth node.

    Node *ptr = head;

    while(ptr != NULL)
    {
    cout

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

    When will you upload entire data structures nesoacademy plz upload soon????

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

    Is there a reason why your doing "ptr = head" rather than just using the "head" (that you've actually passed to the function) in the "while" loop?
    I've done it both ways and both work.

    • @dimi5929
      @dimi5929 2 роки тому +2

      if you use "head" you won't be able to access the first node as it would then point to a different node.

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

    At very end of addtoend function, don't we need to mention head=ptr?

  • @shivam-qx2hd
    @shivam-qx2hd 2 роки тому +6

    Here is the Executable Code:
    // how to add node at end
    #include
    #include
    struct node
    {
    int data;
    struct node *link;
    };
    void add_at_end(struct node *head, int data)
    {
    struct node *ptr, *temp;
    ptr = head;
    temp = (struct node *)malloc(sizeof(struct node));
    temp->data = 67;
    temp->link = NULL;
    while (ptr != NULL)
    {
    printf("%d
    ", ptr->data);
    ptr = ptr->link;
    }
    ptr = temp->link;
    printf("%d",temp->data);
    }
    int main()
    {
    struct node *head = malloc(sizeof(struct node));
    head->data = 45;
    head->link = NULL;
    struct node *current = malloc(sizeof(struct node));
    current->data = 98;
    current->link = NULL;
    head->link = current;
    current = (struct node *)malloc(sizeof(struct node));
    current->data = 3;
    current->link = NULL;
    head->link->link = current;
    add_at_end(head, 67);
    return 0;
    }

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

      tq bro

    • @MdArman-f7n6j
      @MdArman-f7n6j Рік тому

      thanks

    • @yogeshwaranraguraman9078
      @yogeshwaranraguraman9078 2 місяці тому

      Bro,
      ptr = temp->link; ?? How?
      temp->link contains NULL right, You are assigning NULL instead of a New node address
      we have to assign ptr = temp; because temp contains the address of the new node and ptr contains the last link address as a NULL. so you have to assign ptr = temp; is correct. and temp->link contains NULL as already you have assigned.
      isn't it?

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

    we can also write the code as
    ptr = head;
    while(1)
    {
    if(ptr->link == NULL)
    {
    ptr->link=temp;
    break;
    }
    ptr=ptr->link;
    }
    sir plz complete the play list as soon as possible.....🙏

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

    can we use head->link->link = temp , no need to traverse the list....?

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

    amazing video sir!

  • @monkman670
    @monkman670 2 місяці тому

    you are amazing

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

    Thank you so much.

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

    Damn good explanation ....love it ❤️❤️

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

    Your are the best

  • @TravelVlog_WalkingTour
    @TravelVlog_WalkingTour 2 роки тому +2

    Im getting this error please help me with this
    error: 'head' undeclared (first use in the function).
    How to solve this i use the exact code as shown on the video but it gives me error it does not work

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

    Can't we do using two pointers only as we did in previous lectures

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

    Can you please tell that, are we supposed to call free( ) function for *ptr and *temp...within add_at_end( ) function?

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

    Thank You 😊

  • @tayyab.sheikh
    @tayyab.sheikh 4 місяці тому +1

    Takeaway for me!
    You cannot learn data structures without making and understanding figures.

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

    Thanxx sr😊

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

    Sir Are you uploading these code somewhere? I want the code

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

    Helpful❤

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

    Please show the output of codes

  • @alpha-vy9ej
    @alpha-vy9ej 4 роки тому

    Sir please upload the video of graphs in dfs and bfs concept in code

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

    What if the list is empty when we try to insert a node? We have to check that, right?

  • @sukshithshetty8349
    @sukshithshetty8349 2 роки тому +2

    This code doesnt work, how should head be defined here?

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

    this program won't execute correctly because there is no printf function. am i correct?

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

    Give me the notes for stacks and queue with program and algorithms

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

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

    Sir this program showing error, will please solve the problem and send program again.

  • @HCJ-Technology
    @HCJ-Technology 3 роки тому +1

    Sir am compiler not able to call function for add node

  • @Cat_Sterling
    @Cat_Sterling 3 роки тому +6

    What would be a good way to cover an edge case with an empty list here?
    What about this if statement before the while loop:
    if (ptr == NULL)
    ptr = temp;

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

      yeah i was wondering the same thing

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

    This node is visible only inside the function ....not in main then what's the use

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

    In my case, why does my IDE say Application error when I run this code?

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

    hey this program is giving ivalid conversion from void* to *node
    i am not able to sort this out
    can you help

  • @ayeshaayesha232
    @ayeshaayesha232 2 місяці тому

    Can anyone write the same code using switch case statement ,add at begin,end and specific position.i have tried but showing error.

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

    Sir please complete the full code

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

    5:21 code

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

    How to print a name using linked list

  • @22saithejasrireddy16
    @22saithejasrireddy16 3 роки тому

    Sir where can I get this code ⁉️

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

    but I can't even understand the difference between of them
    while( ptr! = NULL)
    Vs
    while(ptr -> link! = NULL)
    Please anyone can clarify?

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

      ptr stores the address of the node. Like, I think to access elements of the node (data and next address) we need to use ptr->data and ptr->link.
      Like, if we want to stop when the next address is NULL, we can't be using ptr==NULL right? We want to stop when ptr->link == NULL. Like, ptr == NULL means, node's address is NULL, we can't get that way....
      I hope you got it 😅

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

      You can think of it this way. we are using ptr -> link = NULL here because:
      we want to check if we have the address of the next node, we are over-checking (ptr becomes NULL) if we use ptr only.
      In contrast, for the count and print functions, we use ptr != NULL, because:
      we are checking the entire node. If we do ptr -> link, we are missing one node. Because the last node is always the format of data, pointer. And this pointer is always NULL)

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

    4:50 I didn't get why not ptr !=null???

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

      If we use ptr!= null, when ptr points to 3000, the condition executes and ptr->link part will get assigned to ptr( i.e) [ zero will be assigned to ptr].if this happens we can't able to find the last node where new node to be inserted..so instead we are check the node link part is zero or not

    • @AshokKumar-ix4nf
      @AshokKumar-ix4nf 3 роки тому

      @@akashpraveen9814 but it have NULL value why it wouldn't assign NULL instead of 0 can u clear my doubt

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

      @@AshokKumar-ix4nf sorry, i didn't get you

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

      @@AshokKumar-ix4nf it's a same thing Null Or zero

  • @GopikaRaja-c7k
    @GopikaRaja-c7k Рік тому

    What is wp =hd ??

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

    that's grate

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

    wow!

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

    Could you plzz provide cmplt code..

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

    👐

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

    I am gettin a zero at the start of data at node? why

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

    5 stars

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

    but when i am printing it ,it is not showing anything.

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

      Yeah even same problem to me i think its a source code if you know how to do keep comment ..

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

      @@rithikvardhanreddy349 i think we have to try ptr->link != NULL instead of ptr != NULL then it should work.............

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

      That might be because the list is initially empty. As soon as you add one node in the list this solution works, else this code needs to handle empty list scenario as well. @nesoacademy please correct me if i am wrong.
      Try adding this before while loop and it should work fine:
      if(head == NULL)
      {
      head = temp;
      return;
      }

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

      @@roopaligarg792 yeah i think your right once i will try it

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

      @@roopaligarg792 no it doesn't work because we r creating an another copy of head in the function, therefore we should use double pointers in the function parameter to reflect any changes to our main list.

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

    This code is not working 😔

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

    Wow

  • @SUBHANKARDEY-ji6xs
    @SUBHANKARDEY-ji6xs Рік тому

    run hi nhi kar raha

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

    waste of time
    all progrrams are incomplete

  • @CSstudent_1001
    @CSstudent_1001 10 місяців тому

    The best 🤍🤍🤍

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

    i got "Segmentation fault" !
    what to do??
    #include
    #include
    struct node
    {
    int data;
    struct node*link;
    };
    void print_end(struct node*head,int data){
    struct node*ptr,*temp;
    ptr=head;
    temp=malloc(sizeof(struct node));
    temp->data= data;
    temp->link=NULL;
    while(ptr!=NULL)
    {
    printf("%d",ptr->data);
    ptr=ptr->link;
    }
    ptr->link=temp;
    }
    int main() {
    //node-1//
    struct node*head=malloc(sizeof(struct node*));
    head->data=46;
    head->link=NULL;
    //node-2//
    struct node*current=malloc(sizeof(struct node*));
    current->data=47;
    current->link=NULL;
    head->link=current;
    //node-3//
    current=malloc(sizeof(struct node*));
    current->data=48;
    current->link=NULL;
    head->link->link=current;
    //node4//
    current=malloc(sizeof(struct node*));
    current->data=55;
    current->link=NULL;
    head->link->link->link=current;
    print_end(head,49);
    return 0;
    }

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

    You are a life saver! Thanks