-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathstack.cpp
More file actions
57 lines (50 loc) · 943 Bytes
/
stack.cpp
File metadata and controls
57 lines (50 loc) · 943 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
51
52
53
54
55
56
57
#include <cstdio>
#include <iostream>
using namespace std;
class Stack{
private:
int _size;
int _head;
int* _data;
public:
Stack(int size){
_size = size;
_head = -1;
_data = new int[size];
}
void push(int data){
if (_head == _size - 1)
{
cout << "Stack full" << endl;
return;
}
_data[++_head] = data;
}
int& head(){
if (_head == -1)
throw exception();
return _data[_head];
}
int& pop(){
if (_head == -1)
throw exception();
return _data[_head--];
}
void print(){
for (int i = 0; i <= _head; i++){
cout << _data[i] << " ";
}
cout << endl;
}
};
int main(){
Stack s(10);
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
cout << s.head() << endl;
s.print();
return 0;
}