-
Notifications
You must be signed in to change notification settings - Fork 0
/
linearsearchll.c
76 lines (71 loc) · 1.38 KB
/
linearsearchll.c
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
//Linear search - Singly Linked list | Vishruth codes
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*root=NULL;
//Function to insert elements into the list
void append(int ar[], int n)
{
struct node *last,*p;
root=(struct node *)malloc(sizeof(struct node));
root->data=ar[0];
root->next=NULL;
last=root;
for(int i=1;i<n;i++)
{
p=(struct node *)malloc(sizeof(struct node));
p->data=ar[i];
p->next=NULL;
last->next=p;
last=p;
}
}
//Function to display elements into the list
void display()
{
printf("\nThe array into SLL: ");
struct node *p;
p=root;
while(p)
{
printf("%d -> ",p->data);
p=p->next;
}
}
//Function to search the key element and reutrn its address
int * lsearch(int k)
{
struct node *p;
p=root;
while(p) //=>p!=NULL
{
if(p->data==k)
{
return (&p->data);
}
p=p->next;
}
return 0;
}
int main()
{
int len=5,key,*addr;
int ar[]={1,3,5,7,9};
append(ar,len);
display();
printf("\nEnter the key element: ");
scanf("%d",&key);
addr=lsearch(key);
if(addr==0)
{
printf("\nThe element is not present in the list.");
}
else
{
printf("\nThe element is present in the list at address %d", addr);
}
return 0;
}