forked from TheAlgorithms/C-Plus-Plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
226 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* @file | ||
* @brief Provides Node class and related utilities | ||
**/ | ||
#ifndef DATA_STRUCTURES_NODE_HPP_ | ||
#define DATA_STRUCTURES_NODE_HPP_ | ||
|
||
#include <iostream> /// for std::cout | ||
#include <memory> /// for std::shared_ptr | ||
#include <vector> /// for std::vector | ||
|
||
/** Definition of the node as a linked-list | ||
* \tparam ValueType type of data nodes of the linked list should contain | ||
*/ | ||
template <class ValueType> | ||
struct Node { | ||
using value_type = ValueType; | ||
ValueType data = {}; | ||
std::shared_ptr<Node<ValueType>> next = {}; | ||
}; | ||
|
||
template <typename Node, typename Action> | ||
void traverse(const Node* const inNode, const Action& action) { | ||
if (inNode) { | ||
action(*inNode); | ||
traverse(inNode->next.get(), action); | ||
} | ||
} | ||
|
||
template <typename Node> | ||
void display_all(const Node* const inNode) { | ||
traverse(inNode, | ||
[](const Node& curNode) { std::cout << curNode.data << " "; }); | ||
} | ||
|
||
template <typename Node> | ||
std::vector<typename Node::value_type> push_all_to_vector( | ||
const Node* const inNode, const std::size_t expected_size=0) { | ||
std::vector<typename Node::value_type> res; | ||
if (expected_size != 0) { | ||
res.reserve(expected_size); | ||
} | ||
traverse(inNode, | ||
[&res](const Node& curNode) { res.push_back(curNode.data); }); | ||
return res; | ||
} | ||
|
||
#endif // DATA_STRUCTURES_NODE_HPP_ |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* This class specifies the basic operation on a queue as a linked list */ | ||
#ifndef DATA_STRUCTURES_QUEUE_HPP_ | ||
#define DATA_STRUCTURES_QUEUE_HPP_ | ||
|
||
#include "node.hpp" | ||
|
||
/** Definition of the queue class */ | ||
template <class ValueType> | ||
class queue { | ||
using node_type = Node<ValueType>; | ||
|
||
public: | ||
using value_type = ValueType; | ||
/** Show queue */ | ||
void display() const { | ||
std::cout << "Front --> "; | ||
display_all(this->queueFront.get()); | ||
std::cout << '\n'; | ||
std::cout << "Size of queue: " << size << '\n'; | ||
} | ||
|
||
std::vector<value_type> toVector() const { | ||
return push_all_to_vector(this->queueFront.get(), this->size); | ||
} | ||
|
||
private: | ||
void ensureNotEmpty() const { | ||
if (isEmptyQueue()) { | ||
throw std::invalid_argument("Stack is empty."); | ||
} | ||
} | ||
|
||
public: | ||
/** Determine whether the queue is empty */ | ||
bool isEmptyQueue() const { return (queueFront == nullptr); } | ||
|
||
/** Add new item to the queue */ | ||
void enQueue(const value_type& item) { | ||
auto newNode = std::make_shared<node_type>(); | ||
newNode->data = item; | ||
newNode->next = nullptr; | ||
if (isEmptyQueue()) { | ||
queueFront = newNode; | ||
queueRear = newNode; | ||
} else { | ||
queueRear->next = newNode; | ||
queueRear = queueRear->next; | ||
} | ||
size++; | ||
} | ||
|
||
/** Return the first element of the queue */ | ||
ValueType front() const { | ||
ensureNotEmpty(); | ||
return queueFront->data; | ||
} | ||
|
||
/** Remove the top element of the queue */ | ||
void deQueue() { | ||
ensureNotEmpty(); | ||
queueFront = queueFront->next; | ||
size--; | ||
} | ||
|
||
/** Clear queue */ | ||
void clear() { | ||
queueFront = nullptr; | ||
queueRear = nullptr; | ||
size = 0; | ||
} | ||
|
||
private: | ||
std::shared_ptr<node_type> queueFront = | ||
{}; /**< Pointer to the front of the queue */ | ||
std::shared_ptr<node_type> queueRear = | ||
{}; /**< Pointer to the rear of the queue */ | ||
std::size_t size = 0; | ||
}; | ||
|
||
#endif // DATA_STRUCTURES_QUEUE_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.