-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircular_linked_list.h
More file actions
87 lines (74 loc) · 1.7 KB
/
circular_linked_list.h
File metadata and controls
87 lines (74 loc) · 1.7 KB
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
#ifndef CIRCULAR_LINKED_LIST_H_INCLUDED
#define CIRCULAR_LINKED_LIST_H_INCLUDED
#define TRUE 1
#define FALSE 0
typedef int bool;
typedef struct {
int value;
struct CNode *next;
} CNode;
void insertToCLL (CNode **head, int value) {
CNode *n = malloc(sizeof(CNode));
n->value = value;
n->next = *head;
if(*head == NULL) {
n->next = n;
*head = n;
}
else {
CNode *t = *head;
while (t->next != *head)
t = t->next;
t->next = n;
}
}
void deleteFromCLL (CNode **head, int value) {
if (*head == NULL)
return;
CNode *t = *head, *prev = *head;
// If head will be removed
if((*head)->value == value) {
while(t->next != *head)
t = t->next;
t->next = (*head)->next;
*head = (*head)->next;
return;
}
// Search the value
t = t->next;
while(t != *head && t->value != value) {
prev = t;
t = t->next;
}
// If value was found, delete
if(t->value == value) {
prev->next = t->next;
free(t);
}
}
bool searchInCLL (CNode *head, int value) {
CNode *t = head;
if (t->value == value)
return TRUE;
t = t->next;
while(t != head) {
if(t->value == value)
return TRUE;
t = t->next;
}
return FALSE;
}
void printCLL (CNode *head) {
CNode *t = head;
if(t != NULL) {
printf("%d -> ", t->value);
t = t->next;
}
while(t != head) {
if(t->next == head) printf("%d -----> *%d", t->value, head->value);
else printf("%d -> ", t->value);
t = t->next;
}
printf("\n");
}
#endif // CIRCULAR_LINKED_LIST_H_INCLUDED