-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_stack.h
More file actions
38 lines (32 loc) · 875 Bytes
/
Copy pathprint_stack.h
File metadata and controls
38 lines (32 loc) · 875 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#ifndef CPP_ALGORITHM_PRINT_STACK_H
#define CPP_ALGORITHM_PRINT_STACK_H
#include "linked_list.h"
#include <iostream>
#include <stack>
namespace PrintStack
{
/**
* \brief Print the linked list in reverse order using stack.
* \param head the head of the stack
*/
void PrintLinkedListInReverseOrder(
const std::shared_ptr<LinkedList::Node<int>>& head);
}
// ----------------------------------------------------------------------------
inline void PrintStack::PrintLinkedListInReverseOrder(
const std::shared_ptr<LinkedList::Node<int>>& head)
{
std::stack<std::shared_ptr<LinkedList::Node<int>>> nodes;
auto node = head;
while (node != nullptr)
{
nodes.push(node);
node = node->next;
}
while (!nodes.empty())
{
std::cout << nodes.top()->data << " ";
nodes.pop();
}
}
#endif