-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment1_answer.cpp
More file actions
331 lines (328 loc) · 11 KB
/
assignment1_answer.cpp
File metadata and controls
331 lines (328 loc) · 11 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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include <iostream>
#include <string>
#include<string.h>
using namespace std;
//-------------------------------------------------------------------(SLL)----------------------------------------------------------------------
// class node
class Node{
public:
Node* next;
string data;
virtual void TSortedList(){}
virtual void insertNewNode(string dataOfNode){}
};
Node* head;
//-------------------------
class SortSinglyList : public Node{
void TSortedList(){
string temp;
Node *current;
for(int i=0;i<4;i++){
current = head;
for (int j= 1; j<4; j++){
if ((current->data) < ((current->next)-> data)){
temp = current->data;
current->data = (current->next)-> data;
(current->next)-> data = temp;
}
current = current-> next;
if (current == NULL){break;}
}
}
current = head;
for (int k = 0; k < 4;k++){
cout << current->data << "\t";
current = current->next;
}
}
//-------------------------
void insertNewNode(string dataOfNode){
Node* new_node = new Node();
new_node->next = NULL;
new_node->data = dataOfNode;
if (head == NULL){
head = new_node;
return;
}
Node *current = head;
while (current-> next != NULL){
current = current-> next;
}
current-> next = new_node;
}
};
//-------------------------------------------------------------------(DLL)----------------------------------------------------------------------
class node{
public:
node* next;
node* prev;
int data;
};
class SNode{
public:
SNode* next;
int data;
};
node* head1;
//-------------------------------------------------
class DuplicateManipuationList : public node{
public:
void insertNewNode(int dataOfNode){
node* new_node = new node();
new_node->next = NULL;
new_node->prev = NULL;
new_node->data = dataOfNode;
if (head1 == NULL){
head1 = new_node;
return;
}
node *current = head1;
while (current-> next != NULL){
current = current-> next;
}
new_node->prev = current;
current-> next = new_node;
}
//-------------------------------------------------
int removeDuplicates(SNode* temp){
node* current = head1;
int x = 0;
while(current != NULL){
if (head1->data == temp->data){
head1 = current->next;
current->next->prev = NULL;
current->next = NULL;
current = head1;
x++;
continue;
}
while(current->data != temp->data){
if (current->next == NULL && current->data != temp->data){
return x;
}
current = current->next;
}
if (current->next == NULL && current->data == temp->data){
(current->prev)->next = NULL;
current->prev = NULL;
x++;
return x;
}
(current->prev)->next = current->next;
(current->next)->prev = current->prev;
current->prev = NULL;
node *del = current;
current = current->next;
del->next = NULL;
x++;
delete del;
}
return x;
}
};
//------------------------------------------------------------------(ARRAY)---------------------------------------------------------------------
// Function that insert an element in a full array .
void fullArrayInsert(int *fullArray, int *newArray){
for (int i = 0; i< 5;i++){
*(newArray+i) = *(fullArray+i);
}
cout << "Write an element you want to insert : ";
cin >> *(newArray+5);
}
//-------------------------
// Function that insert an element in a non full array .
void notFullarrayInsert(int *notFullArray){
cout << "Write an element you want to insert : ";
cin >> *(notFullArray+5);
}
//-------------------------
// Function that sort the array in descending order .
void arraySort(int *array){
for (int i = 1; i <6; i++){
int temp = array[i];
int j = i - 1;
while(j>=0 && temp > array[j]){
array[i] = array[j];
j--;
}
array[j+1] = temp;
}
// Another algorithm :
/**
int temp ;
for(int i =1;i<8;i++){
for (int j = i-1 ; j>=0;j--){
temp = *(array+j+1);
if (temp > *(array+j)){
*(array+j+1) = *(array+j);
*(array+j) = temp;
}
}
}**/
}
//-------------------------
// Function that insert an element in its right position in a non sorted array .
void InsertionSort(int *array){
int newElement;
cout <<"Write a new an element to insert it in the sorted array : ";
cin >> newElement;
for (int i = 1 ;i<7;i++){
if (newElement > *array){
for(int j = 6;j>0;j--){
array[j] = array[j-1];
}
*array = newElement;
break;
}
if (newElement < *(array+5)){
*(array+6) = newElement;
break;
}
else if ((*(array)) >= newElement && *(array+i) <= newElement ) {
for (int j = 6; j > i ; j--){
array [j] = array [j-1];
}
*(array+i) = newElement;
break;
}
}
}
//-------------------------
void deleteElement(int* array){
int order;
cout << "Write element order you want to delete (1 => First , 2 => Second , . . . ): " ;
cin >> order;
for (int i=0;i<7;i++){
if (i == (order-1)){
for(int j = i; j<7;j++){
array[j] = array[j+1];
}
break;
}
}
cout <<endl<<endl;
cout << "--------------------------------------------------------------------------"<<endl;
cout << "Array After deleting is : ";
for (int i = 0; i<6;i++){
cout<<*(array+i)<<"\t";
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------(SLL)---------------------------------------------------------------------
/*
int main(){
Node* bptr;
SortSinglyList d;
bptr = &d;
bptr->insertNewNode("orange");
bptr->insertNewNode("apple");
bptr->insertNewNode("banana");
bptr->insertNewNode("zebra");
bptr->TSortedList();
return 0;
}
*/
//-------------------------------------------------------------------(DLL)----------------------------------------------------------------------
/*
int main() {
node bptr;
DuplicateManipuationList dptr;
dptr.insertNewNode(7);
dptr.insertNewNode(15);
dptr.insertNewNode(3);
dptr.insertNewNode(2);
dptr.insertNewNode(1);
dptr.insertNewNode(3);
dptr.insertNewNode(7);
dptr.insertNewNode(15);
dptr.insertNewNode(15);
//--------------------------
SNode* new_node1 = new SNode();
new_node1->data = 7;
SNode* new_node2 = new SNode();
new_node2->next = NULL;
new_node2->data = 15;
new_node1->next = new_node2;
new_node2 = NULL;
//--------------------------
new_node1->data = dptr.removeDuplicates(new_node1);
(new_node1->next)->data = dptr.removeDuplicates(new_node1->next);
cout << "Times that 7 appears is : " << new_node1->data << "\n";
cout << "Times that 15 appears is : " <<(new_node1->next)-> data << "\n";
node* current = head1;
cout << endl<<"List after remove duplicate is : " << current->data << " , " << current->next->data << " , " << current->next->next->data << "\t";
return 0;
}
*/
//------------------------------------------------------------------(ARRAY)---------------------------------------------------------------------
int main()
{
int choice;
int arrayFull[5] = {80,200,40,1000,930}; // Full array with size 5 .
int arrayNotFull[10] = {80,200,40,1000,930}; // Not full array (Size = 10 , elements = 5)
int arrayNew[15]={}; // We will allocate this larger array and copy elements from the old (Full) array to this new one .
cout << "----------------------------- You have 2 arrays with elements {80,200,40,1000,930} in the order -----------------------"<<endl<<endl;
cout << "1 => Full Array with size 5" <<"\t"<<", 2 => Not Full Array with size 10" <<endl;
cout <<"Do you want an element to add in a full array or not a full array ? Write (1 or 2) ";
cin >> choice;
switch (choice){
case 1:
cout <<endl<<endl;
cout << "--------------------------------------------------------------------------"<<endl;
cout << "Array before sorting is : ";
for(int i = 0; i< 5; i++){
cout << arrayFull[i] << "\t";
}
cout <<endl;
fullArrayInsert(arrayFull, arrayNew); // Insert element in a full array .
arraySort(arrayNew); // Sort the array in descending order .
cout <<endl<<endl<<endl;
cout << "--------------------------------------------------------------------------"<<endl;
cout << "Array After sorting is : ";
for(int i = 0; i< 6; i++){
cout << arrayNew[i] << "\t";
}
cout <<endl<<endl;
InsertionSort(arrayNew);
cout <<endl<<endl;
cout << "--------------------------------------------------------------------------"<<endl;
cout << "Array After sorting is : ";
for(int i = 0; i< 7; i++){
cout << arrayNew[i] << "\t";
}
cout <<endl<<endl;
cout << "--------------------------------------------------------------------------"<<endl;
deleteElement(arrayNew);
break;
case 2:
cout <<endl<<endl;
cout << "--------------------------------------------------------------------------"<<endl;
cout << "Array before sorting is : ";
for(int i = 0; i< 5; i++){
cout << arrayNotFull[i] << "\t";
}
cout <<endl<<endl;
notFullarrayInsert(arrayNotFull); // Insert element in a not full array .
arraySort(arrayNotFull); // Sort the array in descending order .
cout <<endl<<endl;
cout << "--------------------------------------------------------------------------"<<endl;
cout << "Array After sorting is : ";
for(int i = 0; i< 6; i++){
cout << arrayNotFull[i] << "\t";
}
cout <<endl<<endl;
InsertionSort(arrayNotFull);
cout <<endl<<endl;
cout << "--------------------------------------------------------------------------"<<endl;
cout << "Array After sorting is : ";
for(int i = 0; i< 7; i++){
cout << arrayNotFull[i] << "\t";
}
cout <<endl<<endl;
cout << "--------------------------------------------------------------------------"<<endl;
deleteElement(arrayNotFull);
break;
}
return 0;
}