-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrayList.cpp
257 lines (222 loc) · 5.37 KB
/
arrayList.cpp
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include<iostream>
#define size 10
using namespace std;
class List{
int *list; // will get the list array
int *t; // temporary pointer we will use this later.
int length; // will count the total length
public:
void insertElement(int val);
bool deleteElement(int x, int* p);
// List(int length){this->length = length; list = new list(this->length); };
List();
bool is_empty();
bool is_full();
void printList();
int searchElement(int x);
void insertElementAT(int x, int pos);
void emptyList();
int totalLength(){return length;}
void copyList( List &fromThis );
int* getList(){return list;};
~List(){cout << "Distructor is called " << endl;} // checking our distructor
void sort();
void reverseList();
};
// 1,2,3
void List::printList()
{
int *p = list;
t = list + this->length;
cout << "[ ";
while (p != t)
{
cout << *p << " ";
p++;
}
cout << "]" << endl;
}
int List::searchElement(int x)
{
int *p = list;
t = list + this->length;
while (p!= t)
{
if(x == *p)return *p;
p++;
}
cout << "Value not found" << endl;
return -1; // means value not found
}
void List::insertElementAT(int x, int pos) // position will starts from zero
{
if (pos >= length+1 || pos < 0 || pos == size) //i.e if three length then should be max pos contain 3, 4 is not valid
{
cout << "Invalid position, maybe the list is full or your input is incorrect" << endl;
return;
}
if (pos+1 == length ){*(this->list + pos) = x;return; } // no arrangement needed
// 1 2 3 4 5 , if pos = 1, and x = 23, the list should be 1, 23, 2, 3, 4, 5
int swap;
for(int i = this->length ; i > pos; i--)
{
swap = *(this->list + (i-1));
*(this->list + (i-1)) = *(this->list + i); // sending this deleted value to last position
*(this->list + i) = swap;
}
*(this->list + pos) = x;
cout << "Value inserted successfully " << endl;
this->length++;
}
bool List::is_empty()
{
if(this->length == size)
{
return false;
}
return true;
}
bool List::is_full()
{
if (this->length == size)
{
return true;
}
return false;
}
List::List()
{
cout << "constructor called " << endl;
this->list= new int[size];
t = NULL;
length = 0;
}
//1,2,3
void List::insertElement(int val)
{
if(is_full())
{
cout << "list is full" << endl;
return;
}
t = list + this->length;
*t = val;
length++;
}
bool List::deleteElement(int x, int* p = NULL)
{
if (p == NULL)
{
t = list + this->length; // t will be present at garbage value, i.e after the last
p = list;
}
if (p == t)return false; // means value not found
if(x == *p)
{
length--;
*p = -1;
return true;
}
if(deleteElement(x, p+1) == true) // recursive call to find the element
{
cout << "element " << x << " is deleted succesfully" << endl;
if (p+1 == t) return true; // it is last element no need to arrange list
else
{
p += 1;
while(p != t)
{
*p = *(p + 1); // sending this deleted value to last position
p++;
}
}
}
return false;
}
void List::emptyList()
{
length = 0;
t = list = NULL;
cout<<"The list is now empty" << endl;
}
void List::copyList(List &fromThis )
{
int *cp = fromThis.getList(); // returning whole list
for (int i = 0 ; i < fromThis.totalLength(); i++)
{
this->insertElement(*(cp + i));
}
this->length = fromThis.totalLength();
cout << "\nList copies successfully" << endl;
}
bool compare(List &l1, List &l2)
{
if (l1.totalLength() != l2.totalLength())return false; // if list are not same
int *a1 = l1.getList(); // getting list array
int *a2 = l1.getList();
for (int i = 0 ; i < l1.totalLength(); i++)
{
if (*(a1 + i) != *(a2 + i))return false;
}
return true; // it means lists are same
}
void List::sort()
{
int *l = this->getList();
int swap;
for (int i = 0 ; i < this->totalLength(); i++)
{
for (int j = i; j < this->totalLength(); j++)
{
if (*(l + i) > *(l + j))
{
swap = *(l + j);
*(l + j) = *(l + i);
*(l + i) = swap;
}
}
}
cout << "List sorted successfully" << endl;
}
void List::reverseList()
{
int left= -1;
int swap;
for (int right = this->length-1; right > int(this->length/2)-1; right--) // i dit /2 to handle even and odd length list , swapping last with first and so on
{
left++;
if (this->list + left == this->list + right) // moving right to right side and left to left side
{
break;
}
swap = *(this->list + right);
*(this->list + right) = *(this->list + left);
*(this->list + left) = swap;
}
cout << "list reversed successfully" << endl;
}
// isinstance
int main()
{
List s;
List s2;
s.insertElement(23);
s.insertElement(4);
s.insertElement(7);
s.insertElement(8);
s.printList();
s2.copyList(s); // copy s to s2
s2.printList();
cout << compare(s, s2) << endl; // compare takes 2 list objects
s2.sort();
s2.printList();
s2.insertElementAT(100, 1); // inserting 100 at index 1, index starts from 0
s2.printList();
s2.reverseList();
s2.printList();
s2.deleteElement(7);
s2.printList();
s2.reverseList();
s2.printList();
cout << s.searchElement(7) << endl;
}