-
Notifications
You must be signed in to change notification settings - Fork 17
/
Linear_searching_LL.cpp
83 lines (65 loc) · 1.45 KB
/
Linear_searching_LL.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
#include<iostream>
using namespace std;
class node{
public:
int data;
node* next;
node(int val){
data = val; // Constructor
next = NULL;
}
};
void insertAtTail(node* &head,int val){
node* n = new node(val); // created a new node n which has data = value and next = NULL
if (head== NULL)
{
head = n;
return ;
}
// Head is pointing to initial value of node
node* temp = head; // iterator for traversing to last of linked list
while (temp->next!=NULL)
{
temp = temp->next; // temp stores address and temp->next give address
}
temp->next=n;
}
void InsertAtHead(node* &head, int val){
node* n ;
n = new node(val);
n->next = head;
head= n;
}
bool search(node* head, int key){
node* temp;
temp = head;
while (temp!=NULL)
{
if (temp->data==key)
{
return true;
}
temp=temp->next;
}
return false;
}
void display(node* head){
node* temp = head;
while (temp!=NULL)
{
cout<<temp->data<<" -> ";
temp = temp->next;
}
cout<<"NULL"<<endl;
}
int main(){
node* head=NULL;
insertAtTail(head,1);
insertAtTail(head,2);
insertAtTail(head,3);
display(head);
InsertAtHead(head, 4);
display(head);
cout<<search(head,3)<<endl;
return 0;
}