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 }
T isdigit(T... args)
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
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 1:
237  std::cout << "\nEnter the element to be inserted : ";
238  std::cin >> s;
239 
240  if (data_structures::linked_list::isDigit(s)) {
241  x = std::stoi(s);
242  l.push_back(x);
243  } else {
244  std::cout << "Wrong Input!\n";
245  }
246  break;
247  case 2:
248  std::cout << "\nEnter the element to be removed : ";
249  std::cin >> s;
250  if (data_structures::linked_list::isDigit(s)) {
251  x = std::stoi(s);
252  l.erase(x);
253  } else {
254  std::cout << "Wrong Input!\n";
255  }
256  break;
257  case 3:
258  std::cout << "\nEnter the element to be searched : ";
259  std::cin >> s;
260  if (data_structures::linked_list::isDigit(s)) {
261  x = std::stoi(s);
263  l.search(x);
264  } else {
265  std::cout << "Wrong Input!\n";
266  }
267  break;
268  case 4:
269  l.display();
270  std::cout << "\n";
271  break;
272  default:
273  std::cout << "Invalid Input\n" << std::endl;
274  break;
275  }
276  } while (choice != 0);
277  return 0;
278 }
Definition: linked_list.cpp:81
std::shared_ptr< link > search(int find_elem)
Definition: linked_list.cpp:197
void erase(int old_elem)
Definition: linked_list.cpp:152
void display()
Definition: linked_list.cpp:181
void push_back(int new_elem)
Definition: linked_list.cpp:123
T endl(T... args)
T stoi(T... args)
Here is the call graph for this function: