Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
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
 Data Structures algorithms.
 
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
201 {
202 test(); // run self-test implementations
203 return 0;
204}
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
173 {
175 // 1st test
176 L.insert(11);
177 L.insert(12);
178 L.insert(15);
179 L.insert(10);
180 L.insert(-12);
181 L.insert(-20);
182 L.insert(18);
183 assert(L.top() == 11);
184 assert(L.last() == 18);
185 L.reverseList();
186 // Reversal Testing
187 assert(L.top() == 18);
188 assert(L.traverse(1) == -20);
189 assert(L.traverse(2) == -12);
190 assert(L.traverse(3) == 10);
191 assert(L.traverse(4) == 15);
192 assert(L.traverse(5) == 12);
193 assert(L.last() == 11);
194 std::cout << "All tests have successfully passed!" << std::endl;
195}
Definition: linked_list.cpp:81
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
void reverseList()
Utility function for reversing a list.
Definition: reverse_a_linked_list.cpp:107
int32_t top()
Utility function to find the top element of the list.
Definition: reverse_a_linked_list.cpp:123
std::shared_ptr< link > last
last link on the list
Definition: linked_list.cpp:84
int32_t traverse(int32_t index)
Utility function to find the i th element of the list.
Definition: reverse_a_linked_list.cpp:149
T endl(T... args)
Here is the call graph for this function: