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

  s | To 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

{
// function statements here
for (char i : s) {
if (!isdigit(i)) {
return false;
}
}
return true;
}
std::isdigit
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

{
data_structures::linked_list::list l;
int choice = 0;
int x = 0;
std::string s;
do {
std::cout << "\n1. Insert";
std::cout << "\n2. Delete";
std::cout << "\n3. Search";
std::cout << "\n4. Print";
std::cout << "\n0. Exit";
std::cout << "\n\nEnter you choice : ";
std::cin >> choice;
switch (choice) {
case 1:
std::cout << "\nEnter the element to be inserted : ";
std::cin >> s;
if (data_structures::linked_list::isDigit(s)) {
x = std::stoi(s);
l.push_back(x);
} else {
std::cout << "Wrong Input!\n";
}
break;
case 2:
std::cout << "\nEnter the element to be removed : ";
std::cin >> s;
if (data_structures::linked_list::isDigit(s)) {
x = std::stoi(s);
l.erase(x);
} else {
std::cout << "Wrong Input!\n";
}
break;
case 3:
std::cout << "\nEnter the element to be searched : ";
std::cin >> s;
if (data_structures::linked_list::isDigit(s)) {
x = std::stoi(s);
std::shared_ptr<data_structures::linked_list::link> found =
l.search(x);
} else {
std::cout << "Wrong Input!\n";
}
break;
case 4:
l.display();
std::cout << "\n";
break;
default:
std::cout << "Invalid Input\n" << std::endl;
break;
}
} while (choice != 0);
return 0;
}
std::cin
std::cout
std::string
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:194
data_structures::linked_list::list::erase
void erase(int old_elem)
Definition: linked_list.cpp:152
data_structures::linked_list::list::display
void display()
Definition: linked_list.cpp:178
data_structures::linked_list::list::push_back
void push_back(int new_elem)
Definition: linked_list.cpp:123
std::endl
T endl(T... args)
std::shared_ptr< data_structures::linked_list::link >
std::stoi
T stoi(T... args)
Here is the call graph for this function: