-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_stack.cpp
More file actions
100 lines (89 loc) · 1.15 KB
/
Copy patharray_stack.cpp
File metadata and controls
100 lines (89 loc) · 1.15 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
using namespace std;
template<class T>
class stack
{
public:
stack();
~stack();
T top();
void pop();
bool empty();
void push(T data);
void print();
private:
int capacity;
int size;
T* items;
};
template<class T>
stack<T>::stack()
{
capacity=4;
items=new T[capacity];
size=0;
}
template <class T>
stack<T>::~stack()
{
delete[] items;
}
template <class T>
void stack<T>::push(T data)
{
if(size>=capacity)
{
T* new_area=new T[2*capacity];
for(int i=0;i<size;++i)
new_area[i]=items[i];
delete[] items;
items=new_area;
}
items[size]=data;
size++;
}
template <class T>
T stack<T>::top()
{
if(size>0)
return items[size-1];
else
{
cout<<"empty stack";
return -1;
}
}
template <class T>
void stack<T>::pop()
{
if(size>0)
size--;
}
template <class T>
void stack<T>::print()
{
for(int i=0;i<size;++i)
{
cout<<items[i]<<" ";
}
cout<<endl;
}
template <class T>
bool stack<T>::empty()
{
return size>0;
}
int main()
{
stack<int> si;
si.push(4);
si.push(5);
si.push(6);
si.push(7);
si.print();
si.push(8);
si.print();
si.pop();
si.print();
return 0;
}