-
Notifications
You must be signed in to change notification settings - Fork 4
/
linklist1.C
50 lines (45 loc) · 808 Bytes
/
linklist1.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
#include<stdlib.h>
#include<stdio.h>
struct node {
int data;
struct node *next;
};
void printlist(struct node *n)
{
while(n!=NULL)
{
printf("%d\n",n->data);
n = n->next;
}
}
int search(struct node **head_ref,int num)
{
int count = 0;
if(*head_ref==NULL)
return 0;
struct node *temp = *head_ref;
while(temp!=NULL)
{
if(temp->data == num)
return count;
temp = temp->next;
count++;
}
}
int main()
{
struct node *head ,*second ,*third ;
head = (struct node*)malloc(sizeof(struct node));
second = (struct node*)malloc(sizeof(struct node));
third = (struct node*)malloc(sizeof(struct node));
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
printlist(head);
int num = search(&head,3);
printf("%d ",num);
return 0;
}