Skip to content

Commit 7e38973

Browse files
Merge pull request #9 from shreyam06/patch-2
Create searchingll.c
2 parents 6a31811 + 8dca849 commit 7e38973

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

Linked List/searchingll.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
struct node
4+
{
5+
int data;
6+
struct node *next;
7+
} * head, *temp;
8+
void creatLL(int);
9+
void display();
10+
int findElement(int);
11+
int main()
12+
{
13+
int n, f, place;
14+
printf("enter the numbe rof node");
15+
scanf("%d", &n);
16+
creatLL(n);
17+
display();
18+
printf("\nenter the element to be searched");
19+
scanf("%d", &f);
20+
place = findElement(f);
21+
if (place <= n)
22+
{
23+
printf("element is at node of pos %d", place);
24+
}
25+
else
26+
{
27+
printf("element not found");
28+
}
29+
}
30+
void creatLL(int n)
31+
{
32+
int i, num;
33+
struct node *newNode;
34+
if (n >= 1)
35+
{
36+
head = (struct node *)malloc(sizeof(struct node));
37+
if (head != NULL)
38+
{
39+
printf("enter the data");
40+
scanf("%d", &num);
41+
head->data = num;
42+
head->next = NULL;
43+
temp = head;
44+
45+
for (i = 2; i <= n; i++)
46+
{
47+
newNode = (struct node *)malloc(sizeof(struct node));
48+
printf("entet the data");
49+
scanf("%d", &num);
50+
newNode->data = num;
51+
temp->next = newNode;
52+
newNode->next = NULL;
53+
temp = newNode;
54+
}
55+
}
56+
}
57+
}
58+
void display()
59+
{
60+
struct node *tmp;
61+
tmp = head;
62+
while (tmp != NULL)
63+
{
64+
printf("%d\t", tmp->data);
65+
tmp = tmp->next;
66+
}
67+
}
68+
int findElement(int f)
69+
{
70+
int count = 1;
71+
struct node *tmp;
72+
tmp = head;
73+
while (tmp->next != NULL)
74+
{
75+
if (tmp->data == f)
76+
{
77+
break;
78+
}
79+
else
80+
{
81+
count++;
82+
tmp = tmp->next;
83+
}
84+
}
85+
return count;
86+
}

0 commit comments

Comments
 (0)