-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharraylist.cpp
More file actions
117 lines (113 loc) · 1.75 KB
/
arraylist.cpp
File metadata and controls
117 lines (113 loc) · 1.75 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
#include <iostream>
using namespace std;
class arraylist {
private:
int *arr;
int maxsize;
int length;
public:
arraylist(int s=10) {
if (s < 0) {
maxsize = 10;
}
else {
maxsize = s;
length = 0;
arr = new int[maxsize];
}
}
bool isempthy() {
return length == 0;
}
bool isfull() {
return length == maxsize;
}
int getsize() {
return length;
}
void print() {
for (size_t i=0; i < length; i++) {
cout << arr[i] << " ";
}
}
void add(int pos, int element) {
if (isfull()) {
cout << "array is full";
}
else if (pos<0 || pos>length) {
cout << "out of range";
}
else {
for (int i = length; i > pos; i--) {
arr[i] = arr[i - 1];
}
arr[pos] = element;
length++;
}
}
void remove(int pos) {
if (isempthy()) {
cout << "array is empthy";
}
else if (pos<0 || pos>length) {
cout << "out of range";
}
else {
for (int i = pos; i < length; i++) {
arr[i] = arr[i + 1];
}
length--;
}
}
void insertatend(int element) {
if (isfull()) {
cout << "isfull";
}
else {
arr[length] = element;
length++;
}
}
int search(int element) {
for (size_t i = 0; i < length; i++) {
if (arr[i] == element)
return i;
return -1;
}
}
void insertnodublicate(int element) {
if (search(element) == -1) {
insertatend(element);
}
else {
cout << "element is there";
}
}
void update(int pos, int element) {
if (pos > length || pos < 0) {
cout << "out of range";
}
else {
arr[pos] = element;
}
}
int getelement(int pos) {
if (pos > length || pos < 0) {
cout << "out of range";
}
else {
return arr[pos];
}
}
~arraylist() {
delete[]arr;
}
};
int main()
{
arraylist ar(100);
ar.add(0, 10);
ar.add(1, 20);
ar.insertatend(30);
ar.print();
}