-
Notifications
You must be signed in to change notification settings - Fork 132
/
Appending_Node.c
69 lines (55 loc) · 1.75 KB
/
Appending_Node.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<stdio.h>
#include<stdlib.h>
void append();
struct node
{
int data;
struct node *link;
};
struct node *root;
int main()
{
root=(struct node*)malloc(sizeof(struct node));
printf("ROOT 1 DATA:");
scanf("%d", &root->data);
root->link=NULL;
struct node*current;
current=(struct node*)malloc(sizeof(struct node));
printf("ROOT 2 DATA:");
scanf("%d", ¤t->data);
current->link=NULL;
root->link=current; // this will store the address of the second node into the link part of the first node
current=malloc(sizeof(struct node));
printf("ROOT 3 DATA:");
scanf("%d", ¤t->data);
current->link=NULL;
root->link->link=current; // this will store the base address of the third node into the link part of the second node
printf("APPENDING NODE:\n");
append(); // now the user will be prompted for appending the element to the last of the linked list
}
void append()
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("ENTER NODE DATA:");
scanf("%d", &temp->data);
if(root==NULL)
{
root=temp; // we are checking if the linked list is empty then this will be the first node to be inserted in the linked list
}
else
{
struct node *ptr; // used for traversing through nodes
ptr=root;
while(ptr->link!=NULL)
{
ptr=ptr->link;
}
ptr->link=temp; // this will connect the last node with the temp node which we created before
}
printf("PRINTING DATA IN ALL NODES\n");
printf("NODE 1: %d\n", root->data);
printf("NODE 2: %d\n", root->link->data);
printf("NODE 3: %d\n", root->link->link->data);
printf("NODE 4: %d\n", root->link->link->link->data);
}