Skip to content

Commit 728b0cb

Browse files
Merge pull request #10 from AbhiiVops/main
Added code for Linked List Insertion and Deletion In C
2 parents 690a40a + 7232b80 commit 728b0cb

10 files changed

+744
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
4+
struct Node
5+
{
6+
int data;
7+
struct Node *next;
8+
9+
};
10+
11+
void linkedListTraversal(struct Node* ptr) { // ham ek head node naam ka pointer lenge jo head ko point krega
12+
// (head will be passed in argument from the main function at
13+
// the time of the function call)
14+
15+
while(ptr != NULL)
16+
{
17+
printf("Element : %d\n",ptr->data); // ye intially pointer ke data ko print krega
18+
ptr = ptr->next; // aur ye pointer ke data ko update krega next data m
19+
}
20+
21+
}
22+
int main() {
23+
24+
struct Node * head;
25+
struct Node * second;
26+
struct Node * third;
27+
struct Node * fourth;
28+
29+
// Allocate memory for nodes in the linked list in Heap
30+
head = (struct Node*)malloc(sizeof(struct Node));
31+
second = (struct Node*)malloc(sizeof(struct Node));
32+
third = (struct Node*)malloc(sizeof(struct Node));
33+
fourth = (struct Node*)malloc(sizeof(struct Node));
34+
35+
// Link first and second nodes
36+
head->data = 7;
37+
head->next = second;
38+
39+
// Link second and third node
40+
second->data =11;
41+
second->next = third;
42+
43+
// Link third and fourth node
44+
third->data = 66;
45+
third->next = fourth;
46+
47+
// Terminate the list at the fourth node
48+
fourth-> data= 82;
49+
fourth->next = NULL;
50+
51+
linkedListTraversal(head);
52+
53+
return 0;
54+
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
4+
struct Node
5+
{
6+
int data;
7+
struct Node *next;
8+
9+
};
10+
11+
void linkedListTraversal(struct Node* ptr) { // pointer for traversal in Linked list
12+
13+
while(ptr != NULL) {
14+
printf("Element : %d\n",ptr->data);
15+
ptr = ptr->next;
16+
17+
}
18+
19+
}
20+
21+
struct Node *insertAtFirst(struct Node *head,int data) {
22+
struct Node * ptr = (struct Node*)malloc(sizeof(struct Node));
23+
ptr-> next = head;
24+
ptr -> data = data;
25+
head =ptr;
26+
return head;
27+
}
28+
29+
30+
int main() {
31+
32+
struct Node * head;
33+
struct Node * second;
34+
struct Node * third;
35+
struct Node * fourth;
36+
37+
// Allocate memeory for nodes in the linked list in Heap
38+
head = (struct Node*)malloc(sizeof(struct Node));
39+
second = (struct Node*)malloc(sizeof(struct Node));
40+
third = (struct Node*)malloc(sizeof(struct Node));
41+
fourth = (struct Node*)malloc(sizeof(struct Node));
42+
43+
// Link first and third nodes
44+
head->data = 7;
45+
head->next = second;
46+
47+
// Link second and third node
48+
second->data =11;
49+
second->next = third;
50+
51+
// Link third and fourth node
52+
third->data = 66;
53+
third->next = fourth;
54+
55+
// Terminate the list at the fourth node
56+
fourth-> data= 82;
57+
fourth->next = NULL;
58+
59+
linkedListTraversal(head);
60+
head = insertAtFirst(head,56);
61+
printf("\n")
62+
; linkedListTraversal(head);
63+
64+
return 0;
65+
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
4+
5+
struct Node {
6+
7+
int data;
8+
struct Node * next;
9+
};
10+
11+
void LinkedListTraversal(struct Node* ptr) {
12+
13+
while(ptr!=NULL) {
14+
15+
printf("Element: %d\n",ptr->data);
16+
ptr = ptr->next;
17+
}
18+
19+
}
20+
21+
struct Node * DeleteAfterNode(struct Node * head,int value) {
22+
23+
24+
struct Node * ptr = (struct Node *)malloc (sizeof(struct Node));
25+
ptr = head;
26+
27+
struct Node * q= (struct Node *)malloc (sizeof(struct Node));
28+
q = ptr->next;
29+
30+
while(q->data != value && q->next !=NULL) {
31+
ptr = ptr-> next;
32+
q = ptr ->next;
33+
34+
}
35+
36+
if(q->data == value) {
37+
ptr->next = q->next;
38+
free(q);
39+
}
40+
41+
42+
43+
44+
return head;
45+
46+
}
47+
48+
int main(){
49+
50+
struct Node * head;
51+
struct Node * second;
52+
struct Node * third;
53+
struct Node * fourth;
54+
55+
56+
// Allocate memory dynamically to these pointers in the heap
57+
head = (struct Node *)malloc(sizeof(struct Node));
58+
second = (struct Node *)malloc(sizeof(struct Node));
59+
third = (struct Node *)malloc(sizeof(struct Node));
60+
fourth = (struct Node *)malloc(sizeof(struct Node));
61+
62+
63+
// Link first and second nodes
64+
head->data = 7;
65+
head->next = second;
66+
67+
// Link second and third node
68+
second->data =11;
69+
second->next = third;
70+
71+
// Link third and fourth node
72+
third->data = 66;
73+
third->next = fourth;
74+
75+
// Terminate the list at the fourth node
76+
fourth-> data= 82;
77+
fourth->next = NULL;
78+
79+
printf("Linked List Before deletion\n");
80+
LinkedListTraversal(head);
81+
head = DeleteAfterNode(head,99);
82+
printf("Linked List After deletion\n");
83+
LinkedListTraversal(head);
84+
85+
86+
87+
return 0;
88+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
4+
5+
struct Node {
6+
7+
int data;
8+
struct Node * next;
9+
};
10+
11+
void LinkedListTraversal(struct Node* ptr) {
12+
13+
while(ptr!=NULL) {
14+
15+
printf("Element: %d\n",ptr->data);
16+
ptr = ptr->next;
17+
}
18+
19+
}
20+
21+
struct Node * DeleteFirst(struct Node * head) {
22+
23+
24+
struct Node * ptr = (struct Node *)malloc (sizeof(struct Node));
25+
ptr = head;
26+
head = head->next;
27+
free(ptr);
28+
29+
return head;
30+
31+
}
32+
33+
int main(){
34+
35+
struct Node * head;
36+
struct Node * second;
37+
struct Node * third;
38+
struct Node * fourth;
39+
40+
41+
// Allocate memory dynamically to these pointers in the heap
42+
head = (struct Node *)malloc(sizeof(struct Node));
43+
second = (struct Node *)malloc(sizeof(struct Node));
44+
third = (struct Node *)malloc(sizeof(struct Node));
45+
fourth = (struct Node *)malloc(sizeof(struct Node));
46+
47+
48+
// Link first and second nodes
49+
head->data = 7;
50+
head->next = second;
51+
52+
// Link second and third node
53+
second->data =11;
54+
second->next = third;
55+
56+
// Link third and fourth node
57+
third->data = 66;
58+
third->next = fourth;
59+
60+
// Terminate the list at the fourth node
61+
fourth-> data= 82;
62+
fourth->next = NULL;
63+
64+
printf("Linked List Before deletion\n");
65+
LinkedListTraversal(head);
66+
head = DeleteFirst(head);
67+
printf("Linked List After deletion\n");
68+
LinkedListTraversal(head);
69+
70+
71+
72+
return 0;
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
4+
5+
struct Node {
6+
7+
int data;
8+
struct Node * next;
9+
};
10+
11+
void LinkedListTraversal(struct Node* ptr) {
12+
13+
while(ptr!=NULL) {
14+
15+
printf("Element: %d\n",ptr->data);
16+
ptr = ptr->next;
17+
}
18+
19+
}
20+
21+
struct Node * DeleteLastNode(struct Node * head) {
22+
23+
24+
struct Node * ptr = (struct Node *)malloc(sizeof(struct Node));
25+
ptr = head;
26+
27+
struct Node * q= (struct Node *)malloc(sizeof(struct Node));
28+
29+
q = ptr->next;
30+
31+
32+
33+
34+
35+
while(q->next != NULL) {
36+
ptr = ptr->next;
37+
q = ptr->next;
38+
}
39+
40+
ptr->next = NULL;
41+
free(q);
42+
43+
return head;
44+
45+
}
46+
47+
int main(){
48+
49+
struct Node * head;
50+
struct Node * second;
51+
struct Node * third;
52+
struct Node * fourth;
53+
54+
55+
// Allocate memory dynamically to these pointers in the heap
56+
head = (struct Node *)malloc(sizeof(struct Node));
57+
second = (struct Node *)malloc(sizeof(struct Node));
58+
third = (struct Node *)malloc(sizeof(struct Node));
59+
fourth = (struct Node *)malloc(sizeof(struct Node));
60+
61+
62+
// Link first and second nodes
63+
head->data = 7;
64+
head->next = second;
65+
66+
// Link second and third node
67+
second->data =11;
68+
second->next = third;
69+
70+
// Link third and fourth node
71+
third->data = 66;
72+
third->next = fourth;
73+
74+
// Terminate the list at the fourth node
75+
fourth-> data= 82;
76+
fourth->next = NULL;
77+
78+
printf("Linked List Before deletion\n");
79+
LinkedListTraversal(head);
80+
head = DeleteLastNode(head);
81+
printf("Linked List After deletion\n");
82+
LinkedListTraversal(head);
83+
84+
85+
86+
return 0;
87+
}

0 commit comments

Comments
 (0)