Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Insert at the end of the Linked List
WhatsApp
Kaushik S
Nov 16
2015
865
0
0
#include <stdio.h>
#include<stdlib.h>
typedef
struct
node{
int
data;
struct
node* next;
}node;
struct
node* head;
void
InsertAtEnd(
int
x)
{
struct
node* temp=NULL;
temp=(node *)malloc(
sizeof
(
struct
node));
temp->data=x;
if
(head==NULL)
{
head=temp;
temp->next=NULL;
}
else
{
struct
node* temp2=NULL;
temp2=(node *)malloc(
sizeof
(
struct
node));
temp=head;
while
(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=temp2;
temp2->data=x;
temp2->next=NULL;
}
}
void
print()
{
struct
node* temp=head;
while
(temp!=NULL)
{
printf(
"%d"
,temp->data);
temp=temp->next;
}
}
void
main()
{
InsertAtEnd(1);
InsertAtEnd(2);
InsertAtEnd(3);
print();
getch();
}
C
Datastructures
Up Next
Insert at the end of the Linked List