Skip to content

Commit a9ea7b3

Browse files
Merge pull request #327 from bhavy007/main
Added Insertion of node at tail of LinkedList
2 parents 71362cf + ab5f085 commit a9ea7b3

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
#include <iostream>
3+
4+
using namespace std;
5+
6+
7+
8+
struct Node
9+
{
10+
int data;
11+
struct Node *next;
12+
};
13+
14+
struct Node *head;
15+
16+
void
17+
Insert (int data)
18+
{
19+
struct Node *temp = (struct Node *) malloc (sizeof (struct Node *));
20+
21+
22+
temp->data = data;
23+
temp->next = NULL;
24+
25+
if (head == NULL)
26+
{
27+
head = temp;
28+
return;
29+
}
30+
31+
struct Node *temp2 = head;
32+
while (temp2->next != NULL)
33+
{
34+
temp2 = temp2->next;
35+
}
36+
temp2->next = temp;
37+
}
38+
39+
void
40+
Print ()
41+
{
42+
struct Node *temp = head;
43+
while (temp != NULL)
44+
{
45+
printf (" %d", temp->data);
46+
temp = temp->next;
47+
}
48+
printf ("\n");
49+
50+
}
51+
52+
53+
int
54+
main ()
55+
{
56+
head = NULL;
57+
Insert (4);
58+
Insert (6);
59+
Insert (8);
60+
Insert (2);
61+
Print ();
62+
63+
return 0;
64+
}
65+

0 commit comments

Comments
 (0)