forked from ratnesh-maurya/Educational-Tech-Exploration
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertion.cpp
More file actions
121 lines (112 loc) · 2.81 KB
/
insertion.cpp
File metadata and controls
121 lines (112 loc) · 2.81 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <bits/stdc++.h>
struct Node {
int data;
struct Node* next;
};
void display(struct Node* ptr){
while(ptr!=0){
printf("%d " ,ptr->data);
ptr= ptr->next;
}
printf("\n");
}
struct Node* insert_at_beginning(struct Node* start, int data1){
struct Node* ptr = (struct Node*)malloc(sizeof(struct Node));
ptr->data = data1;
ptr->next = start;
start = ptr;
return start;
}
struct Node* insert_in_between(struct Node* start, int data, int index){
struct Node* ptr = (struct Node*)malloc(sizeof(struct Node));
struct Node* p = start;
int i=0;
while(i!=index-1){
p = p->next;
i++;
}
ptr->data = data;
ptr->next = p->next;
p->next = ptr;
p = start;
return p;
}
struct Node* insert_at_the_end(struct Node* start, int data){
struct Node* ptr = (struct Node*)malloc(sizeof(struct Node)), *p;
ptr->data = data;
p = start;
while(p->next!=NULL){
p = p->next;
}
p->next = ptr;
ptr->next = NULL;
return start;
}
struct Node* delete_at_beginning(struct Node* start){
struct Node* p;
p = start;
start = start->next;
free(p);
return start;
}
struct Node* delete_in_between(struct Node* start, int index){
struct Node* p=start, *q=start->next;
int i=0;
while(i!=index-1){
p = p->next;
q = q->next;
i++;
}
p->next = q->next;
free(q);
return start;
}
struct Node* delete_at_the_end(struct Node* start){
struct Node* p = start,*q = start->next;
while(q->next != NULL){
q = q->next;
p = p->next;
}
p->next = NULL;
free(q);
return start;
}
struct Node * delete_at_given_key(struct Node* start, int key){
struct Node* ptr = start,*q=start->next;
while(q->data!=key && q->next!=NULL){
ptr=ptr->next;
q = q->next;
}
ptr->next = q->next;
free(q);
return start;
}
int main()
{
struct Node *start,*second,*third;
start = (struct Node*)malloc(sizeof(struct Node));
second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));
start->data=5;
start->next=second;
second->data=6;
second->next=third;
third->data=2;
third->next= NULL;
display(start);
start = insert_at_beginning(start,4);
display(start);
start = insert_in_between(start,22,2);
display(start);
start = insert_at_the_end(start,2);
display(start);
start = delete_at_beginning(start);
display(start);
start = delete_in_between(start,2);
display(start);
start = delete_at_the_end(start);
display(start);
start = delete_at_given_key(start,22);
display(start);
return 0;
}