Single Linked List (Inserting a Node at the End)

Sdílet
Vložit
  • čas přidán 7. 09. 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

Komentáře • 124

  • @vijaysinghchauhan7079
    @vijaysinghchauhan7079 Před 3 lety +74

    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 Před 3 lety +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 Před 10 měsíci +6

    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 Před 2 lety +2

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

  • @AishLovesSpaghetti
    @AishLovesSpaghetti Před 3 lety +17

    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.

  • @ajmalkhaniit
    @ajmalkhaniit Před 4 lety +9

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

  • @dudefx8971
    @dudefx8971 Před 4 lety +41

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

    • @nesoacademy
      @nesoacademy  Před 4 lety +17

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

  • @abdulbaseermahmood1591
    @abdulbaseermahmood1591 Před 4 lety +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.

  • @SatyaPrakash-ol9gv
    @SatyaPrakash-ol9gv Před 4 lety +8

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

  • @ahmadaklakh6736
    @ahmadaklakh6736 Před rokem

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

  • @sallavatsal
    @sallavatsal Před 2 lety +4

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

  • @iqbalahmed9128
    @iqbalahmed9128 Před 4 lety +6

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

  • @harroopsinghgala6405
    @harroopsinghgala6405 Před 4 lety +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.

  • @ecea044gauravgogoi7
    @ecea044gauravgogoi7 Před 4 lety +1

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

  • @shivam-qx2hd
    @shivam-qx2hd Před rokem +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 Před rokem

      tq bro

    • @user-bk2un6er8h
      @user-bk2un6er8h Před rokem

      thanks

    • @yogeshwaranraguraman9078
      @yogeshwaranraguraman9078 Před 26 dny

      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?

  • @dhananjay9923
    @dhananjay9923 Před rokem

    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

  • @aniketshivhare601
    @aniketshivhare601 Před 4 lety +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.

  • @yuechenxiao4228
    @yuechenxiao4228 Před 2 lety +2

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

  • @indanadeevena4597
    @indanadeevena4597 Před 9 měsíci +1

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

  • @reverbism
    @reverbism Před 2 lety +1

    Thank You 4/04/2022 2:47 am

  • @selfyqueen2605
    @selfyqueen2605 Před rokem

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

  • @DatasafeWorld
    @DatasafeWorld Před 11 měsíci

    You make difficult concepts sound so easy.

  • @mamo100
    @mamo100 Před rokem

    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.

  • @whatthefuckor5226
    @whatthefuckor5226 Před 3 lety

    omg thank you makes much more sense with the visual and detailed explanations!!!

  • @Cat_Sterling
    @Cat_Sterling Před 3 lety +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 Před 3 lety

      yeah i was wondering the same thing

  • @dipankar9700
    @dipankar9700 Před 3 lety +1

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

  • @Lordash234
    @Lordash234 Před 2 lety

    finally someone who explains algorithm

  • @allinonesomespecial4
    @allinonesomespecial4 Před 2 lety

    These videos make excitement to learn DSA .🎉👍

  • @shubhangishreya1492
    @shubhangishreya1492 Před 2 lety

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

  • @uttamkarmakarece3534
    @uttamkarmakarece3534 Před 3 lety

    You and your teaching skill is awesome sir

  • @mikeorioles
    @mikeorioles Před 2 lety

    This video is an epiphany. Thank you so much.

  • @TravelVlog_WalkingTour
    @TravelVlog_WalkingTour Před 2 lety +1

    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

  • @aeroabrar_31
    @aeroabrar_31 Před 3 lety +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.....🙏

  • @lipsa-rameshsamal8097
    @lipsa-rameshsamal8097 Před 28 dny

    No 1 playlist...neso academy ❤

  • @ayeshaayesha232
    @ayeshaayesha232 Před 17 dny

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

  • @venkyssss495
    @venkyssss495 Před rokem

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

  • @muhammadzakiahmad8069
    @muhammadzakiahmad8069 Před 2 lety

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

  • @deepalikatara6531
    @deepalikatara6531 Před 4 lety +2

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

  • @photolab6712
    @photolab6712 Před 8 měsíci

    superb playlist... best videos on linkedlist

  • @zichangprayingtothelord6087

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

  • @ihabmed1672
    @ihabmed1672 Před 2 lety +1

    Sadly i'm having some problemes
    When i printf the data of the last added node
    It gives me a random number
    And not what i have inserted

  • @alexander-ud9bs
    @alexander-ud9bs Před 10 měsíci

    you are the best bro thank you for this course

  • @rodwynnejones
    @rodwynnejones Před 3 lety +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 Před 2 lety +2

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

  • @punky6320
    @punky6320 Před 3 lety +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

  • @codingstuff52
    @codingstuff52 Před 3 lety +2

    love you bro

  • @dipeshsamrawat7957
    @dipeshsamrawat7957 Před 3 lety

    I love Neso Academy. 💝

  • @istutirajeev
    @istutirajeev Před 3 lety

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

  • @AbhishekSingh-cu1fe
    @AbhishekSingh-cu1fe Před 2 lety

    Awesome Teaching Sir!! Superb 😍

  • @kacperos2720
    @kacperos2720 Před 2 lety

    You are a life saver! Thanks

  • @sukshithshetty8349
    @sukshithshetty8349 Před 2 lety +2

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

  • @MrCEO-jw1vm
    @MrCEO-jw1vm Před 3 měsíci

    Brilliant! Thanks so much!

  • @afreenpoly
    @afreenpoly Před 2 lety +1

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

  • @GousePeera-r4r
    @GousePeera-r4r Před 12 dny

    Thanks bro 😊

  • @monkman670
    @monkman670 Před 11 dny

    you are amazing

  • @user-nr8vc7es7o
    @user-nr8vc7es7o Před 5 měsíci

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

  • @dominiquefortin5345
    @dominiquefortin5345 Před 2 lety

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

  • @rahulkhandelwal3632
    @rahulkhandelwal3632 Před 3 lety

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

  • @HCJ-Technology
    @HCJ-Technology Před 3 lety +1

    Sir am compiler not able to call function for add node

  • @sudheerjalli4707
    @sudheerjalli4707 Před 4 lety +1

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

  • @saiharshagurijala2066
    @saiharshagurijala2066 Před 3 lety +2

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

  • @tulikagupta3998
    @tulikagupta3998 Před 3 lety +1

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

  • @vishalmpj9485
    @vishalmpj9485 Před 2 lety

    thank u it's easy to understand

  • @eyzhie9096
    @eyzhie9096 Před 2 lety

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

  • @SimranjeetkaurWarwal
    @SimranjeetkaurWarwal Před rokem

    Big fan of ur voice sir🥰😁

  • @pankajgopal7574
    @pankajgopal7574 Před 3 lety +1

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

  • @tayyab.sheikh
    @tayyab.sheikh Před 2 měsíci +1

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

  • @chandrikaiyer9141
    @chandrikaiyer9141 Před 3 lety

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

  • @nakulrajkr
    @nakulrajkr Před rokem

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

  • @Krishna-ek9jk
    @Krishna-ek9jk Před 3 lety

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

  • @palashchandradas3277
    @palashchandradas3277 Před 2 lety

    Thank you so much.

  • @johnjohn7795
    @johnjohn7795 Před 2 lety

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

  • @shivanshbhardwaj5841
    @shivanshbhardwaj5841 Před 4 lety

    amazing video sir!

  • @sadiaafrin2623
    @sadiaafrin2623 Před 2 lety

    Your are the best

  • @deepakpandey2100
    @deepakpandey2100 Před 4 lety

    Thank You 😊

  • @Nickname1234_
    @Nickname1234_ Před 2 lety

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

  • @user-mx9fq5kq3p
    @user-mx9fq5kq3p Před 3 měsíci

    Please show the output of codes

  • @alpha-vy9ej
    @alpha-vy9ej Před 3 lety

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

  • @nikhilsachan8598
    @nikhilsachan8598 Před 4 lety

    Thanxx sr😊

  • @roshangogu2670
    @roshangogu2670 Před rokem

    Helpful❤

  • @tradertrader2520
    @tradertrader2520 Před 3 lety +1

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

    • @akashpraveen9814
      @akashpraveen9814 Před 3 lety

      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 Před 3 lety

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

    • @akashpraveen9814
      @akashpraveen9814 Před 3 lety

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

    • @vaibhavmourya65
      @vaibhavmourya65 Před 2 lety

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

  • @ajoydev8876
    @ajoydev8876 Před 2 lety

    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 Před 2 lety

      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 Před rokem

      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)

  • @user-zl9bn4vy8u
    @user-zl9bn4vy8u Před 10 měsíci

    What is wp =hd ??

  • @siddharthkumar5600
    @siddharthkumar5600 Před 5 měsíci

    that's grate

  • @anyagoswami989
    @anyagoswami989 Před rokem

    Could you plzz provide cmplt code..

  • @22saithejasrireddy16
    @22saithejasrireddy16 Před 3 lety

    Sir where can I get this code ⁉️

  • @kaushaljha1384
    @kaushaljha1384 Před 3 lety

    Sir please complete the full code

  • @sukshithshetty8349
    @sukshithshetty8349 Před 2 lety

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

  • @mischieviousgirl
    @mischieviousgirl Před 5 měsíci

    5:21 code

  • @istutirajeev
    @istutirajeev Před 3 lety

    wow!

  • @karinakumari3671
    @karinakumari3671 Před 2 lety

    How to print a name using linked list

  • @nehavashishth9354
    @nehavashishth9354 Před 4 lety

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

    • @rithikvardhanreddy349
      @rithikvardhanreddy349 Před 4 lety

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

    • @abhishekmalviya4667
      @abhishekmalviya4667 Před 4 lety

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

    • @roopaligarg792
      @roopaligarg792 Před 3 lety +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 Před 3 lety

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

    • @tejagowtham3856
      @tejagowtham3856 Před 3 lety

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

  • @harpreetvirk3308
    @harpreetvirk3308 Před 2 lety

    5 stars

  • @harmeetsingh7381
    @harmeetsingh7381 Před 2 lety

    👐

  • @Vikaskarbail
    @Vikaskarbail Před 2 lety

    Wow

  • @sridevichowdary9763
    @sridevichowdary9763 Před rokem

    This code is not working 😔

  • @SUBHANKARDEY-ji6xs
    @SUBHANKARDEY-ji6xs Před 11 měsíci

    run hi nhi kar raha

  • @vamsikrish7640
    @vamsikrish7640 Před rokem

    waste of time
    all progrrams are incomplete

  • @JaisuryaPilla
    @JaisuryaPilla Před rokem

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

  • @CSstudent_1001
    @CSstudent_1001 Před 8 měsíci

    The best 🤍🤍🤍

  • @karanshrivastava3086
    @karanshrivastava3086 Před rokem

    Any one can find the mistake in given program
    // Online C compiler to run C program online
    #include
    #include
    struct node
    {
    int data;
    struct node* next;
    };
    int main() {
    struct node *head=NULL;
    struct node *one=malloc(sizeof(struct node));
    struct node *two=malloc(sizeof(struct node));
    struct node *three=malloc(sizeof(struct node));
    //struct node *four=malloc(sizeof(struct node));
    one->data=50;
    one->next=two;
    two->data=50;
    two->next=three;
    three->data=50;
    three->next=NULL;
    head=one;
    struct node *start,*four;
    start=head;
    four=(struct node*)malloc(sizeof(struct node));
    four->data=20;
    four->next=NULL;
    while(start->next!=NULL)
    start=start->next;

    start->next=four;
    while(head->next!=NULL)
    {
    printf("%d
    ",head->data);
    head=head->next;
    }
    return 0;
    }