Skip to content

Commit e2bf654

Browse files
vil02Panquesito7Daemon19aadarshktFocusucof
authored
[feat/fix]: improve the data stack implementations (#2235)
* fix: remove memory leak in stack * style: use camelCase * style: simplify logic * style: rename stack.h to stack.hpp * updating DIRECTORY.md * clang-format and clang-tidy fixes for 2205c6f * fix: remove redundant file name Co-authored-by: David Leal <[email protected]> * clang-format and clang-tidy fixes for a080aaa * refactor: use std::shared_ptr * fix: use C++11 * chore: synchronize with master (#2) * chore: fix Markdown formatting in `dynamic_programming/kadane2.cpp` (#2276) * docs: fix grammatical errors and typos (#2201) * docs: fix grammatical errors and typos * compilation error fixed * Revert "compilation error fixed" This reverts commit 0083cbf. * feat: added physics directory and ground to ground projectile motion algorithm (#2279) * feat: added physics folder, ground to ground projectile motion calculations * feat: added max height function * fix: bug in angle calculations * test: added test cases * docs: added comments to test case variables * docs: added comments to calculations * fix: changed floats to doubles * updating DIRECTORY.md * Update physics/ground_to_ground_projectile_motion.cpp Co-authored-by: David Leal <[email protected]> * chore: add missing namespace * rerun checks Co-authored-by: David <[email protected]> Co-authored-by: David Leal <[email protected]> * docs: updated a logically wrong doc comment (#2329) * feat: add CMakeLists to the `divide_and_conquer` directory (#2072) * fix: stairs pattern not printing slash (#2111) when we propose 2 backslash then it doesn't take it as a comment and gets printed Co-authored-by: David Leal <[email protected]> * fix: use FreeGlut newest GitHub link (#2397) * updating DIRECTORY.md * fix: use FreeGlut newest GitHub link * chore(fix): `data_strcutres` -> `data_structures` (#2399) * feat: add Find non repeating number implementation (#2061) * add find_single_number * add fix issues * remove .vscode * add .vscode * Update .vscode/settings.json Co-authored-by: David Leal <[email protected]> * chore(fix): minor issues Co-authored-by: David Leal <[email protected]> * [feat/docs]: improve the `quick_sort.cpp` algorithm (#2396) * [feat/docs]: improve the `quick_sort.cpp`... ...algorithm implementation. * clang-format and clang-tidy fixes for 40c858b * chore(fix): add original author * updating DIRECTORY.md * chore: update Discord links (#2407) * docs: remove unneeded Markdown header * feat: improve the Awesome Workflow (#2408) * fix: Awesome Workflow issues Thanks to @tjgurwara99 for the original fix: TheAlgorithms/C#1176 * chore: apply suggestions from code review Co-authored-by: Taj <[email protected]> * feat: various improvements * chore: apply suggestions from code review Co-authored-by: Taj <[email protected]> * chore: remove LGTM and fix... ...CodeQL badges. * docs: add guide on integrating CMake (#2410) Taken from TheAlgorithms/C#1163 * updating DIRECTORY.md Co-authored-by: Daemon <[email protected]> Co-authored-by: aadarshkt <[email protected]> Co-authored-by: Focus <[email protected]> Co-authored-by: David <[email protected]> Co-authored-by: David Leal <[email protected]> Co-authored-by: Arjit Malik <[email protected]> Co-authored-by: Harsh Singh <[email protected]> Co-authored-by: Ravi Dev Pandey <[email protected]> Co-authored-by: Mehmet <[email protected]> Co-authored-by: Taj <[email protected]> Co-authored-by: github-actions[bot] <[email protected]> * docs: update authors and include comments apply suggestions of @Panquesito7 Co-authored-by: David Leal <[email protected]> * style: apply clang-format * clang-format and clang-tidy fixes for b35b721 * style: make display and isEmptyStack const * tests: remove test_stack_legacy.cpp * style: throw invalid_argument from top and pop if stack empty * updating DIRECTORY.md * style: add missing include docs, remove cassert * style: use assert macro, document includes * fix: use const reference in lambdas in display and toVector * style: remove shared_ptr from traverse Co-authored-by: David Leal <[email protected]> Co-authored-by: Daemon <[email protected]> Co-authored-by: aadarshkt <[email protected]> Co-authored-by: Focus <[email protected]> Co-authored-by: Arjit Malik <[email protected]> Co-authored-by: Harsh Singh <[email protected]> Co-authored-by: Ravi Dev Pandey <[email protected]> Co-authored-by: Mehmet <[email protected]> Co-authored-by: Taj <[email protected]> Co-authored-by: github-actions[bot] <[email protected]>
1 parent 582cdc7 commit e2bf654

File tree

5 files changed

+309
-208
lines changed

5 files changed

+309
-208
lines changed

DIRECTORY.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
* [Reverse A Linked List](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/reverse_a_linked_list.cpp)
6767
* [Skip List](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/skip_list.cpp)
6868
* [Sparse Table](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/sparse_table.cpp)
69-
* [Stack](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/stack.h)
69+
* [Stack](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/stack.hpp)
7070
* [Stack Using Array](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/stack_using_array.cpp)
7171
* [Stack Using Linked List](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/stack_using_linked_list.cpp)
7272
* [Stack Using Queue](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/stack_using_queue.cpp)

data_structures/stack.h

-150
This file was deleted.

data_structures/stack.hpp

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* @file
3+
* @author danghai
4+
* @author [Piotr Idzik](https://github.com/vil02)
5+
* @brief This class specifies the basic operation on a stack as a linked list
6+
**/
7+
#ifndef DATA_STRUCTURES_STACK_HPP_
8+
#define DATA_STRUCTURES_STACK_HPP_
9+
10+
#include <iostream> /// for IO operations
11+
#include <memory> /// for std::shared_ptr
12+
#include <stdexcept> /// for std::invalid_argument
13+
#include <vector> /// for std::vector
14+
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+
}
32+
33+
/** Definition of the stack class
34+
* \tparam value_type type of data nodes of the linked list in the stack should
35+
* contain
36+
*/
37+
template <class ValueType>
38+
class stack {
39+
public:
40+
using value_type = ValueType;
41+
42+
/** Show stack */
43+
void display() const {
44+
std::cout << "Top --> ";
45+
traverse(stackTop.get(), [](const node<value_type>& inNode) {
46+
std::cout << inNode.data << " ";
47+
});
48+
std::cout << std::endl;
49+
std::cout << "Size of stack: " << size << std::endl;
50+
}
51+
52+
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;
59+
}
60+
61+
private:
62+
void ensureNotEmpty() const {
63+
if (isEmptyStack()) {
64+
throw std::invalid_argument("Stack is empty.");
65+
}
66+
}
67+
68+
public:
69+
/** Determine whether the stack is empty */
70+
bool isEmptyStack() const { return (stackTop == nullptr); }
71+
72+
/** Add new item to the stack */
73+
void push(const value_type& item) {
74+
auto newNode = std::make_shared<node<value_type>>();
75+
newNode->data = item;
76+
newNode->next = stackTop;
77+
stackTop = newNode;
78+
size++;
79+
}
80+
81+
/** Return the top element of the stack */
82+
value_type top() const {
83+
ensureNotEmpty();
84+
return stackTop->data;
85+
}
86+
87+
/** Remove the top element of the stack */
88+
void pop() {
89+
ensureNotEmpty();
90+
stackTop = stackTop->next;
91+
size--;
92+
}
93+
94+
/** Clear stack */
95+
void clear() {
96+
stackTop = nullptr;
97+
size = 0;
98+
}
99+
100+
private:
101+
std::shared_ptr<node<value_type>> stackTop =
102+
{}; /**< Pointer to the stack */
103+
std::size_t size = 0; ///< size of stack
104+
};
105+
106+
#endif // DATA_STRUCTURES_STACK_HPP_

0 commit comments

Comments
 (0)