-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Rework on Stack Array Data Structure #2683
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
Merged
+166
−49
Merged
Changes from 4 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
759d532
ref: rework on stack array data structure
sozelfist 940ac14
chore(docs): add `namespace` docstring
sozelfist e741146
chore: add `std::out_of_range` exception and test cases when stack is…
sozelfist 19fc4b4
ref: add `full` and `empty` methods
sozelfist 9c55a9a
ref: improve stack array implementation
sozelfist ee40cbc
Merge branch 'master' into ds/stack
sozelfist 2e79385
fix: remove comparision to true from asserts
realstealthninja adf14bb
chore: remove `stack.hpp`
sozelfist a027a13
fix: revert
realstealthninja 1b8b60f
Update data_structures/stack_using_array.cpp
realstealthninja 548085b
docs: add namespace comment
realstealthninja 7c442b8
Merge branch 'master' into ds/stack
sozelfist 7ae10a6
chore: remove redundant line in docstring of `empty` method
sozelfist b0210e1
Merge branch 'master' into ds/stack
realstealthninja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
@@ -1,62 +1,178 @@ | ||
#include <iostream> | ||
/** | ||
* @file | ||
* @brief Implementation of a stack data structure | ||
* @details | ||
* This implementation provides functionalities to push, pop, and view elements | ||
* of the stack. It also includes a self-test method to ensure proper | ||
* functionality. | ||
*/ | ||
|
||
int *stack; | ||
int stack_idx = 0, stack_size; | ||
#include <cassert> /// For assert | ||
#include <iostream> /// For IO operations | ||
#include <stdexcept> /// For std::out_of_range | ||
|
||
void push(int x) { | ||
if (stack_idx == stack_size) { | ||
std::cout << "\nOverflow"; | ||
} else { | ||
stack[stack_idx++] = x; | ||
/* | ||
* @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: | ||
T* stack; ///< Pointer to the stack array | ||
int stackSize; ///< Maximum size of the stack | ||
int stackIndex; ///< Index pointing to the top element of the stack | ||
|
||
public: | ||
/** | ||
* @brief Constructs a new Stack object | ||
* | ||
* @param size Maximum size of the stack | ||
*/ | ||
Stack(int size) : stackSize(size), stackIndex(0) { stack = new T[size]; } | ||
sozelfist marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
/** | ||
* @brief Destroys the Stack object | ||
*/ | ||
~Stack() { delete[] stack; } | ||
|
||
/** | ||
* @brief Checks if the stack is full | ||
* | ||
* @return true if the stack is full, false otherwise | ||
*/ | ||
bool full() const { return stackIndex == stackSize; } | ||
|
||
/** | ||
* @brief Checks if the stack is empty | ||
* | ||
sozelfist marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
* @return true if the stack is empty, false otherwise | ||
*/ | ||
bool empty() const { return stackIndex == 0; } | ||
sozelfist marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
/** | ||
* @brief Pushes an element onto the stack | ||
* | ||
* @param element Element to push onto the stack | ||
* @return true if the element was successfully pushed onto the stack, false | ||
* otherwise | ||
*/ | ||
bool push(T element) { | ||
if (full()) { | ||
return false; | ||
} else { | ||
stack[stackIndex++] = element; | ||
return true; | ||
} | ||
sozelfist marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
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 is empty"); | ||
sozelfist marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} else { | ||
return stack[--stackIndex]; | ||
} | ||
} | ||
} | ||
|
||
void show() { | ||
for (int i = 0; i < stack_idx; i++) { | ||
std::cout << stack[i] << "\n"; | ||
/** | ||
* @brief Displays all elements in the stack | ||
*/ | ||
void show() { | ||
for (int i = 0; i < stackIndex; i++) { | ||
std::cout << stack[i] << "\n"; | ||
} | ||
} | ||
} | ||
|
||
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 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 is empty"); | ||
} else { | ||
return stack[stackIndex - 1]; | ||
} | ||
} | ||
|
||
/** | ||
* @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 is empty"); | ||
} else { | ||
return stack[0]; | ||
} | ||
} while (ch != 0); | ||
} | ||
}; | ||
} // namespace data_structures | ||
|
||
/** | ||
* @brief Self-test implementations | ||
* @returns void | ||
*/ | ||
static void test() { | ||
data_structures::Stack<int> stack(5); | ||
|
||
// Test push, pop, topmost, bottom, full, and empty operations | ||
assert(stack.empty() == true); | ||
realstealthninja marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
assert(stack.full() == false); | ||
realstealthninja marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
assert(stack.push(10) == true); | ||
sozelfist marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
assert(stack.push(20) == true); | ||
assert(stack.push(30) == true); | ||
assert(stack.push(40) == true); | ||
assert(stack.push(50) == true); | ||
assert(stack.push(60) == false); | ||
|
||
sozelfist marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert(stack.pop() == 50); | ||
assert(stack.pop() == 40); | ||
assert(stack.pop() == 30); | ||
|
||
assert(stack.topmost() == 20); | ||
assert(stack.bottom() == 10); | ||
|
||
delete[] stack; | ||
assert(stack.pop() == 20); | ||
assert(stack.pop() == 10); | ||
|
||
assert(stack.empty() == true); | ||
realstealthninja marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
assert(stack.full() == false); | ||
realstealthninja marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
// Test for exceptions when stack is empty | ||
try { | ||
stack.topmost(); | ||
} catch (const std::out_of_range& e) { | ||
assert(std::string(e.what()) == "Stack is empty"); | ||
} | ||
|
||
try { | ||
stack.bottom(); | ||
} catch (const std::out_of_range& e) { | ||
assert(std::string(e.what()) == "Stack is empty"); | ||
} | ||
} | ||
|
||
/** | ||
* @brief Main function | ||
* @returns 0 on exit | ||
*/ | ||
int main() { | ||
test(); // run self-test implementations | ||
return 0; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.