TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Implementation of Reversing a single linked list More...
#include <cassert>
#include <iostream>
#include <new>
Go to the source code of this file.
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 | |
Node * | data_structures::linked_list::copy_all_nodes (const Node *const node) |
creates a deep copy of a list starting at the input node | |
static void | test () |
Self-test implementations. | |
void | test_copy_constructor () |
void | test_assignment_operator () |
int | main () |
Main function. | |
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.
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
Definition in file reverse_a_linked_list.cpp.
creates a deep copy of a list starting at the input node
[in] | node | pointer to the first node/head of the list to be copied |
Definition at line 53 of file reverse_a_linked_list.cpp.
int main | ( | void | ) |
Main function.
Definition at line 301 of file reverse_a_linked_list.cpp.
|
static |
Self-test implementations.
Definition at line 228 of file reverse_a_linked_list.cpp.
void test_assignment_operator | ( | ) |
Definition at line 274 of file reverse_a_linked_list.cpp.
void test_copy_constructor | ( | ) |
Definition at line 252 of file reverse_a_linked_list.cpp.