-
Notifications
You must be signed in to change notification settings - Fork 216
/
Copy pathlinked_list_linear_search.cpp
55 lines (54 loc) · 1.08 KB
/
linked_list_linear_search.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
// Search linearly in a LinkedList
// Program Author : Abhisek Kumar Gupta
#include<bits/stdc++.h>
using namespace std;
class node{
public:
int data;
node* next;
node(int d){
data = d;
next = NULL;
}
};
bool linear_search(node *head, int key){
if(head == NULL){
return false;
}
else{
node *temp = head;
while(temp != NULL){
if(temp->data == key){
return true;
}
temp = temp->next;
}
}
return false;
}
void insert_at_head(node *&head, int data){
node *n = new node(data);
n->next = head;
head = n;
}
void print_linked_list(node *head){
while(head != NULL){
cout << head->data << "->";
head = head->next;
}
}
int main(){
node *head = NULL;
insert_at_head(head, 1);
insert_at_head(head, 2);
insert_at_head(head, 3);
print_linked_list(head);
cout << endl;
if(linear_search(head, 1)){
cout << "Found";
}
else{
cout << "Not Found";
}
return 0;
}