-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
279 lines (255 loc) · 6.52 KB
/
main.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include <iostream>
#include <math.h>
using namespace std;
class node
{
public:
int data, size;
node *next;
void add(int);
void display(node *);
node* createCopy(int);
int validationEngine();
node* deleteNode();
};
node *head, *head2;
int del_index, tc_pass_num, expected_del_index;
int node::validationEngine()
{
int i = 0;
bool is_elem_deleted = false, check_all_elem = true, is_num_valid = false;
node *temp, *temp2;
tc_pass_num = 0;
temp = head;
while(temp != NULL)
{
temp = temp->next;
i++;
}
if(i == (size - 1)) //Validation 0: Total size of original array must be (s2.size - 1) after deletion
{
cout<<"\n\n----Validation 0: Check number of elements----";
cout<<"\nExpected number of elements before deletion = "<<(size - 1);
cout<<"\nObtained number of elements after deletion = "<<i;
cout<<"\nResult: PASSED\n";
tc_pass_num++;
}
else
{
cout<<"\n\n----Validation 0: Check number of elements----\nResult: FAILED\n";
return (tc_pass_num = 0);
}
temp = head;
temp2 = head2;
for(i = 0; i < size; i++)
{
if(is_elem_deleted == false && (temp->data != temp2->data)) //Validation 1: Check absence of deleted element in original set s1
{
is_elem_deleted = true;
if(temp2->next != NULL)
temp2 = temp2->next;
if(i == (size - 1)) //End of elements in s1, no more to check. So last element should be the deleted element
{
cout<<"\n----Validation 1: Check Deleted element----";
cout<<"\nExpected deleted index = "<<(size - 1 - del_index);
cout<<"\nObtained deleted index = "<<i;
cout<<"\nResult: PASSED\n";
cout<<"\n----Validation 2: Check Data integity----";
cout<<"\nExpected output: Values of original data set s1 must not be altered apart from deleted element.";
cout<<"\nResult: PASSED\n";
return 1;
}
}
if(temp->data != temp2->data) //Validation 2: Check if any other element mismatches
{
cout<<"\nValue mismatch at detected at index "<<i;
check_all_elem = false;
cout<<"\nResult: FAILED";
return 0; //Validation result immediately marked as: Fail
}
if(temp->next != NULL) //Check needed to stop traversal of original set s1 in case of deleting of last element
temp = temp->next;
if(temp2->next != NULL)
temp2 = temp2->next;
}
cout<<"\n----Validation 1: Check Deleted element----";
if(is_elem_deleted == true)
{
cout<<"\nGiven deleted index = "<<(del_index); //Since 'i' would have incremented extra, after the for loop
cout<<"\nExpected deleted index = "<<expected_del_index;
cout<<"\nResult: PASSED\n";
}
else
cout<<"\nResult: FAILED\n\n";
cout<<"\n----Validation 2: Check Data integity----";
if(check_all_elem == true)
{
cout<<"\nExpected output: Values of original data set s1 must not be altered apart from deleted element.";
cout<<"\nResult: PASSED\n";
}
else
cout<<"\nResult: FAILED\n\n";
if(is_elem_deleted == true && check_all_elem == true)
return 1;
else
return 0;
}
node* node::createCopy(int val)
{
node *temp = new node;
if(head2 == NULL)
{
temp->data = val;
temp->next = NULL;
head2 = temp;
}
else
{
node *ptr;
ptr = head2;
while(ptr->next != NULL)
{
ptr = ptr->next;
}
temp->data = val;
temp->next = NULL;
ptr->next = temp;
}
return head2;
}
node* node::deleteNode()
{
node* bef_node= NULL, *curr = NULL, *aft_node= NULL;
int curr_pos = 0;
curr = head;
if(del_index == (size - 1))
{
curr = head;
cout<<". So actual element to be deleted = "<<curr->data<<" at index = "<<(size - del_index - 1)<<endl;
expected_del_index = (size - 1);
head = head->next;
free(curr);
size--;
cout<<"New elements are:\n";
display(head);
return head;
}
while(curr_pos < ((size - 1) - del_index))
{
curr_pos++;
bef_node = curr;
curr = curr->next;
}
expected_del_index = curr_pos;
cout<<"\nActual element to be deleted = "<<curr->data<<" at index = "<<curr_pos<<endl;
//Deleting node pointed by curr
bef_node->next = curr->next;
free(curr);
size--;
cout<<"New elements are:\n";
display(head);
return head;
}
void node::add(int val)
{
node *temp = new node;
if(head == NULL)
{
temp->data = val;
temp->next = NULL;
head = temp;
}
else
{
node *ptr;
ptr = head;
while(ptr->next != NULL)
{
ptr = ptr->next;
}
temp->data = val;
temp->next = NULL;
ptr->next = temp;
}
}
void node::display(node *x)
{
node *temp;
int idx = 0;
temp = x;
while(temp != NULL)
{
cout<<endl<<"Data = ["<<temp->data<<"] -- index = "<<idx++;
temp = temp->next;
}
}
int main()
{
int i = 0, sample[10] = {0}, pass_count = 0, ch = 0;
node s1, s2;
for(i = 0; i < 10; i++)
{
sample[i] = ((rand() % 50) + 10); //restricting generated number from 10 to 60
s1.add(sample[i]);
s2.createCopy(sample[i]);
}
s1.size = i;
s2.size = i;
cout<<"\nOriginal Data Set generated:";
s1.display(head);
cout<<"\n\n----- Validation engine begins -----\n\n";
cout<<"1. Deleting node at index = 0 by giving del_index = "<<(s2.size - 1)<<"\n2. Deleting node at any other given index\n3. Deleting node at last index = "<<(s2.size - 1)<<" by giving del_index = 0"<<endl;
cout<<"\nEnter which test case to be executed: ";
cin>>ch;
switch(ch)
{
case 1:
{
del_index = (s2.size - 1);
cout<<"\n1. Deleting node at index = 0 by giving del_index = "<<del_index;
head = s1.deleteNode();
pass_count = s2.validationEngine();
if(pass_count)
cout<<"\nOverall result: Deleted node successfully at index = "<<(s2.size - 1 - del_index)<<" by giving del_index = "<<del_index<<endl;
else
cout<<"\nFailed.....\n";
break;
}
case 2:
{
cout<<"\nEnter a random index number to be deleted apart from 0 or 10: ";
cin>>del_index;
if(del_index <= 0 || del_index >= 10)
{
cout<<"\nInvalid index provided";
return 0;
}
cout<<"\n2. Deleting node at any other given del_index = "<<del_index;
head = s1.deleteNode();
pass_count = s2.validationEngine();
if(pass_count)
cout<<"\nOverall result: Deleted node successfully at index = "<<(s2.size - 1 - del_index)<<" by giving del_index = "<<del_index<<endl;
else
cout<<"\nFailed.....\n";
break;
}
case 3:
{
del_index = 0;
cout<<"\n3. Deleting node at last index = "<<(s2.size - 1)<<" by giving first index 0 as input"<<endl;
head = s1.deleteNode();
pass_count = s2.validationEngine();
if(pass_count)
cout<<"\nOverall result: Deleted node successfully at index = "<<(s2.size - 1 - del_index)<<" by giving del_index = "<<del_index<<endl;
else
cout<<"\nFailed.....\n";
break;
}
default:
{
cout<<"\nInvalid user choice given.";
return 0;
}
}
return 0;
}