Skip to content

Commit 7c09048

Browse files
vil02github-actions[bot]Panquesito7
authored
[fix/docs]: remove memory leak in queue (#2417)
* fix: remove memory leak in queue * updating DIRECTORY.md * clang-format and clang-tidy fixes for effd74c * style: simplify logic while using reserve * style: use value_type as return type in front * style: use proper error message * style: use pre-increment and pre-decrement * docs: use doxygen syntax * docs: improve wording Co-authored-by: github-actions[bot] <[email protected]> Co-authored-by: David Leal <[email protected]>
1 parent 5b23872 commit 7c09048

File tree

7 files changed

+250
-161
lines changed

7 files changed

+250
-161
lines changed

DIRECTORY.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@
5757
* [Linkedlist Implentation Usingarray](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/linkedlist_implentation_usingarray.cpp)
5858
* [List Array](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/list_array.cpp)
5959
* [Morrisinorder](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/morrisinorder.cpp)
60-
* [Queue](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/queue.h)
60+
* [Node](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/node.hpp)
61+
* [Queue](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/queue.hpp)
6162
* [Queue Using Array](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/queue_using_array.cpp)
6263
* [Queue Using Array2](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/queue_using_array2.cpp)
6364
* [Queue Using Linked List](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/queue_using_linked_list.cpp)

data_structures/node.hpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @file
3+
* @brief Provides Node class and related utilities
4+
**/
5+
#ifndef DATA_STRUCTURES_NODE_HPP_
6+
#define DATA_STRUCTURES_NODE_HPP_
7+
8+
#include <iostream> /// for std::cout
9+
#include <memory> /// for std::shared_ptr
10+
#include <vector> /// for std::vector
11+
12+
/** Definition of the node as a linked-list
13+
* \tparam ValueType type of data nodes of the linked list should contain
14+
*/
15+
template <class ValueType>
16+
struct Node {
17+
using value_type = ValueType;
18+
ValueType data = {};
19+
std::shared_ptr<Node<ValueType>> next = {};
20+
};
21+
22+
template <typename Node, typename Action>
23+
void traverse(const Node* const inNode, const Action& action) {
24+
if (inNode) {
25+
action(*inNode);
26+
traverse(inNode->next.get(), action);
27+
}
28+
}
29+
30+
template <typename Node>
31+
void display_all(const Node* const inNode) {
32+
traverse(inNode,
33+
[](const Node& curNode) { std::cout << curNode.data << " "; });
34+
}
35+
36+
template <typename Node>
37+
std::vector<typename Node::value_type> push_all_to_vector(
38+
const Node* const inNode, const std::size_t expected_size = 0) {
39+
std::vector<typename Node::value_type> res;
40+
res.reserve(expected_size);
41+
traverse(inNode,
42+
[&res](const Node& curNode) { res.push_back(curNode.data); });
43+
return res;
44+
}
45+
46+
#endif // DATA_STRUCTURES_NODE_HPP_

data_structures/queue.h

-88
This file was deleted.

data_structures/queue.hpp

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/* This class specifies the basic operation on a queue as a linked list */
2+
#ifndef DATA_STRUCTURES_QUEUE_HPP_
3+
#define DATA_STRUCTURES_QUEUE_HPP_
4+
5+
#include "node.hpp"
6+
7+
/** Definition of the queue class */
8+
template <class ValueType>
9+
class queue {
10+
using node_type = Node<ValueType>;
11+
12+
public:
13+
using value_type = ValueType;
14+
/**
15+
* @brief prints the queue into the std::cout
16+
*/
17+
void display() const {
18+
std::cout << "Front --> ";
19+
display_all(this->queueFront.get());
20+
std::cout << '\n';
21+
std::cout << "Size of queue: " << size << '\n';
22+
}
23+
24+
/**
25+
* @brief converts the queue into the std::vector
26+
* @return std::vector containning all of the elements of the queue in the
27+
* same order
28+
*/
29+
std::vector<value_type> toVector() const {
30+
return push_all_to_vector(this->queueFront.get(), this->size);
31+
}
32+
33+
private:
34+
/**
35+
* @brief throws an exception if queue is empty
36+
* @exception std::invalid_argument if queue is empty
37+
*/
38+
void ensureNotEmpty() const {
39+
if (isEmptyQueue()) {
40+
throw std::invalid_argument("Queue is empty.");
41+
}
42+
}
43+
44+
public:
45+
/**
46+
* @brief checks if the queue has no elements
47+
* @return true if the queue is empty, false otherwise
48+
*/
49+
bool isEmptyQueue() const { return (queueFront == nullptr); }
50+
51+
/**
52+
* @brief inserts a new item into the queue
53+
*/
54+
void enQueue(const value_type& item) {
55+
auto newNode = std::make_shared<node_type>();
56+
newNode->data = item;
57+
newNode->next = nullptr;
58+
if (isEmptyQueue()) {
59+
queueFront = newNode;
60+
queueRear = newNode;
61+
} else {
62+
queueRear->next = newNode;
63+
queueRear = queueRear->next;
64+
}
65+
++size;
66+
}
67+
68+
/**
69+
* @return the first element of the queue
70+
* @exception std::invalid_argument if queue is empty
71+
*/
72+
value_type front() const {
73+
ensureNotEmpty();
74+
return queueFront->data;
75+
}
76+
77+
/**
78+
* @brief removes the first element from the queue
79+
* @exception std::invalid_argument if queue is empty
80+
*/
81+
void deQueue() {
82+
ensureNotEmpty();
83+
queueFront = queueFront->next;
84+
--size;
85+
}
86+
87+
/**
88+
* @brief removes all elements from the queue
89+
*/
90+
void clear() {
91+
queueFront = nullptr;
92+
queueRear = nullptr;
93+
size = 0;
94+
}
95+
96+
private:
97+
std::shared_ptr<node_type> queueFront =
98+
{}; /**< Pointer to the front of the queue */
99+
std::shared_ptr<node_type> queueRear =
100+
{}; /**< Pointer to the rear of the queue */
101+
std::size_t size = 0;
102+
};
103+
104+
#endif // DATA_STRUCTURES_QUEUE_HPP_

data_structures/stack.hpp

+6-32
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,9 @@
77
#ifndef DATA_STRUCTURES_STACK_HPP_
88
#define DATA_STRUCTURES_STACK_HPP_
99

10-
#include <iostream> /// for IO operations
11-
#include <memory> /// for std::shared_ptr
1210
#include <stdexcept> /// for std::invalid_argument
13-
#include <vector> /// for std::vector
1411

15-
/** Definition of the node as a linked-list
16-
* \tparam ValueType type of data nodes of the linked list should contain
17-
*/
18-
template <class ValueType>
19-
struct node {
20-
ValueType data = {}; ///< data at current node
21-
std::shared_ptr<node<ValueType>> next =
22-
{}; ///< pointer to the next ::node instance
23-
};
24-
25-
template <typename Node, typename Action>
26-
void traverse(const Node* const inNode, const Action& action) {
27-
if (inNode) {
28-
action(*inNode);
29-
traverse(inNode->next.get(), action);
30-
}
31-
}
12+
#include "node.hpp" /// for Node
3213

3314
/** Definition of the stack class
3415
* \tparam value_type type of data nodes of the linked list in the stack should
@@ -42,20 +23,13 @@ class stack {
4223
/** Show stack */
4324
void display() const {
4425
std::cout << "Top --> ";
45-
traverse(stackTop.get(), [](const node<value_type>& inNode) {
46-
std::cout << inNode.data << " ";
47-
});
48-
std::cout << std::endl;
26+
display_all(this->stackTop.get());
27+
std::cout << '\n';
4928
std::cout << "Size of stack: " << size << std::endl;
5029
}
5130

5231
std::vector<value_type> toVector() const {
53-
std::vector<value_type> res;
54-
res.reserve(this->size);
55-
traverse(stackTop.get(), [&res](const node<value_type>& inNode) {
56-
res.push_back(inNode.data);
57-
});
58-
return res;
32+
return push_all_to_vector(this->stackTop.get(), this->size);
5933
}
6034

6135
private:
@@ -71,7 +45,7 @@ class stack {
7145

7246
/** Add new item to the stack */
7347
void push(const value_type& item) {
74-
auto newNode = std::make_shared<node<value_type>>();
48+
auto newNode = std::make_shared<Node<value_type>>();
7549
newNode->data = item;
7650
newNode->next = stackTop;
7751
stackTop = newNode;
@@ -98,7 +72,7 @@ class stack {
9872
}
9973

10074
private:
101-
std::shared_ptr<node<value_type>> stackTop =
75+
std::shared_ptr<Node<value_type>> stackTop =
10276
{}; /**< Pointer to the stack */
10377
std::size_t size = 0; ///< size of stack
10478
};

0 commit comments

Comments
 (0)