-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstack.h
More file actions
64 lines (59 loc) · 915 Bytes
/
stack.h
File metadata and controls
64 lines (59 loc) · 915 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
58
59
60
61
62
63
64
#include <string.h>
#include <assert.h>
#include <iostream>
template<class T>
class stack
{
public:
stack(int defalut = 8): sequence(new T[defalut]), top(0), capacity(defalut)
{}
~stack()
{
delete[] sequence;
}
void push(const T value)
{
if (top == capacity) //stack is full,allocate larger capacity
{
capacity <<= 1;
T* temp = new T[capacity];
memcpy(temp, sequence, sizeof(T)*top);
delete[] sequence;
sequence = temp;
}
sequence[top++] = value;
}
T pop()
{
assert(top != 0);
return sequence[top--];
}
T operator[](int n)
{
return *(this->sequence + n);
}
int size() const
{
return top;
}
private:
T* sequence;
int top;
int capacity;
};
/*
for test
*/
// int main(int argc, char const *argv[])
// {
// stack<int> s;
// int i=0;
// while(s.size()<100)
// {
// s.push(i);
// cout<<s[i++];
// }
// while(s.size()>0)
// s.pop();
// return 0;
// }