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
205 {
206 test(); // run self-test implementations
207 return 0;
208}
static void test()
Self-test implementations.
Definition: reverse_a_linked_list.cpp:177
Here is the call graph for this function:

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void
177 {
179 // 1st test
180 L.insert(11);
181 L.insert(12);
182 L.insert(15);
183 L.insert(10);
184 L.insert(-12);
185 L.insert(-20);
186 L.insert(18);
187 assert(L.top() == 11);
188 assert(L.last() == 18);
189 L.reverseList();
190 // Reversal Testing
191 assert(L.top() == 18);
192 assert(L.traverse(1) == -20);
193 assert(L.traverse(2) == -12);
194 assert(L.traverse(3) == 10);
195 assert(L.traverse(4) == 15);
196 assert(L.traverse(5) == 12);
197 assert(L.last() == 11);
198 std::cout << "All tests have successfully passed!" << std::endl;
199}
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:153
T endl(T... args)
Here is the call graph for this function: