-
Notifications
You must be signed in to change notification settings - Fork 0
/
stackarray.c
56 lines (51 loc) · 870 Bytes
/
stackarray.c
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
#include <iostream>
#include <cstdlib>
using namespace std;
int* myArr = new int[2];
int sizex = 2;
int tepe=0;
int pop() {
if (tepe == 0) {
cout << "stack underflow ! " << endl;
exit(1);
}
if (tepe == sizex / 4) {
int* temp = new int[sizex / 2];
for (int i = 0; i <tepe; i++) {
temp[i] = myArr[i];
}
delete[] myArr;
myArr = temp;
}
tepe = tepe - 1;
cout << "Last added index has been deleted " << endl;
return myArr[tepe];
}
void push(int a) {
if(tepe==sizex) {
int* temp = new int[sizex * 2];
for (int i = 0; i < sizex; i++) {
temp[i] = myArr[i];
}
delete[] myArr;
myArr = temp;
sizex*=2;
}
myArr[tepe++] = a;
}
void print() {
for (int i = 0; i < tepe; i++) {
cout <<sizex<<". index"<< myArr[i] << endl;
}
}
int main() {
push(12);
push(21);
push(42);
push(59);
push(74);
print();
pop();
pop();
print();
}