Skip to content

Instantly share code, notes, and snippets.

@yashihei
Last active December 29, 2015 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yashihei/7671831 to your computer and use it in GitHub Desktop.
Save yashihei/7671831 to your computer and use it in GitHub Desktop.
#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