Skip to content

Commit ce62f7f

Browse files
committed
linear search program
1 parent 392cd65 commit ce62f7f

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

Data Structures/LinkedLists/Singly_LL.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ Node* RemoveDuplicates(Node *head) //O(n)-TIME COMPLEXITY
214214
temp1=temp->next;
215215
if(temp->data == temp1->data) {
216216

217+
217218
temp->next = temp1->next;
218219
delete(temp1);
219220

@@ -232,7 +233,7 @@ Node* RemoveDuplicates(Node *head) //O(n)-TIME COMPLEXITY
232233

233234

234235

235-
//a function to remove a dpulicate node from an unsorted List
236+
//a function to remove a duplicate node from an unsorted List
236237

237238
void RemoveUnsortedDup(struct Node *head) {
238239

@@ -241,7 +242,7 @@ void RemoveUnsortedDup(struct Node *head) {
241242

242243
//traversing the list
243244
while(temp->next!=NULL) {
244-
temp1= temp;
245+
temp1 = temp;
245246

246247
while(temp1->next!=NULL)
247248
{
@@ -385,14 +386,19 @@ int main() {
385386

386387

387388

389+
388390
InsertList(1,1);
389-
InsertList(10,2);
390-
InsertList(1,3);
391+
InsertList(1,2);
392+
InsertList(20,3);
391393
InsertList(20,4);
392-
InsertList(25,5);
394+
InsertList(51,5);
395+
InsertList(61,6);
393396
// cout<<"\n";
394397
//
395398

399+
cout<<listlength(head)<<endl;
400+
401+
//removing duplicates form unsorted list
396402
RemoveUnsortedDup(head);
397403
cout<<"After duplicate removal"<<endl;
398404
cout<<listlength(head)<<endl;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include<iostream>
2+
#include<algorithm>
3+
using namespace std;
4+
5+
//Linear search program- TIME COMPLEXITY-O(n)
6+
7+
//one by one match data with array elements , if data matches , return its index , otherwise keep searching and matching
8+
//Linear search is rarely used practically.
9+
10+
//other search algorithms such as the binary search algorithm and hash tables allow significantly faster searching comparison to Linear search.
11+
int n=5;
12+
int LinearSearch(int *arr,int data) {
13+
14+
for(int i = 0; i < n ; i++)
15+
{
16+
if(arr[i]==data)
17+
{
18+
return i;
19+
}
20+
21+
return -1;
22+
23+
}
24+
25+
26+
27+
}
28+
29+
int main() {
30+
31+
32+
33+
int data,arr[n];
34+
cin>>data;
35+
for(int i = 0; i < n ; i++) {
36+
cin>>arr[i];
37+
38+
}
39+
40+
cout<<LinearSearch(arr,data);
41+
42+
43+
return 0;
44+
}

0 commit comments

Comments
 (0)