Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- template<class T,int Maxsize=10>
- class stack{
- public:
- void init(){ sp = 0; };
- void push(T);
- void pop();
- private:
- int sp;
- T buffer[Maxsize];
- static void Error(){ cout << "\n Stack Error\n" <<endl ; }
- };
- template<class T,int Maxsize=10>
- void stack<T, Maxsize>::push(T data)
- {
- if (sp == Maxsize)
- Error();
- else
- buffer[sp++] = data;
- }
- template<class T,int Maxsize=10>
- void stack<T, Maxsize>::pop()
- {
- if (sp == 0)
- Error();
- else
- cout << buffer[--sp] << endl;
- }
- void main()
- {
- stack<int> st1;
- stack<char> st2;
- st1.init();
- st1.push(1);
- st1.push(2);
- st1.push(3);
- st1.push(4);
- st1.push(5);
- st1.push(6);
- st1.push(7);
- st1.push(8);
- st1.push(9);
- st1.push(10);
- st1.pop();
- st1.pop();
- st1.pop();
- st1.pop();
- st1.pop();
- st1.pop();
- st1.pop();
- st1.pop();
- st1.pop();
- st1.pop();
- st1.pop();
- st2.init();
- st2.push('a');
- st2.push('b');
- st2.push('c');
- st2.push('d');
- st2.push('e');
- st2.push('f');
- st2.push('g');
- st2.push('h');
- st2.push('i');
- st2.push('j');
- st2.pop();
- st2.pop();
- st2.pop();
- st2.pop();
- st2.pop();
- st2.pop();
- st2.pop();
- st2.pop();
- st2.pop();
- st2.pop();
- st2.pop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement