forked from RaghavSekhri/UCA-Ds_Algos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linked_List_AdditionAfterNode.c
85 lines (81 loc) · 1.56 KB
/
Linked_List_AdditionAfterNode.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
void append(int, struct node*);
void addition_at_beginning(struct node*);
int main()
{
int n;
struct node *head = NULL;
printf("How many elements you want to enter in a linked list : ");
scanf("%d",&n);
append(n,head);
return 0;
}
void append(int n,struct node* head)
{
struct node *temp;
for(int i=0;i<n;i++)
{
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter %d node data : ",i+1);
scanf("%d",&(temp->data));
temp->next = NULL;
if(head == NULL)
head = temp;
else
{
struct node *p;
p = head;
while(p->next != NULL)
p=p->next;
p->next = temp;
}
}
temp = head;
printf("Linked List before addition of node at beginning :- \n");
while(temp != NULL)
{
printf("The data is : %d and next address is : %p>\n",temp->data,temp->next);
temp = temp->next;
}
struct node *temp1,*p;
int i=1;
int loc;
printf("Enter location at which you want to enter a node : ");
scanf("%d",&loc);
int flag=0;
if(loc>n)
{
printf("Invalid location \n");
flag=1;
}
else
{
p = head;
while(i<loc)
{
p=p->next;
i++;
}
temp1=(struct node*)malloc(sizeof(struct node));
printf("Enter node data : ");
scanf("%d",&temp1->data);
temp1->next=NULL;
temp1->next = p->next;
p->next = temp1;
}
if(flag==0)
{
temp = head;
printf("Linked list after addition of node at particular loction :- \n");
while(temp != NULL)
{
printf("The data is : %d and next address is : %p>\n",temp->data,temp->next);
temp = temp->next;
}
}
}