Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
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:

Classes

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

Namespaces

 data_structures
 Data Structures algorithms.
 
 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.

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 only digits present in the string
false if any other character is found
40  {
41  // function statements here
42  for (char i : s) {
43  if (!isdigit(i)) {
44  return false;
45  }
46  }
47  return true;
48 }
Here is the call graph for this function:

◆ 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
219  {
221  int choice = 0;
222  int x = 0;
223  std::string s;
224  do {
225  std::cout << "\n1. Insert";
226  std::cout << "\n2. Delete";
227  std::cout << "\n3. Search";
228  std::cout << "\n4. Print";
229  std::cout << "\n0. Exit";
230  std::cout << "\n\nEnter you choice : ";
231  std::cin >> choice;
232  switch (choice) {
233  case 1:
234  std::cout << "\nEnter the element to be inserted : ";
235  std::cin >> s;
236 
237  if (data_structures::linked_list::isDigit(s)) {
238  x = std::stoi(s);
239  l.push_back(x);
240  } else {
241  std::cout << "Wrong Input!\n";
242  }
243  break;
244  case 2:
245  std::cout << "\nEnter the element to be removed : ";
246  std::cin >> s;
247  if (data_structures::linked_list::isDigit(s)) {
248  x = std::stoi(s);
249  l.erase(x);
250  } else {
251  std::cout << "Wrong Input!\n";
252  }
253  break;
254  case 3:
255  std::cout << "\nEnter the element to be searched : ";
256  std::cin >> s;
257  if (data_structures::linked_list::isDigit(s)) {
258  x = std::stoi(s);
260  l.search(x);
261  } else {
262  std::cout << "Wrong Input!\n";
263  }
264  break;
265  case 4:
266  l.display();
267  std::cout << "\n";
268  break;
269  default:
270  std::cout << "Invalid Input\n" << std::endl;
271  break;
272  }
273  } while (choice != 0);
274  return 0;
275 }
Here is the call graph for this function:
std::isdigit
T isdigit(T... args)
data_structures::linked_list::list::erase
void erase(int old_elem)
Definition: linked_list.cpp:152
std::string
STL class.
std::shared_ptr< data_structures::linked_list::link >
data_structures::linked_list::list
Definition: linked_list.cpp:81
data_structures::linked_list::list::display
void display()
Definition: linked_list.cpp:178
std::stoi
T stoi(T... args)
data_structures::linked_list::list::push_back
void push_back(int new_elem)
Definition: linked_list.cpp:123
std::cout
data_structures::linked_list::list::search
std::shared_ptr< link > search(int find_elem)
Definition: linked_list.cpp:194
std::endl
T endl(T... args)
std::cin