-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLT_02.cpp
More file actions
248 lines (219 loc) · 5.51 KB
/
Copy pathLT_02.cpp
File metadata and controls
248 lines (219 loc) · 5.51 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
#include<iostream>
#include<cmath>
using namespace std;
struct Node {
private:
int data;
Node *next;
public:
void insertNode(Node *&head, int val) {
Node *node = new Node;
node->data=val;
node->next=NULL;
if(head==NULL) {
head=node;
cout<<"Node inserted at first."<<endl;
return;
}
Node *current=head;
while(current->next != NULL) {
current=current->next;
}
current->next=node;
cout<<"Node inserted successfuly."<<endl;
return;
}
void displayAllNode(Node *head) {
if(head==NULL) {
cout<<"Linked List is Empty!"<<endl;
return;
}
Node *current=head;
cout<<"Displaying Linked List Elements: "<<endl;
while(current != NULL) {
cout<<current->data<<" ";
current=current->next;
}
return;
}
void insertAfter(Node *&head, int val, int target) {
if(head==NULL) {
Node *node = new Node;
node->data=val;
node->next=NULL;
head=node;
cout<<"List is Empty! So Insertion is possilbe only at start."<<endl;
return;
}
Node *node = new Node;
node->data=val;
Node *current = head;
Node *store;
while(current != NULL ) {
if(current->data==target) {
store=current->next;
current->next=node;
node->next=store;
cout<<"Node inserted successfuly."<<endl;
return;
}
current=current->next;
}
cout<<"Target is not found so Insertion not possible!"<<endl;
return;
}
void deleteNode(Node *&head, int val) {
if(head==NULL) {
cout<<"List is empty! So deletion is not possible."<<endl;
return;
}
if(head != NULL) {
if(head->data==val) {
head=head->next;
cout<<"First Node is deleted."<<endl;
return;
}
}
Node *current= head;
Node *pred=head;
current=current->next;
while(current != NULL) {
if(current->data==val) {
pred->next=current->next;
cout<<"Node deleted successfully."<<endl;
return;
}
pred=current;
current=current->next;
}
cout<<"Sorry! Element is not found."<<endl;
return;
}
void searchNode(Node *head, int val) {
if(head==NULL) {
cout<<"Linked List is empty."<<endl;
return;
}
Node *current = head;
while(current != NULL) {
if(current->data==val) {
cout<<"Congratulations! element is founded."<<endl;
return;
}
current=current->next;
}
cout<<"Sorry! element is not founded."<<endl;
}
void displayinRange(Node *head, int start, int end) {
if(head==NULL) {
cout<<"Linked List is empty."<<endl;
return;
}
Node *current=head;
while(current != NULL) {
if(current->data>=start && current->data<=end) {
cout<<current->data<<" ";
}
current=current->next;
}
}
void Report_Statistics(Node *head) {
if(head==NULL) {
cout<<"List is empty! So no report statistics."<<endl;
return;
}
Node *current=head;
double MAX= current->data;
double MIN= current->data;
double SUM=current->data;
int count=1;
current=current->next;
while(current != NULL) {
if(current->data>MAX) MAX=current->data;
if(current->data<MIN) MIN=current->data;
SUM=SUM+current->data;
count++;
current=current->next;
}
cout<<"Maximum in the Linked List: "<<MAX<<endl;
cout<<"Minimum in the Linked List: "<<MIN<<endl;
cout<<"Sum of all elements inList: "<<SUM<<endl;
cout<<"Total Nodes in the L.List: "<<count<<endl;
double AVR=SUM/count;
cout<<"Average in the Linked List: "<<AVR<<endl;
double SD_SUM=0, SQR=0, Minus=0;
Node *now = head;
while(now != NULL ) {
Minus=now->data - AVR;
SQR=Minus*Minus;
SD_SUM=SD_SUM + SQR;
now=now->next;
}
double Standard_Deviation=SD_SUM/--count;
double result = sqrt(Standard_Deviation);
cout<<"Standard Deviation in List: "<<result<<endl;
}
};
int main() {
Node run;
Node *head=NULL;
int M_value=0, M_target=0;
cout<<"===== Select a choice of your interest ====="<<endl;
cout<<"1. Display all the elements of the list: "<<endl;
cout<<"2. Insert a node at the end: "<<endl;
cout<<"3. Insert a node after a given value: "<<endl;
cout<<"4. Delete a node: "<<endl;
cout<<"5. Search for an element: "<<endl;
cout<<"6. Report Statistics: "<<endl;
cout<<"7. Display elements that fall in a given range: "<<endl;
cout<<"8. For Quit: "<<endl;
char option;
do {
cout<<"\nEnter a choice: ";
cin>>option;
switch(option) {
case '1':
run.displayAllNode(head);
break;
case '2':
cout<<"Enter value: ";
cin>>M_value;
run.insertNode(head, M_value);
break;
case '3':
cout<<"Enter target after which value is to be enterd: ";
cin>>M_target;
cout<<"Enter value: ";
cin>>M_value;
run.insertAfter(head, M_value, M_target);
break;
case '4':
cout<<"Enter value to delete from Linked List: ";
cin>>M_target;
run.deleteNode(head, M_target);
break;
case '5':
cout<<"Enter element to search: ";
cin>>M_value;
run.searchNode(head, M_value);
break;
case '6':
run.Report_Statistics(head);
break;
case '7':
cout<<"Enter Starting Range: ";
int S_point;
cin>>S_point;
cout<<"Enter ending Range: ";
int E_point;
cin>>E_point;
run.displayinRange(head, S_point, E_point);
break;
case '8':
cout<<"Exit";
break;
default: cout<<"In-valid! Choice?"<<endl;
}
}while(option != '8');
return 0;
}