-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathgeneric_stack.cpp
More file actions
77 lines (68 loc) · 1.25 KB
/
generic_stack.cpp
File metadata and controls
77 lines (68 loc) · 1.25 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <cstdio>
#include <iostream>
using namespace std;
template <class T>
class Stack{
private:
int _size;
int _head;
T* _data;
public:
Stack(int size){
_size = size;
_head = -1;
_data = new T[size];
}
void push(T data){
if (_head == _size - 1)
{
cout << "Stack full" << endl;
return;
}
_data[++_head] = data;
}
T& head(){
if (_head == -1)
throw exception();
return _data[_head];
}
T& pop(){
if (_head == -1)
throw exception();
_head--;
return _data[_head];
}
bool empty(){
return _head == -1;
}
void print(){
for (int i = 0; i <= _head; i++){
cout << &_data[i] << " ";
}
cout << endl;
}
};
class Data{
public:
int x;
double y;
Data(){
x = 0;
y = 0;
}
Data(int x_value, double y_value): x(x_value), y(y_value){}
Data(const Data& obj){
x = obj.x;
y = obj.y;
}
};
int main(){
Stack<Data*> s(10);
s.push(new Data(1, 2.0));
s.push(new Data(2, 19.0));
Data* d = s.pop();
delete d;
s.push(new Data(10, 20.0));
s.print();
return 0;
}