-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
148 lines (110 loc) · 2.48 KB
/
main.cpp
File metadata and controls
148 lines (110 loc) · 2.48 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
#include "intArray_t.h"
using namespace std;
int main(int argsc, int** argv) {
intArray_t arr, tmp;
//Note: tmp array will be used to avoid memory leaks
while (1) {
cout << "\n| n | fst | lst | ins | rmv | RmvAll | delete | DeleteAll |A|P| < | F | : ";
char c;
int *e;
int i;
cin >> c;
if (c != 'n' && c != 'f' && c != 'l' && c != 'e' && c != 'i' && c
!= 'r' && c != 'R' && c != '<' && c != 'A' && c != 'P' && c != 'F' && c != 'd' && c != 'D')
break;
switch (c) {
case 'n':
cout << arr.getSize() << endl;
break;
case 'f':
e = arr.getFirst();
if (e != 0)
cout << *e << endl;
else
cout << "Array Empty!";
break;
case 'l':
e = arr.getLast();
if (e != 0)
cout << *e << endl;
else
cout << "Array Empty!";
break;
case 'd': //Remove and delete
cout << "Input value of elements to delete :";
cin >> i;
//Remove all elemetes from tmp array with value i
e = tmp.remove(i);
while ( e != 0){
e = tmp.remove(i);
}
arr.removeAndDelete(i);
break;
case 'D': //remove and delete all
cout << "Deleting whole array";
arr.removeAndDeleteAll();
tmp.removeAll(); //Remove all elements from tmp (without deleting them)
break;
case 'i':
e = new int;
cout << "Input element value :";
cin >> *e;
arr.insert(e);
tmp.insert(e);
break;
case 'r': // remove
cout << "Input element value to remove :";
cin >> i;
e = arr.remove(i) ;
if (e == 0){
cout << "Element not found!";
}
else
cout << *e << " element removed!";
delete e;
tmp.remove(i); //Remove elements from tmp array as well
break;
case 'R': // remove all
cout << "Removing all array";
arr.removeAll();
tmp.removeAndDeleteAll(); //Free memory used to avoid leaks
break;
case 'A': // append
e = new int;
cout << "Tnput element value :";
cin >> *e;
cout << "Input index: ";
cin >> i;
arr.append(i, e);
tmp.append(i, e);
break;
case 'P': // prepend
e = new int;
cout << "Input element value :";
cin >> *e;
cout << "Input index: ";
cin >> i;
arr.prepend(i, e);
tmp.prepend(i, e);
break;
case 'F': //find
cout << "Input element to find: ";
cin >> i;
e = arr.find(i);
if (e != 0)
cout << *e;
else
cout << "Element not found\n";
break;
case '<': // print array
cout << arr;
cout << tmp;
break;
}
}
//Free all memory used
tmp.removeAndDeleteAll();
arr.removeAll();
return 0;
}