TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
linked_list.cpp File Reference

Implementation of singly linked list algorithm. More...

#include <iostream>
#include <memory>
#include <string>
Include dependency graph for linked_list.cpp:

Go to the source code of this file.

Classes

class  data_structures::linked_list::link
 
class  data_structures::linked_list::list
 

Namespaces

namespace  data_structures
 for IO operations
 
namespace  linked_list
 Functions for singly linked list algorithm.
 

Functions

bool data_structures::linked_list::isDigit (const std::string &s)
 
int main ()
 

Detailed Description

Implementation of singly linked list algorithm.

The linked list is a data structure used for holding a sequence of values, which can be added, removed and displayed.

Algorithm

Values can be added by iterating to the end of a list(by following the pointers) starting from the first link. Whichever link points to null is considered the last link and is pointed to the new value.

Values can be removed by also iterating through the list. When the node containing the value is found, the node pointing to the current node is made to point to the node that the current node is pointing to, and then returning the current node to heap store.

Definition in file linked_list.cpp.

Function Documentation

◆ isDigit()

bool data_structures::linked_list::isDigit ( const std::string & s)

This function checks if the string passed consists of only digits.

Parameters
sTo be checked if s contains only integers
Returns
true if there are only digits present in the string
false if any other character is found

Definition at line 40 of file linked_list.cpp.

40 {
41 // function statements here
42 for (char i : s) {
43 if (!isdigit(i)) {
44 return false;
45 }
46 }
47 return true;
48}

◆ main()

int main ( void )

Main function: Allows the user add and delete values from the list. Also allows user to search for and display values in the list.

Returns
0 on exit

Definition at line 222 of file linked_list.cpp.

222 {
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}
double l(double x)
Another test function.