Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] master from TheAlgorithms:master #36

Open
wants to merge 73 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
8a36824
fix: remove memory leak `iterative_factorial.cpp` (#2535)
vil02 Aug 30, 2024
e139ee9
fix: add and use function `deleteList` to remove memory leak (#2534)
vil02 Aug 30, 2024
435f1ed
chore: Make CI builds parallel (#2714)
realstealthninja Aug 30, 2024
9e44b68
fix: remove memory leak (#2532)
vil02 Aug 30, 2024
519d37f
fix: remove memory issues (#2533)
vil02 Aug 30, 2024
7fc338d
fix: use `GITHUB_ACTOR` in `git config` (#2673)
vil02 Aug 31, 2024
8bde3ea
fix: remove memory leak in `sublist_search.cpp` (#2541)
vil02 Aug 31, 2024
af72fab
fix: make interface of `NCRModuloP` fail-safe (#2469)
vil02 Aug 31, 2024
4a03c62
Fix compilation failure with gcc >= 13 (#2702)
VirtualTim Aug 31, 2024
1f5828e
fix: fix deprecation warning for macOS (#2711)
MaximSmolskiy Aug 31, 2024
c84c7da
[fix/docs]: improve the Jump Game algorithm (#2514)
Panquesito7 Aug 31, 2024
1d69222
refactor: fix typo in neural_network.cpp (#2689)
eltociear Aug 31, 2024
9702903
Implement Trapped RainWater (#2716)
sozelfist Aug 31, 2024
502a953
feat: let KMP algorithm return index (#2713)
Yancey2023 Sep 1, 2024
fe41cf4
docs: update DIRECTORY.md (#2718)
github-actions[bot] Sep 1, 2024
54a20b4
Rework on Stack Array Data Structure (#2683)
sozelfist Sep 1, 2024
52db277
fix: remove memory leak from `iterative_tree_traversals.cpp` (#2720)
vil02 Sep 1, 2024
db182d5
fix: fix bug in timSort (#2692)
MaximSmolskiy Sep 1, 2024
b6108e4
fix: remove memory leak from `recursive_tree_traversal.cpp` (#2721)
vil02 Sep 2, 2024
c652c4f
fix: stack buffer underflow in `tim_sort.cpp` (#2722)
vil02 Sep 3, 2024
490974e
fix: memory leak in `median_search2.cpp` (#2723)
vil02 Sep 4, 2024
ef209df
Added Iterative Quick Sort (#2684)
sebe324 Sep 4, 2024
15e3fed
style: add missing `const` in `bloom_filter.cpp` (#2724)
vil02 Sep 4, 2024
d74f4d3
fix: memory leak in `huffman.cpp` (#2728)
vil02 Sep 11, 2024
7828b8e
fix: memory leak in `morrisinorder.cpp` (#2729)
vil02 Sep 11, 2024
b169269
fix: remove memory leak from `reverse_binary_tree.cpp` (#2730)
vil02 Sep 13, 2024
920b6d4
Added test cases in greedy_algorithms\kruskals_minimum_spanning_tree.…
Monalisa0311 Sep 13, 2024
8df7a88
fix: remove memory leak from `circular_queue_using_linked_list.cpp` (…
vil02 Sep 13, 2024
a0b7dee
style: lint `huffman.cpp` (#2733)
vil02 Sep 15, 2024
9374b00
style: add missing `const` in `median_search2.cpp` (#2734)
vil02 Sep 17, 2024
f789e3b
style: cleanup `catalan_numbers.cpp` (#2740)
vil02 Sep 29, 2024
cde1672
feat: Create gray_code.cpp (#2750)
hollowcrust Oct 4, 2024
901f231
feat: Added gale_shapley.cpp in greedy_algorithms (#2743)
BKarthik7 Oct 4, 2024
c56496d
chore: use M_PI instead of PI parameter (#2747)
Divyansh-jain2 Oct 4, 2024
51be6aa
docs: update DIRECTORY.md (#2754)
github-actions[bot] Oct 4, 2024
0ecb6bd
docs: reword binary search (#2752)
jiya10208 Oct 5, 2024
60093d7
feat: add duval's algorithm (#2725)
aminegh20 Oct 5, 2024
f9fb58f
docs: update DIRECTORY.md (#2771)
github-actions[bot] Oct 6, 2024
b957b1d
feat: Add hemi-sphere area algorithm (#2767)
HarshilShah1804 Oct 7, 2024
fddedd8
fix: Adding documentations, tests, and amending algorithm for gcd_of_…
hollowcrust Oct 7, 2024
821d20c
feat: LFU (Least frequently used) cache (#2757)
deDSeC00720 Oct 7, 2024
dd36279
feat: Digit Separation Algorithm added (#2759)
mjk22071998 Oct 7, 2024
faff217
feat: add recursive inserstion sort (#2764)
Fandroid745 Oct 8, 2024
512efd1
fix: Update documentations and tests for fibonacci.cpp (#2793)
ritk20 Oct 9, 2024
34d3568
docs: update DIRECTORY.md (#2804)
github-actions[bot] Oct 10, 2024
5eff2b9
Create Longest_Substring_Without_Repeating_Characters.cpp (#2761)
ashish5kmax Oct 10, 2024
49ee852
docs: update DIRECTORY.md (#2810)
github-actions[bot] Oct 11, 2024
136e6c0
docs, test: Fit bubble_sort into guidelines and enhance bubble sort a…
hollowcrust Oct 11, 2024
c2adc3b
feat: Added Exponential Probability Distribution (#2780)
HarshilShah1804 Oct 12, 2024
e841605
docs: update DIRECTORY.md (#2827)
github-actions[bot] Oct 14, 2024
05d784d
Longest Increasing subsequence using binary search most optimal appr…
namanmodi65 Oct 18, 2024
d438f0f
docs: update DIRECTORY.md (#2857)
github-actions[bot] Oct 19, 2024
37a9811
docs, test: fit modular inverse fermat little theorem to contributing…
hollowcrust Oct 21, 2024
ffef544
feat: LRU (Least recently used) cache - different implementation (#2783)
deDSeC00720 Oct 22, 2024
ecb8a33
feat: Added Binary Addition Algorithm (#2802)
mjk22071998 Oct 24, 2024
db3f9d3
Create Unbounded_knapsack.cpp (#2786)
yeolesanskruti Oct 28, 2024
649a145
docs, test: Fit Sieve of Eratosthenes to contributing guidelines (#2803)
ritk20 Oct 29, 2024
85d62bb
docs, test: Fit Topological Sort algorithm to contributing guidelines…
HarshilShah1804 Nov 1, 2024
5fd6442
docs: updating `DIRECTORY.md` (#2875)
github-actions[bot] Nov 2, 2024
9b1d702
docs: update DIRECTORY.md (#2896)
github-actions[bot] Nov 3, 2024
c6af943
fix: add cstdint header to all files using fixed width integers (#2717)
realstealthninja Nov 4, 2024
0d766b0
feat: update to CXX standard 17 and add CMakeLists file to directorie…
realstealthninja Nov 4, 2024
8b1eab2
doc: Use the doxygen awesome theme (#2807)
realstealthninja Nov 4, 2024
34e7231
docs: update DIRECTORY.md (#2898)
github-actions[bot] Nov 6, 2024
48743d3
feat: add non-preemptive SJF scheduling (#2801)
LakshmiSrikumar Nov 7, 2024
acb9ac2
docs: update DIRECTORY.md (#2899)
github-actions[bot] Nov 17, 2024
769b3c7
Added to z_function test corner cases for empty text and pattern (#2863)
jasonjyu Nov 17, 2024
94202ec
fix: incorrect exception handling in factorial function (#2868)
henriq4 Nov 24, 2024
41653de
chore: use annotations instead of in house linter (#2905)
realstealthninja Nov 24, 2024
06d195f
fix: permissions in github actions (#2906)
realstealthninja Nov 24, 2024
d1bb156
chore: fix permission issues in cpp-linter (#2907)
realstealthninja Nov 24, 2024
c851590
fix: workflow issue in awesome workflow (#2908)
realstealthninja Nov 24, 2024
77b9f39
docs, tests, fix: fit fibonacci_fast to contributing guidelines (#2893)
setbit123 Nov 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rework on Stack Array Data Structure (TheAlgorithms#2683)
* ref: rework on stack array data structure
- The `Stack` class is placed within the `data_structures` namespace
- The destructor is added to the Stack class to ensure memory deallocation
- Comments are added to the member functions to describe their purpose
- The self-test function is provided, which demonstrates the usage of the Stack class

* chore(docs): add `namespace` docstring

* chore: add `std::out_of_range` exception and test cases when stack is empty

* ref: add `full` and `empty` methods

* ref: improve stack array implementation
- Use TOS instead of stack index
- Add tests for overflow and underflow

* fix: remove comparision to true from asserts

* chore: remove `stack.hpp`

* fix: revert

* Update data_structures/stack_using_array.cpp

* docs: add namespace comment

* chore: remove redundant line in docstring of `empty` method

---------

Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com>
  • Loading branch information
sozelfist and realstealthninja authored Sep 1, 2024
commit 54a20b447efbdf56da7f331de7469e86d50f7dd5
215 changes: 166 additions & 49 deletions data_structures/stack_using_array.cpp
Original file line number Diff line number Diff line change
@@ -1,62 +1,179 @@
#include <iostream>
#include <cassert> /// For std::assert
#include <iostream> /// For std::cout
#include <memory> /// For std::unique_ptr
#include <stdexcept> /// For std::out_of_range

int *stack;
int stack_idx = 0, stack_size;
/**
* @namespace
* @brief data_structures
*/
namespace data_structures {
/**
* @brief Class representation of a stack
* @tparam T The type of the elements in the stack
*/
template <typename T>
class Stack {
private:
std::unique_ptr<T[]> stack; ///< Smart pointer to the stack array
int stackSize; ///< Maximum size of the stack
int stackIndex; ///< Index pointing to the top element of the stack

void push(int x) {
if (stack_idx == stack_size) {
std::cout << "\nOverflow";
} else {
stack[stack_idx++] = x;
public:
/**
* @brief Constructs a new Stack object
*
* @param size Maximum size of the stack
*/
Stack(int size) : stackSize(size), stackIndex(-1), stack(new T[size]) {}

/**
* @brief Checks if the stack is full
*
* @return true if the stack is full, false otherwise
*/
bool full() const { return stackIndex == stackSize - 1; }

/**
* @brief Checks if the stack is empty
* @return true if the stack is empty, false otherwise
*/
bool empty() const { return stackIndex == -1; }

/**
* @brief Pushes an element onto the stack
*
* @param element Element to push onto the stack
*/
void push(T element) {
if (full()) {
throw std::out_of_range("Stack overflow");
} else {
stack[++stackIndex] = element;
}
}
}

void pop() {
if (stack_idx == 0) {
std::cout << "\nUnderflow";
} else {
std::cout << "\n" << stack[--stack_idx] << " deleted";
/**
* @brief Pops an element from the stack
*
* @return The popped element
* @throws std::out_of_range if the stack is empty
*/
T pop() {
if (empty()) {
throw std::out_of_range("Stack underflow");
}
return stack[stackIndex--];
}

/**
* @brief Displays all elements in the stack
*/
void show() const {
for (int i = 0; i <= stackIndex; i++) {
std::cout << stack[i] << "\n";
}
}
}

void show() {
for (int i = 0; i < stack_idx; i++) {
std::cout << stack[i] << "\n";
/**
* @brief Displays the topmost element of the stack
*
* @return The topmost element of the stack
* @throws std::out_of_range if the stack is empty
*/
T topmost() const {
if (empty()) {
throw std::out_of_range("Stack underflow");
}
return stack[stackIndex];
}
}

void topmost() { std::cout << "\nTopmost element: " << stack[stack_idx - 1]; }
void bottom() { std::cout << "\nBottom element: " << stack[0]; } // If we need access to first element without using pop command
int main() {
std::cout << "\nEnter stack_size of stack : ";
std::cin >> stack_size;
stack = new int[stack_size];
int ch, x;
do {
std::cout << "\n0. Exit";
std::cout << "\n1. Push";
std::cout << "\n2. Pop";
std::cout << "\n3. Print";
std::cout << "\n4. Print topmost element:";
std::cout << "\n5. Print Bottom element:";
std::cout << "\nEnter Your Choice : ";
std::cin >> ch;
if (ch == 1) {
std::cout << "\nInsert : ";
std::cin >> x;
push(x);
} else if (ch == 2) {
pop();
} else if (ch == 3) {
show();
} else if (ch == 4) {
topmost();
} else if(ch == 5) {
bottom();
/**
* @brief Displays the bottom element of the stack
*
* @return The bottom element of the stack
* @throws std::out_of_range if the stack is empty
*/
T bottom() const {
if (empty()) {
throw std::out_of_range("Stack underflow");
}
} while (ch != 0);
return stack[0];
}
};
} // namespace data_structures

/**
* @brief Self-test implementations
* @returns void
*/
static void test() {
data_structures::Stack<int> stack(5);

// Test empty and full operations
assert(stack.empty());
assert(!stack.full());

delete[] stack;
// Test pushing elements and checking topmost
stack.push(10);
assert(stack.topmost() == 10);

stack.push(20);
assert(stack.topmost() == 20);

stack.push(30);
stack.push(40);
stack.push(50);
assert(stack.full());

// Test stack overflow
try {
stack.push(60);
} catch (const std::out_of_range& e) {
assert(std::string(e.what()) == "Stack overflow");
}

// Test popping elements
assert(stack.pop() == 50);
assert(stack.pop() == 40);
assert(stack.pop() == 30);

// Check topmost and bottom elements
assert(stack.topmost() == 20);
assert(stack.bottom() == 10);

assert(stack.pop() == 20);
assert(stack.pop() == 10);

assert(stack.empty());
assert(!stack.full());

// Test stack underflow
try {
stack.pop();
} catch (const std::out_of_range& e) {
assert(std::string(e.what()) == "Stack underflow");
}

try {
stack.topmost();
} catch (const std::out_of_range& e) {
assert(std::string(e.what()) == "Stack underflow");
}

try {
stack.bottom();
} catch (const std::out_of_range& e) {
assert(std::string(e.what()) == "Stack underflow");
}
}

/**
* @brief Main function
* @returns 0 on exit
*/
int main() {
test(); // run self-test implementations
std::cout << "All tests passed!" << std::endl;
return 0;
}