-
Notifications
You must be signed in to change notification settings - Fork 7
/
linear_search_linked_llist.cpp
99 lines (84 loc) · 1.46 KB
/
linear_search_linked_llist.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include<iostream>
using namespace std;
class Node{
public:
int data;
Node *next;
}*first=NULL;
void create(int a[],int n){
int i;
Node *t,*last;
first=new Node();
first->data = a[0];
first->next = NULL;
last=first;
for(i=1;i<n;i++){
t = new Node();
t->data = a[i];
t->next = NULL;
last->next=t;
last=t;
}
}
int max(Node *p){
int m = -32768;
while(p){
if(p->data>m)
m = p->data;
p=p->next;
}
return m;
}
void display(Node *p){
while(p!=NULL){
cout<<p->data<<" ";
p=p->next;
}
}
Node * Lsearch(Node *p,int key){
while(p!=NULL){
if(key==p->data)
return p;
p=p->next;
}
return NULL;
}
Node * eff_Lsearch(Node *p,int key){
Node *q=NULL;
while(p!=NULL){
if(key==p->data){
q->next=p->next;
p->next=first;
first=p;
return p;
}
q=p;
p=p->next;
}
return NULL;
}
Node * Rsearch(Node *p,int key){
if(p==NULL)
return NULL;
if(key==p->data)
return p;
return Rsearch(p->next,key);
}
int main(){
Node *temp;
int a[] = {8,18,28,12,4};
create(a,5);
// rec_display(first);
// cout<<"Count: "<<count(first);
// cout<<"Recursive Count: "<<rec_count(first);
// cout<<"Sum is: "<<add(first);
// cout<<"Maximum: "<<max(first);
temp = Lsearch(first,18);
temp = eff_Lsearch(first,18);
if(temp)
cout<<"Key found :"<<temp->data<<"\n";
else
cout<<"Key not found"<<"\n";
display(first);
return 0;
}