-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathinsert_ll
76 lines (69 loc) · 1.42 KB
/
insert_ll
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
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int data;
struct node* next;
}node;
//creating a linked list
node* create_ll(int data){
node* current;
current=(node*)malloc(sizeof(node));
current->data=data;
return current;
}
//adding a node at the end
void add_at_end(node **head, int data ){
node* new_node=create_ll(data);
new_node->next=NULL;
if(*head==NULL){
*head=new_node;
}
else{
node* temp= *head;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=new_node;
}
}
//adding a node at the beginning
void add_at_start(node **head, int data){
node* new_node=create_ll(data);
new_node->next=*head;
*head=new_node;
}
//adding node at any position
void add_at_position(node **head, int position, int data){
node* new_node=create_ll(data);
int i=1;
node* temp=*head;
if(position==1){
add_at_start(head, data);
}
else{
while(i<position-1){
temp=temp->next;
i++;
}
new_node->next=temp->next;
temp->next=new_node;
}
}
//printing a linked list
void print(node *head){
while(head!=NULL){
printf("%d\n", head->data);
head=head->next;
}
}
int main(){
node* head=NULL;
node* tail;
int n=0;
printf("Enter the number of data: ");
scanf("%d", &n);
for(int i=0;i<n;i++){
add_at_end(&head, i+1);
}
return 0;
}