-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackArrResize.cpp
More file actions
61 lines (50 loc) · 1.04 KB
/
stackArrResize.cpp
File metadata and controls
61 lines (50 loc) · 1.04 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
#include<bits/stdc++.h>
using namespace std;
/***************************************ORIGINAL aUTHOR*******************************
*************************************************************************************
*************************************************************************************
*************astrainL3gi0N**********************************************************/
class stackarr
{
int N;
int *S;
int top = -1;
public:
stackarr(int n){
this->N = n;
S = new int[n];
}
void push(int item){
if(top >= N-1) resize(2*N);
S[++top] = item;
}
int pop(){
if(top == -1){
cout<<"underflow\n";
return -1;
}
if(top < N/2) resize(N/2);
return S[top--];
}
void resize(int l){
int *arr = new int [N];
for(int i = 0; i < N; ++i)
arr[i] = S[i];
S = new int[l];
for(int i = 0; i < l; ++i)
S[i] = arr[i];
this->N = l;
}
};
int main()
{
int n; cin>>n;
stackarr st(n);
for(int i = 0; i <= n; ++i){
int k; cin>>k;
st.push(k);
}
for(int i = 0; i <= n; ++i)
cout<<st.pop()<<' ';
return 0;
}