From 0fd803588a8de25e5eb80de9793272af09d33afb Mon Sep 17 00:00:00 2001 From: Kaitou345 <68972628+Kaitou345@users.noreply.github.com> Date: Mon, 30 Aug 2021 06:06:23 +0600 Subject: [PATCH] Templated StackV --- StackV.h | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/StackV.h b/StackV.h index 765dade..b54f90f 100644 --- a/StackV.h +++ b/StackV.h @@ -1,3 +1,4 @@ + //Implementing a Stack ADT with a vector #ifndef STACK_H @@ -6,11 +7,12 @@ #include using namespace std; +template class Stack { private: - vector data; + vector data; public: @@ -22,15 +24,26 @@ class Stack // No d'tor needs to be declared // The vector "knows" how to destroy itself - int size(); - - void push(int); - - void pop(); - - int top(); - - void clear(); + int size() const { return data.size(); } + + void push(const T& data) + { + this->data.push_back(data); + } + void pop() + { + this->data.pop_back(); + } + + // returns the data at the top of the stack + int top() const + { + return this->data.at(data.size() - 1); + } + void clear() + { + this->data.clear(); + } };