-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.cpp
More file actions
50 lines (43 loc) · 930 Bytes
/
Stack.cpp
File metadata and controls
50 lines (43 loc) · 930 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#pragma once
#include <iostream>
using namespace std;
template <class T, int max_size > class Stack {
T s[max_size];
int top;
public:
Stack() { top = 0; }
void reset() { top = 0; }
void push(T i);
T pop();
bool is_full() { return top == max_size; }
public :
bool empty() { return top <= 0; }
void clear() { top = 0; }
void print();
void reverse();
};
template <class T, int max_size >
void Stack <T, max_size >::push(T i) {
if ( !is_full() ) {
s[top] = i;
++top;
}
else
throw "Stack_is_full";
}
template <class T, int max_size >
T Stack <T, max_size >::pop() {
if ( !empty() ) {
--top;
return s[top];
}
else
throw "Stack_is_empty";
}
template <class T, int max_size >
void Stack<T, max_size >::print() {
cout << "Stack : " << endl;
for(int i = 0; i < top; i++) {
cout << s[i];
}
}