C# Corner
Tech
News
Videos
Forums
Trainings
Books
Live
More
Interviews
Events
Jobs
Learn
Career
Members
Blogs
Challenges
Certifications
Bounties
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
Addition in Linked List
WhatsApp
Kaushik S
Nov 16
2015
851
0
0
#include<stdio.h>
#include<conio.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()
{
int
total=0;
struct
node* temp=head;
while
(temp!=NULL)
{
total=total+temp->data;
temp=temp->next;
}
printf(
"%d"
,total);
}
void
main()
{
insertAtEnd(1);
insertAtEnd(2);
insertAtEnd(3);
insertAtEnd(4);
insertAtEnd(5);
print();
getch();
}
C
Datastructures
Up Next
Addition in Linked List