forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLinear_Search.dart
129 lines (123 loc) · 3.48 KB
/
Linear_Search.dart
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
//Linear search or sequential search is a method for finding an element within a list.
//It sequentially checks each element of the list until a match is found or the whole list has been searched.
import 'dart:io';
void linearSearch(List<int> list, int x) {
int pass = 0;
list.forEach((val) {
if (val == x) {
print('Found $x in the List!');
pass = 1;
}
});
if (pass == 0) print('Did not find $x in the List!');
}
main() {
List<int> list = new List();
int task = 0;
print('Linear Search Program');
while (task != 4) {
print(
'0 - Display List\n1 - Insert element in List \n2 - Search element in List\n3 - Quit\nSelect task:-');
try {
task = int.parse(stdin.readLineSync());
} on Exception {
print('Please enter valid Integer value!');
continue;
}
switch (task) {
case 0:
if (list.length == 0) {
print('Empty List!');
} else {
print('List:-');
list.forEach((val) {
print(val);
});
}
break;
case 1:
int val = null;
print('Enter value that needs to be inserted in List:- ');
try {
val = int.parse(stdin.readLineSync());
} on Exception {
print('Please enter valid Integer value!');
continue;
}
if (val == null) {
print('Failed to insert element in List!');
continue;
}
list.add(val);
print('Successfully inserted $val in List!');
break;
case 2:
int val = null;
print('Enter value that needs to be searched in List:- ');
try {
val = int.parse(stdin.readLineSync());
} on Exception {
print('Please enter valid Integer value!');
continue;
}
if (val == null) {
print('Failed to search element in List!');
continue;
}
linearSearch(list, val);
break;
case 3:
print('Linear Search Program Complete');
break;
default:
continue;
}
}
}
/*
Sample Input/Output:
Linear Search Program
0 - Display List
1 - Insert element in List
2 - Search element in List
3 - Quit
Select task:- //to insert element in list
1
Enter value that needs to be inserted in List:-
1
Successfully inserted 1 in List!
0 - Display List
1 - Insert element in List
2 - Search element in List
3 - Quit
Select task:-
1
Enter value that needs to be inserted in List:-
2
Successfully inserted 2 in List!
0 - Display List
1 - Insert element in List
2 - Search element in List
3 - Quit
Select task:-
1
Enter value that needs to be inserted in List:-
3
Successfully inserted 3 in List!
0 - Display List
1 - Insert element in List
2 - Search element in List
3 - Quit
Select task:- //to search the given element
2
Enter value that needs to be searched in List:-
2
Found 2 in the List!
0 - Display List
1 - Insert element in List
2 - Search element in List
3 - Quit
Select task:- //to end the linear program algorithm
3
Linear Search Program Complete
*/