-
Notifications
You must be signed in to change notification settings - Fork 69
/
AddingElementsLinkedLIst.c
63 lines (47 loc) · 1.32 KB
/
AddingElementsLinkedLIst.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
//Q. adding the element to the head node of a given linked list
//eg : before deletion : 2 5 8 9 7
//eg : after deletion : 10 2 5 8 9 7
#include <stdlib.h>
#include <stdio.h>
struct Node {
int data;
struct Node * next;
};
struct Node * addingElements(struct Node * head, int ele){
struct Node * ptr = (struct Node *)malloc(sizeof(struct Node));
struct Node * p = head;
ptr->next = p;
ptr->data = ele;
head = ptr;
return(head);
}
struct Node * deletionElements(struct Node * head){
head = head->next;
return(head);
}
void llTraversal(struct Node * head){
while(head != NULL){
printf("%d\n", head->data);
head = head->next;
}
}
int main(){
struct Node * head = (struct Node *)malloc(sizeof(struct Node));
struct Node * second = (struct Node *)malloc(sizeof(struct Node));
struct Node * third = (struct Node *)malloc(sizeof(struct Node));
int ele;
head->data = 10;
head->next = second;
second->data = 20;
second->next = third;
third->data = 30;
third->next = NULL;
printf("Enter the element : ");
scanf("%d", &ele);
printf("Linked list before insertion : \n");
llTraversal(head);
printf("linked list after insertion is : \n");
head = addingElements(head, ele);
llTraversal(head);
return 0;
}