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
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).
I've tried to figure this out all day. This video is straight to the point and goes thru line by line
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.
Kitna aasani se samjha diya sab kuch NESO ACADEMY ke aage koi nahi hai .
Sir you explain each and everything in such a manner we go to automatically deep of the subject
Amazing
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.
Jindagi me pahli bar esa teacher dekha Jo enta difficult chapter ko easy bana Diya thank you so much sir ❤❤❤❤
An in-depth explanation of the linked list concept. Great Work!! I hope you are going to make videos on other data structures too.
Hey neso academy! Are you guys fine? You haven't uploaded videos. Hope u guys are doing well!
Yes, we're fine. Thank you for asking. We will upload the next lecture today.
Amazing lectures! Thanks a lot Neso Academy and the instructors for making such good content and that too for free! :)
Sir plzz upload stack queue and other parts of ds videos....ur way of explianing and visualization is speechless
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
this is the best channel on youtube , please keep uploading the other data structures after linked list also
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.
You make difficult concepts sound so easy.
Im d frst student for the lecture.!! Yayyy way to go neso... very well done
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.
Thank you soooo much for sharing this,making learning programming in C much easier(also teaches me to understand indean accent lol
From Where ur actually ?
*indian
You guys are giving way too good content.thank you so much these lectures are helping me a lot.🙇♀🙇♀
No 1 playlist...neso academy ❤
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.
Isn't that sooo.....🤩😇
For your teaching...👏👏
These videos make excitement to learn DSA .🎉👍
finally someone who explains algorithm
You and your teaching skill is awesome sir
superb playlist... best videos on linkedlist
you are the best bro thank you for this course
Very easy to understand
Thanks sir
U r best 😁😁😁😁😁😁
Thanks a ton team for making these videos :). In fact thanks is a small word for the kind of content you guys produce.
Thank you so much , for explaining very clearly. By using pictorial representation ..
This video is an epiphany. Thank you so much.
Awesome Teaching Sir!! Superb 😍
sir please course complete pdha do sir bhut acha smjhya h apne
Thanku I was struggling doing the same in python but this made it lot easier though in C.
I love Neso Academy. 💝
Sir please provide the video lectures of design and analysis of algorithm for preparation of gate
Brilliant! Thanks so much!
love you bro
thank u it's easy to understand
Your presentation and explanation good but what about remaining topics in neso app
Big fan of ur voice sir🥰😁
Thank You 4/04/2022 2:47 am
thank you so fucking much, after struggling with this for a few days, now i can easily understand what is happening
Thanks bro 😊
why can we use head->link->link = temp .....?
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
Thanks Brother
😊
I did not got the cout
When will you upload entire data structures nesoacademy plz upload soon????
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.
if you use "head" you won't be able to access the first node as it would then point to a different node.
At very end of addtoend function, don't we need to mention head=ptr?
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;
}
tq bro
thanks
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?
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.....🙏
can we use head->link->link = temp , no need to traverse the list....?
amazing video sir!
you are amazing
Thank you so much.
Damn good explanation ....love it ❤️❤️
Your are the best
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
Can't we do using two pointers only as we did in previous lectures
Can you please tell that, are we supposed to call free( ) function for *ptr and *temp...within add_at_end( ) function?
Thank You 😊
Takeaway for me!
You cannot learn data structures without making and understanding figures.
Thanxx sr😊
Sir Are you uploading these code somewhere? I want the code
Helpful❤
Please show the output of codes
Sir please upload the video of graphs in dfs and bfs concept in code
What if the list is empty when we try to insert a node? We have to check that, right?
This code doesnt work, how should head be defined here?
this program won't execute correctly because there is no printf function. am i correct?
It executes bro
Give me the notes for stacks and queue with program and algorithms
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);};
Sir this program showing error, will please solve the problem and send program again.
Sir am compiler not able to call function for add node
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;
yeah i was wondering the same thing
This node is visible only inside the function ....not in main then what's the use
In my case, why does my IDE say Application error when I run this code?
hey this program is giving ivalid conversion from void* to *node
i am not able to sort this out
can you help
Typecast (struct Node*) malloc(sizeof(struct Node))
Can anyone write the same code using switch case statement ,add at begin,end and specific position.i have tried but showing error.
Sir please complete the full code
5:21 code
How to print a name using linked list
Sir where can I get this code ⁉️
but I can't even understand the difference between of them
while( ptr! = NULL)
Vs
while(ptr -> link! = NULL)
Please anyone can clarify?
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 😅
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)
4:50 I didn't get why not ptr !=null???
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
@@akashpraveen9814 but it have NULL value why it wouldn't assign NULL instead of 0 can u clear my doubt
@@AshokKumar-ix4nf sorry, i didn't get you
@@AshokKumar-ix4nf it's a same thing Null Or zero
What is wp =hd ??
that's grate
wow!
Could you plzz provide cmplt code..
👐
I am gettin a zero at the start of data at node? why
5 stars
but when i am printing it ,it is not showing anything.
Yeah even same problem to me i think its a source code if you know how to do keep comment ..
@@rithikvardhanreddy349 i think we have to try ptr->link != NULL instead of ptr != NULL then it should work.............
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;
}
@@roopaligarg792 yeah i think your right once i will try it
@@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.
This code is not working 😔
Wow
run hi nhi kar raha
waste of time
all progrrams are incomplete
The best 🤍🤍🤍
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;
}
You are a life saver! Thanks