TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
linked_list.cpp
Go to the documentation of this file.
1
17
#include <iostream>
18
#include <memory>
19
#include <string>
20
25
namespace
data_structures
{
26
31
namespace
linked_list
{
32
40
bool
isDigit
(
const
std::string& s) {
41
// function statements here
42
for
(
char
i : s) {
43
if
(!isdigit(i)) {
44
return
false
;
45
}
46
}
47
return
true
;
48
}
49
53
class
link
{
54
private
:
55
int
pvalue
;
56
std::shared_ptr<link>
psucc
;
57
58
public
:
63
int
val
() {
return
pvalue
; }
64
69
std::shared_ptr<link>&
succ
() {
return
psucc
; }
70
75
explicit
link
(
int
value = 0) :
pvalue
(value),
psucc
(nullptr) {}
76
};
77
81
class
list
{
82
private
:
83
std::shared_ptr<link>
first
;
84
std::shared_ptr<link>
last
;
85
public
:
89
list
() {
90
// Initialize the first link
91
first
= std::make_shared<link>();
92
// Initialize the last link with the first link
93
last
=
nullptr
;
94
}
95
96
bool
isEmpty
();
97
98
void
push_back
(
int
new_elem);
99
void
push_front
(
int
new_elem);
100
void
erase
(
int
old_elem);
101
void
display();
102
std::shared_ptr<link>
search
(
int
find_elem);
103
void
reverse();
104
};
105
111
bool
list::isEmpty
() {
112
if
(
last
==
nullptr
) {
113
return
true
;
114
}
else
{
115
return
false
;
116
}
117
}
118
123
void
list::push_back
(
int
new_elem) {
124
if
(
isEmpty
()) {
125
first
->succ() = std::make_shared<link>(new_elem);
126
last
=
first
->succ();
127
}
else
{
128
last
->succ() = std::make_shared<link>(new_elem);
129
last
=
last
->succ();
130
}
131
}
132
137
void
list::push_front
(
int
new_elem) {
138
if
(
isEmpty
()) {
139
first
->succ() = std::make_shared<link>(new_elem);
140
last
=
first
->succ();
141
}
else
{
142
std::shared_ptr<link> t = std::make_shared<link>(new_elem);
143
t->succ() =
first
->succ();
144
first
->succ() = t;
145
}
146
}
147
152
void
list::erase
(
int
old_elem) {
153
if
(
isEmpty
()) {
154
std::cout <<
"List is Empty!"
;
155
return
;
156
}
157
std::shared_ptr<link> t =
first
;
158
std::shared_ptr<link> to_be_removed =
nullptr
;
159
while
(t !=
last
&& t->succ()->val() != old_elem) {
160
t = t->succ();
161
}
162
if
(t ==
last
) {
163
std::cout <<
"Element not found\n"
;
164
return
;
165
}
166
to_be_removed = t->succ();
167
t->succ() = t->succ()->succ();
168
to_be_removed.reset();
169
if
(t->succ() ==
nullptr
) {
170
last
= t;
171
}
172
if
(
first
==
last
){
173
last
=
nullptr
;
174
}
175
}
176
181
void
list::display
() {
182
if
(
isEmpty
()) {
183
std::cout <<
"List is Empty!"
;
184
return
;
185
}
186
std::shared_ptr<link> t =
first
;
187
while
(t->succ() !=
nullptr
) {
188
std::cout << t->succ()->val() <<
"\t"
;
189
t = t->succ();
190
}
191
}
192
197
std::shared_ptr<link>
list::search
(
int
find_elem) {
198
if
(
isEmpty
()) {
199
std::cout <<
"List is Empty!"
;
200
return
nullptr
;
201
}
202
std::shared_ptr<link> t =
first
;
203
while
(t !=
last
&& t->succ()->val() != find_elem) {
204
t = t->succ();
205
}
206
if
(t ==
last
) {
207
std::cout <<
"Element not found\n"
;
208
return
nullptr
;
209
}
210
std::cout <<
"Element was found\n"
;
211
return
t->succ();
212
}
213
}
// namespace linked_list
214
}
// namespace data_structures
215
222
int
main
() {
223
data_structures::linked_list::list
l;
224
int
choice = 0;
225
int
x = 0;
226
std::string s;
227
do
{
228
std::cout <<
"\n1. Insert"
;
229
std::cout <<
"\n2. Delete"
;
230
std::cout <<
"\n3. Search"
;
231
std::cout <<
"\n4. Print"
;
232
std::cout <<
"\n0. Exit"
;
233
std::cout <<
"\n\nEnter you choice : "
;
234
std::cin >> choice;
235
switch
(choice) {
236
case
0:
237
std::cout <<
"\nQuitting the program...\n"
;
238
break
;
239
case
1:
240
std::cout <<
"\nEnter the element to be inserted : "
;
241
std::cin >> s;
242
243
if
(
data_structures::linked_list::isDigit
(s)) {
244
x = std::stoi(s);
245
l.push_back(x);
246
}
else
{
247
std::cout <<
"Wrong Input!\n"
;
248
}
249
break
;
250
case
2:
251
std::cout <<
"\nEnter the element to be removed : "
;
252
std::cin >> s;
253
if
(
data_structures::linked_list::isDigit
(s)) {
254
x = std::stoi(s);
255
l.erase(x);
256
}
else
{
257
std::cout <<
"Wrong Input!\n"
;
258
}
259
break
;
260
case
3:
261
std::cout <<
"\nEnter the element to be searched : "
;
262
std::cin >> s;
263
if
(
data_structures::linked_list::isDigit
(s)) {
264
x = std::stoi(s);
265
std::shared_ptr<data_structures::linked_list::link> found =
266
l.search(x);
267
}
else
{
268
std::cout <<
"Wrong Input!\n"
;
269
}
270
break
;
271
case
4:
272
l.display();
273
std::cout <<
"\n"
;
274
break
;
275
default
:
276
std::cout <<
"Invalid Input\n"
<< std::endl;
277
break
;
278
}
279
}
while
(choice != 0);
280
return
0;
281
}
data_structures::linked_list::link::link
link(int value=0)
Definition
linked_list.cpp:75
data_structures::linked_list::link::pvalue
int pvalue
value of the current link
Definition
linked_list.cpp:55
data_structures::linked_list::link::val
int val()
Definition
linked_list.cpp:63
data_structures::linked_list::link::succ
std::shared_ptr< link > & succ()
Definition
linked_list.cpp:69
data_structures::linked_list::link::psucc
std::shared_ptr< link > psucc
pointer to the next value on the list
Definition
linked_list.cpp:56
data_structures::linked_list::list
Definition
linked_list.cpp:81
data_structures::linked_list::list::search
std::shared_ptr< link > search(int find_elem)
Definition
linked_list.cpp:197
data_structures::linked_list::list::erase
void erase(int old_elem)
Definition
linked_list.cpp:152
data_structures::linked_list::list::list
list()
Definition
linked_list.cpp:89
data_structures::linked_list::list::push_front
void push_front(int new_elem)
Definition
linked_list.cpp:137
data_structures::linked_list::list::first
std::shared_ptr< link > first
link before the actual first element
Definition
linked_list.cpp:83
data_structures::linked_list::list::last
std::shared_ptr< link > last
last link on the list
Definition
linked_list.cpp:84
data_structures::linked_list::list::display
void display()
Definition
linked_list.cpp:181
data_structures::linked_list::list::push_back
void push_back(int new_elem)
Definition
linked_list.cpp:123
data_structures::linked_list::list::isEmpty
bool isEmpty()
Definition
linked_list.cpp:111
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
data_structures::linked_list::isDigit
bool isDigit(const std::string &s)
Definition
linked_list.cpp:40
data_structures
for IO operations
linked_list
Functions for singly linked list algorithm.
search
for std::assert
Definition
binary_search.cpp:47
data_structures
linked_list.cpp
Generated by
1.18.0