forked from suman-shah/c-program-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_in_doublyll.c
76 lines (69 loc) · 1.29 KB
/
find_in_doublyll.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
struct node *prev;
int data;
struct node *next;
};
typedef struct My_dll my_dll;
struct My_dll
{
struct node *head; // head=root
};
// Find the index of any node in the doubly linked list.
void find(my_dll *list, int x)
{
struct node *head = list->head;
int index = 0;
while (head != NULL)
{
if (head->data == x)
{
printf("%d\n", index);
return;
}
index++;
head = head->next;
}
printf("-1\n");
}
void print(my_dll *list)
{
struct node *head = list->head;
struct node *ptr;
ptr = head;
while (ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next;
}
printf("\n");
}
int main()
{
struct My_dll *list;
list = (struct My_dll *)malloc(sizeof(struct My_dll));
list->head = NULL;
while (1)
{
char name[100];
scanf("%s", name);
if (strcmp(name, "print") == 0)
{
print(list);
}
else if (strcmp(name, "find") == 0)
{
int a;
scanf("%d", &a);
find(list, a);
}
else if (strcmp(name, "exit") == 0)
{
return 0;
}
}
return 0;
}