TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
reverse_a_linked_list.cpp File Reference

Implementation of Reversing a single linked list More...

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

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

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

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

Definition in file reverse_a_linked_list.cpp.

Function Documentation

◆ copy_all_nodes()

Node * data_structures::linked_list::copy_all_nodes ( const Node *const node)

creates a deep copy of a list starting at the input node

Parameters
[in]nodepointer to the first node/head of the list to be copied
Returns
pointer to the first node/head of the copied list or nullptr

Definition at line 53 of file reverse_a_linked_list.cpp.

53 {
54 if (node) {
55 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
56 Node* res = new Node();
57 res->val = node->val;
58 res->next = copy_all_nodes(node->next);
59 return res;
60 }
61 return nullptr;
62}
Node * copy_all_nodes(const Node *const node)
creates a deep copy of a list starting at the input node

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 301 of file reverse_a_linked_list.cpp.

301 {
302 test(); // run self-test implementations
303 test_copy_constructor();
304 test_assignment_operator();
305 return 0;
306}
static void test()
Self-test implementations.

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void

Definition at line 228 of file reverse_a_linked_list.cpp.

228 {
230 // 1st test
231 L.insert(11);
232 L.insert(12);
233 L.insert(15);
234 L.insert(10);
235 L.insert(-12);
236 L.insert(-20);
237 L.insert(18);
238 assert(L.top() == 11);
239 assert(L.last() == 18);
240 L.reverseList();
241 // Reversal Testing
242 assert(L.top() == 18);
243 assert(L.traverse(1) == -20);
244 assert(L.traverse(2) == -12);
245 assert(L.traverse(3) == 10);
246 assert(L.traverse(4) == 15);
247 assert(L.traverse(5) == 12);
248 assert(L.last() == 11);
249 std::cout << "All tests have successfully passed!" << std::endl;
250}
int32_t traverse(int32_t index) const
Utility function to find the i th element of the list.
int32_t top() const
Utility function to find the top element of the list.
void insert(int32_t new_elem)
Utility function that adds a new element at the end of the list.
void reverseList()
Utility function for reversing a list.
std::shared_ptr< link > last
last link on the list

◆ test_assignment_operator()

void test_assignment_operator ( )

Definition at line 274 of file reverse_a_linked_list.cpp.

274 {
277 L.insert(10);
278 L.insert(20);
279 L.insert(30);
280 otherList = L;
281
282 otherList.insert(40);
283 L.insert(400);
284
285 assert(L.top() == 10);
286 assert(otherList.top() == 10);
287 assert(L.traverse(1) == 20);
288 assert(otherList.traverse(1) == 20);
289
290 assert(L.traverse(2) == 30);
291 assert(otherList.traverse(2) == 30);
292
293 assert(L.last() == 400);
294 assert(otherList.last() == 40);
295}

◆ test_copy_constructor()

void test_copy_constructor ( )

Definition at line 252 of file reverse_a_linked_list.cpp.

252 {
254 L.insert(10);
255 L.insert(20);
256 L.insert(30);
258 otherList.insert(40);
259
260 L.insert(400);
261
262 assert(L.top() == 10);
263 assert(otherList.top() == 10);
264 assert(L.traverse(1) == 20);
265 assert(otherList.traverse(1) == 20);
266
267 assert(L.traverse(2) == 30);
268 assert(otherList.traverse(2) == 30);
269
270 assert(L.last() == 400);
271 assert(otherList.last() == 40);
272}