Last active
December 29, 2015 12:39
-
-
Save yashihei/7671831 to your computer and use it in GitHub Desktop.
This file contains 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
#include <iostream> | |
using namespace std; | |
template <class T> | |
class Stack { | |
public: | |
static const int STACK_SIZE = 100; | |
Stack() {sp = 0;} | |
void push(const T&); | |
T pop(); | |
private: | |
T data[STACK_SIZE]; | |
int sp; | |
}; | |
template<class T> void Stack<T>::push(const T& data) { | |
if (sp >= STACK_SIZE) { | |
cout << "スタック満杯" << endl; | |
return; | |
} | |
this->data[sp++] = data; | |
} | |
template<class T> T Stack<T>::pop() { | |
if (sp <= 0) { | |
cout << "スタック空だよ" << endl; | |
return 0; | |
} | |
T data = this->data[--sp]; | |
return data; | |
} | |
int main() { | |
Stack<int> s; | |
s.push(10); | |
s.pop(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment