From d594d1ec10587897f662727eeafc0f15cd7e2e33 Mon Sep 17 00:00:00 2001 From: Andrei Dinu Date: Sun, 30 Sep 2018 22:20:43 +0300 Subject: [PATCH 1/2] Add files via upload --- CPP/data-structures/stack.cpp | 71 +++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 CPP/data-structures/stack.cpp diff --git a/CPP/data-structures/stack.cpp b/CPP/data-structures/stack.cpp new file mode 100644 index 0000000..e1d47f3 --- /dev/null +++ b/CPP/data-structures/stack.cpp @@ -0,0 +1,71 @@ +#include + +using namespace std; + +template +class stack { +public: + T *arr; + int top; + size_t capacity; + size_t size; + + stack (size_t capacity) { + 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]; + 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); + } + return arr[top]; + } + + bool is_empty() { + if (size != 0) { + return false; + } + return true; + } + + ~stack() { + delete [] arr; + } +}; + +int main() +{ + + return 0; +} \ No newline at end of file From f248f2b66c9e8a4d6d1d81146f2b1b6fcf2bfb8b Mon Sep 17 00:00:00 2001 From: Andrei Dinu Date: Sun, 30 Sep 2018 22:21:41 +0300 Subject: [PATCH 2/2] Update stack.cpp --- CPP/data-structures/stack.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/CPP/data-structures/stack.cpp b/CPP/data-structures/stack.cpp index e1d47f3..7baade9 100644 --- a/CPP/data-structures/stack.cpp +++ b/CPP/data-structures/stack.cpp @@ -33,7 +33,6 @@ class stack { delete []arr; arr = arr2; } - } void pop() { @@ -60,12 +59,11 @@ class stack { } ~stack() { - delete [] arr; + delete []arr; } }; int main() { - return 0; -} \ No newline at end of file +}