forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Linked_List.cpp
208 lines (171 loc) · 4.19 KB
/
Linked_List.cpp
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <iostream>
using namespace std;
struct node {
int data;
node *next;
node(int value): data(value), next(NULL) {}
} *head = NULL;
bool Is_List_Empty() {
return (head == NULL);
}
void Insert_At_Beginning(int value) {
node *temp = new node(value);
temp -> next = head;
head = temp;
}
void Insert_At_End(int value) {
node *temp = new node(value);
if(Is_List_Empty()) {
head = temp;
return;
}
node *current = head;
while(current -> next != NULL) {
current = current -> next;
}
current -> next = temp;
}
void Insert_After_Value(int desired, int value) {
node *current = head;
while(current != NULL && current -> data != desired)
current = current -> next;
if(current == NULL) {
cout << "Element " << desired << " is not in list" << endl;
} else {
node *temp = new node(value);
temp -> next = current -> next;
current -> next = temp;
}
}
void Delete_At_Beginning() {
if(Is_List_Empty())
cout << "List is empty" << endl;
else {
node *temp = head;
head = head -> next;
temp -> next = NULL;
delete temp;
}
}
void Delete_At_End() {
if(Is_List_Empty()) {
cout << "List is empty" << endl;
return;
}
if(head -> next == NULL) {
delete head;
head = NULL;
return;
}
node *temp = head;
while(temp -> next -> next != NULL) {
temp = temp -> next;
}
delete temp -> next;
temp -> next = NULL;
}
void Delete_With_Value(int desired) {
if(Is_List_Empty()) {
cout << "List is empty" << endl;
return;
}
node *temp = head, *prev = NULL;
if(head -> data == desired) {
head = head -> next;
temp -> next = NULL;
delete temp;
return;
}
while(temp != NULL && temp -> data != desired) {
prev = temp;
temp = temp -> next;
}
if(temp == NULL) {
cout << "Element " << desired << " not in list" << endl;
} else {
prev -> next = temp -> next;
temp -> next = NULL;
delete temp;
}
}
void Search(int desired) {
node *temp = head;
while(temp != NULL && temp -> data != desired) {
temp = temp -> next;
}
if(temp == NULL) {
cout << "Element " << desired << " not found" << endl;
} else {
cout << "Element " << desired << " is present in list" << endl;
}
}
void Print_Linked_List() {
if(Is_List_Empty()) {
cout << "List is Empty" << endl;
return;
}
node *current = head;
while(current != NULL) {
cout << current -> data;
current = current -> next;
if (current != NULL)
cout << " -> ";
}
cout << endl;
}
void Length_Iterative() {
int length = 0;
node *temp = head;
while (temp) {
length += 1;
temp = temp -> next;
}
cout << "Length is " << length << " (Iterative)" << endl;
}
int Recursive_Count(node* current) {
if(current == NULL)
return 0;
return (1 + Recursive_Count(current -> next));
}
void Length_Recursive() {
int length = Recursive_Count(head);
cout << "Length is " << length << " (Recursive)" << endl;
}
int main() {
for(int i = 0; i < 5; i++)
Insert_At_Beginning(i);
Print_Linked_List();
for(int i = 5; i < 10; i++)
Insert_At_End(i);
Print_Linked_List();
Insert_After_Value(5, 9);
Insert_After_Value(10, 9);
Print_Linked_List();
for(int i = 0; i < 3; i++)
Delete_At_End();
Print_Linked_List();
for(int i = 0; i < 3; i++)
Delete_At_Beginning();
Print_Linked_List();
Delete_With_Value(1);
Delete_With_Value(5);
Print_Linked_List();
Search(6);
Search(8);
Length_Iterative();
Length_Recursive();
return 0;
}
/* Output
4 -> 3 -> 2 -> 1 -> 0
4 -> 3 -> 2 -> 1 -> 0 -> 5 -> 6 -> 7 -> 8 -> 9
Element 10 is not in list
4 -> 3 -> 2 -> 1 -> 0 -> 5 -> 9 -> 6 -> 7 -> 8 -> 9
4 -> 3 -> 2 -> 1 -> 0 -> 5 -> 9 -> 6
1 -> 0 -> 5 -> 9 -> 6
0 -> 9 -> 6
Element 6 is present in list
Element 8 not found
Length is 3 (Iterative)
Length is 3 (Recursive)
*/