-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirst.c
97 lines (66 loc) · 1.41 KB
/
first.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
void printlist(struct node *n)
{
while(n!=NULL)
{
printf("%d\n",n->data);
n = n->next;
}
}
void push(struct node** head_ref, int data)
{
struct node* new_node = NULL;
new_node = (struct node*)malloc(sizeof(struct node));
new_node->data=data;
new_node->next=(*head_ref);
(*head_ref)=new_node;
}
int count(struct node* head)
{
if(head==NULL)
{
return 0;
}
return (1+count(head->next));
}
void search(struct node* n)
{
int x;
printf("enter the element to be searched");
scanf("%d",&x);
while(n!=NULL)
{
if(n->data==x)
printf("data is present");
n=n->next;
}
}
int main()
{
struct node* head = NULL;
struct node* second = NULL;
struct node* third = NULL;
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;
push(&head,0);
push(&head,4);
printlist(head);
int c=count(head);
printf("recursive count is %d",c);
search(head);
getchar();
return 0;
}