forked from Cravtos/data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list.c
168 lines (139 loc) · 2.88 KB
/
linked_list.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "linked_list.h"
#include <stdio.h>
#include <stdlib.h>
void push(linked_list* ll, int el)
{
while (ll->next != NULL)
ll = ll->next;
ll->next = calloc(1, sizeof(ll)); // check if allocated
ll->next->el = el;
}
void insert(linked_list* ll, int pos, int el)
{
for (int i = 0; i < pos; i++)
{
ll = ll->next;
}
linked_list* ll1 = calloc(1, sizeof(linked_list));
ll1->el = el;
ll1->next = ll->next;
ll->next = ll1;
}
int len(linked_list* ll)
{
int res=0;
while (ll->next != NULL)
{
ll = ll->next;
res++;
}
return res;
}
int is_empty(linked_list *ll){
if(ll->next == NULL)return 1;
return 0;
}
// Print last elements
void tail_uneffective(linked_list* head, size_t size, size_t k)
{
linked_list* list = head->next;
for (int i = 0; i < size - k; i++)
{
list = list->next;
}
for (int j = size - k; j < size; j++)
{
printf("%d ", list->el);
list = list->next;
}
}
// Print last elements.
void tail(linked_list* head, size_t amount)
{
linked_list* beg = head->next;
linked_list* end = head->next;
for (size_t i = 0; i < amount; i++)
{
end = end->next;
}
while (end != NULL)
{
beg = beg->next;
end = end->next;
}
for (int i = 0; i < amount; i++)
{
printf("%d ", beg->el);
beg = beg->next;
}
}
void print(linked_list* head)
{
linked_list* next = head->next;
while (next != NULL)
{
printf("%d ", next->el);
next = next->next;
}
}
// Detects cycles in list using fast and slow pointers.
int has_cycle(linked_list* head)
{
linked_list* slow=head->next;
linked_list* fast=head->next;
while (fast!=NULL && fast->next!=NULL && fast->next->next!=NULL)
{
fast=fast->next->next->next;
slow=slow->next->next;
if (fast==slow)
{
return 1;
}
}
return 0;
}
linked_list* _reverse(linked_list* ll)
{
if (ll->next == NULL)
{
return ll;
}
linked_list* head = ll;
linked_list* tail = ll->next;
linked_list* new_head = _reverse(tail);
tail->next = head;
head->next = NULL;
return new_head;
}
void reverse(linked_list* head)
{
if (head->next == NULL)
return;
linked_list* reversed = _reverse(head->next);
head->next = reversed;
}
void cycle_reverse(linked_list* head)
{
linked_list* prev = NULL;
linked_list* cur = head->next;
while (cur != NULL)
{
linked_list* next = cur->next;
cur->next = prev;
prev = cur;
cur = next;
}
head->next = prev;
}
int delete(linked_list* ll, int pos)
{
for (int i = 0; i < pos; i++)
{
ll = ll->next;
}
int el = ll->next->el;
linked_list* ll1 = ll->next;
ll->next=ll->next->next;
free(ll1);
return el;
}