-
Notifications
You must be signed in to change notification settings - Fork 7
Stack - C++ #14
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
base: master
Are you sure you want to change the base?
Stack - C++ #14
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| #include <iostream> | ||
|
|
||
| using namespace std; | ||
|
|
||
| template <typename T> | ||
| class stack { | ||
| public: | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. variabile publice? why? |
||
| T *arr; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tab-uri de 2/4 spatii, te rog
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/arr/array |
||
| int top; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ai nevoie si de top si de size? |
||
| size_t capacity; | ||
| size_t size; | ||
|
|
||
| stack (size_t capacity) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. creeaza doar un constructor simplu fara capacitate; nimeni nu foloseste Stack(123), ci Stack()
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. poti pune capacitatea initiala ca o constanta |
||
| this->capacity = capacity; | ||
| arr = new T[capacity]; | ||
| top = -1; | ||
| size = 0; | ||
| } | ||
|
|
||
| void push(T data) { | ||
| if (size < capacity) { | ||
| arr[++top] = data; | ||
| ++size; | ||
| } else { | ||
| T *arr2 = new T[capacity * 2]; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/arr2/new_array |
||
| for (int i = 0; i < capacity; ++i) { | ||
| arr2[i] = arr[i]; | ||
| } | ||
| arr2[++top] = data; | ||
| ++size; | ||
| capacity *= 2; | ||
|
|
||
| delete []arr; | ||
| arr = arr2; | ||
| } | ||
| } | ||
|
|
||
| void pop() { | ||
| if (is_empty()) { | ||
| cout << "Stack is empty" << endl; | ||
| return; | ||
| } | ||
| --top; | ||
| --size; | ||
| } | ||
|
|
||
| T peek() { | ||
| if (is_empty()) { | ||
| exit(-1); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Poti folosi -std=c++17 si "optional". |
||
| } | ||
| return arr[top]; | ||
| } | ||
|
|
||
| bool is_empty() { | ||
| if (size != 0) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return size == 0; |
||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| ~stack() { | ||
| delete []arr; | ||
| } | ||
| }; | ||
|
|
||
| int main() | ||
| { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. un mic proof of concept aici |
||
| return 0; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/stack/ArrayStack