TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
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
25namespace data_structures {
26
31namespace linked_list {
32
40bool 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
53class 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
81class 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
112 if (last == nullptr) {
113 return true;
114 } else {
115 return false;
116 }
117}
118
123void 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
137void 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
152void 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
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
197std::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
222int main() {
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}
std::shared_ptr< link > first
link before the actual first element
std::shared_ptr< link > search(int find_elem)
std::shared_ptr< link > last
last link on the list
bool isDigit(const std::string &s)
int main()
for IO operations
Functions for singly linked list algorithm.
for std::assert