# Algorithms_in_C++: data_structures/reverse_a_linked_list.cpp File Reference

Loading...
Searching...
No Matches
reverse_a_linked_list.cpp File Reference

Implementation of Reversing a single linked list More...

`#include <cassert>`
`#include <iostream>`
`#include <memory>`
`#include <new>`
Include dependency graph for reverse_a_linked_list.cpp:

## Classes

class | data_structures::linked_list::Node
class | data_structures::linked_list::list

## Namespaces

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

## Functions

static void | test ()
| Self-test implementations. More...
int | main ()
| Main function. More...

## Detailed Description

Implementation of Reversing a single linked list

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

### 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.

Linked List can be reversed by using 3 pointers: current, previous, and next_node; we keep iterating until the last node. Meanwhile, before changing to the next of current, we store it in the next_node pointer, now we store the prev pointer in the current of next, this is where the actual reversal happens. And then we move the prev and current pointers one step forward. Then the head node is made to point to the last node (prev pointer) after completion of an iteration.

A graphic explanation and view of what's happening behind the scenes

## Function Documentation

## main()

int main | ( | void |  | ) |

Main function.

Returns
  0 on exit

{
test(); // run self-test implementations
return 0;
}
test
static void test()
Self-test implementations.
Definition: reverse_a_linked_list.cpp:173
Here is the call graph for this function:

## test()

static void test | ( |  | ) |

static

Self-test implementations.

Returns
  void

{
data_structures::linked_list::list L;
// 1st test
L.insert(11);
L.insert(12);
L.insert(15);
L.insert(10);
L.insert(-12);
L.insert(-20);
L.insert(18);
assert(L.top() == 11);
assert(L.last() == 18);
L.reverseList();
// Reversal Testing
assert(L.top() == 18);
assert(L.traverse(1) == -20);
assert(L.traverse(2) == -12);
assert(L.traverse(3) == 10);
assert(L.traverse(4) == 15);
assert(L.traverse(5) == 12);
assert(L.last() == 11);
std::cout << "All tests have successfully passed!" << std::endl;
}
std::cout
data_structures::linked_list::list
Definition: linked_list.cpp:81
data_structures::linked_list::list::insert
void insert(int32_t new_elem)
Utility function that adds a new element at the end of the list.
Definition: reverse_a_linked_list.cpp:82
data_structures::linked_list::list::reverseList
void reverseList()
Utility function for reversing a list.
Definition: reverse_a_linked_list.cpp:107
data_structures::linked_list::list::top
int32_t top()
Utility function to find the top element of the list.
Definition: reverse_a_linked_list.cpp:123
data_structures::linked_list::list::last
std::shared_ptr< link > last
last link on the list
Definition: linked_list.cpp:84
data_structures::linked_list::list::traverse
int32_t traverse(int32_t index)
Utility function to find the i th element of the list.
Definition: reverse_a_linked_list.cpp:149
std::endl
T endl(T... args)
Here is the call graph for this function: