-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.h
44 lines (41 loc) · 1.02 KB
/
stack.h
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
#pragma once
template <class type> class stack
{
private:
type * mem;
int size;
int last;
public:
stack(int _size = 0) {
size = _size;
last = 0;
mem = new type[size];
}
stack(stack<type> const & tmp) {
size = tmp.last;
last = tmp.last;
mem = new type[size];
for (size_t i = 0; i < last; i++)
mem[i] = tmp.mem[i];
}
stack<type> & operator= (stack<type> const & tmp)
{
if (size != tmp.size) {
delete[] mem;
size = tmp.size;
mem = new type[size];
}
last = tmp.last;
for (size_t i = 0; i < last; i++)
mem[i] = tmp.mem[i];
return *this;
}
void Push(type tmp) { if (last < size) mem[last++] = tmp; }//äîáàâèòü ýëåìåíò
type Pop() { if (last != 0) return mem[--last]; return 0; }//âçÿòü ñâåðõó
type Top() { if (last != 0) return mem[last - 1]; return 0; }//ïîñìîòðåòü ïîñëåäíèé ýëåìåíò
bool is_empty() { return(last == 0); }//ïðîâåðêà ïóñòîòû
bool is_full() { return(last == size); }//ïðîâåðêà ïîëíîòû
int getlast() { return last; }
int getsize() { return size; }
~stack() { delete[] mem; }
};