-
Notifications
You must be signed in to change notification settings - Fork 0
/
singleLinkedList.c
89 lines (72 loc) · 1.3 KB
/
singleLinkedList.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
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data ;
struct node *next ;
}
node ;
node *list;
node * create (int val)
{
node *n = malloc(sizeof(node));
n->data = val ;
n->next = NULL ;
return n ;
}
node *insert(node *head , int val )
{
node *new = malloc(sizeof(node));
new->data = val ;
new->next = head ;
head = new ;
return head ;
}
int count = 0;
// recursion output
int output (node *lists)
{
if (lists != NULL) {
printf("%d\n" , lists->data);
count = count + 1 ;
output(lists->next);
}
else {
printf("\ncount : %d\n", count);
}
}
int searching (node *lists , int target)
{
node *tmp = lists ;
for (node *tmp = lists ; tmp!= NULL ; tmp = tmp->next)
{
if (tmp->data == target)
{
printf("found ..");
return 0 ;
}
}
printf("not found...") ;
return 1 ;
}
void destroy (node *l)
{
node *tmp ;
while (l != NULL)
{
tmp = l->next ;
free(l);
l = tmp ;
}
}
int main (void)
{
list = NULL ;
list = create (6) ;
list = insert(list, 8) ;
list = insert(list, 4) ;
list = insert(list, 2) ;
output(list);
searching (list, 10);
destroy (list);
}