-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathint_sll.c
More file actions
266 lines (232 loc) · 5.02 KB
/
Copy pathint_sll.c
File metadata and controls
266 lines (232 loc) · 5.02 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Name: int_sll.c
// Purpose: Implements a singly linked list data structure for integers. Head
// is empty.
//
// Author: Taylor Cole
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct Int_Node {
int value;
struct Int_Node* next;
} Int_Node;
/**
* Creates a node on the heap with the given value
*/
Int_Node* sll_new_node(int val)
{
Int_Node* node = malloc(sizeof(Int_Node));
node->value = val;
node->next = NULL;
return node;
}
/**
* Initializes a singly linked list as an empty head node
*/
Int_Node* sll_init(void)
{
Int_Node* head = malloc(sizeof(Int_Node));
head->next = NULL;
return head;
}
/**
* Frees all the nodes of the linked list
*/
void sll_free(Int_Node* sll)
{
Int_Node* next_node;
while (sll) {
next_node = sll->next;
free(sll);
sll = next_node;
}
}
/**
* Get the length of the linked list
*/
int sll_length(Int_Node* sll)
{
int len = 0;
Int_Node* node = sll->next;
while (node) {
len++;
node = node->next;
}
return len;
}
/**
* Inserts a node at the beginning of the list
*/
void sll_insert_front(Int_Node* head, int val)
{
Int_Node* new_node = sll_new_node(val);
new_node->next = head->next;
head->next = new_node;
}
/**
* Inserts a node at the end of the list
*/
void sll_insert_back(Int_Node* head, int val)
{
Int_Node* new_tail = sll_new_node(val);
Int_Node* curr = head;
while (curr->next) curr = curr->next;
curr->next = new_tail;
}
/**
* Inserts a node at the given index
*/
int sll_insert_at_index(Int_Node* head, int index, int val)
{
if (index < 0) {
perror("index out of bounds");
return 1;
}
Int_Node* curr = head;
for (int i = 0; i < index; i++) {
curr = curr->next;
if (curr == NULL) {
perror("sll not long enough");
return 1;
}
}
Int_Node* new_node = sll_new_node(val);
new_node->next = curr->next;
curr->next = new_node;
return 0;
}
/**
* Removes node at the specified index
*/
int sll_remove_at_index(Int_Node* head, int index)
{
if (index < 0 || head->next == NULL) {
perror("list not long enough");
return 1;
}
Int_Node* curr = head;
for (int i = 0; i < index; i++) {
curr = curr->next;
if (curr->next == NULL) {
perror("index not found");
return 1;
}
}
Int_Node* rm_node = curr->next;
curr->next = curr->next->next; // Cut off the node
free(rm_node);
return 0;
}
/**
* Removes a node with the matching value
*/
int sll_remove(Int_Node* head, int val)
{
Int_Node* curr = head;
while (curr->next) {
if (curr->next->value == val) {
Int_Node* rm_node = curr->next;
curr->next = curr->next->next;
free(rm_node);
return 0;
}
curr = curr->next;
}
return 1;
}
/**
* Counts the number of a given value in the list and returns the count
*/
int sll_count(Int_Node* head, int val)
{
int count = 0;
Int_Node* curr = head->next;
while (curr) {
if (curr->value == val)
count++;
curr = curr->next;
}
return count;
}
/**
* Checks if a value is in the list. Returns true or false
*/
bool sll_find(Int_Node* head, int val)
{
Int_Node* curr = head->next;
while (curr) {
if (curr->value == val)
return true;
curr = curr->next;
}
return false;
}
/**
* Creates a slice of linked list at start of size
*/
Int_Node* sll_slice(Int_Node* head, int start, int size)
{
if (start < 0 || size < 0) {
perror("no");
return NULL;
}
Int_Node* curr = head;
// Step to start point
for(int i = 0; i < start + 1; i++) {
curr = curr->next;
if (curr == NULL) {
perror("no");
return NULL;
}
}
Int_Node* new_list = sll_init();
Int_Node* new_list_node = new_list;
for (int i = 0; i < size; i++) {
if (curr == NULL) {
perror("out of bounds");
return NULL;
}
new_list_node->next = sll_new_node(curr->value);
new_list_node = new_list_node->next;
curr = curr->next;
}
return new_list;
}
/**
* Prints the linked list
*/
void sll_print(Int_Node* head)
{
Int_Node* curr = head;
printf("SLL: HEAD ");
while (curr->next) {
printf("-> N(%d) ", curr->next->value);
curr = curr->next;
}
}
/**
* Pushes an element to the front. Alias for sll_insert_front
*/
void sll_push(Int_Node* head, int val)
{
sll_insert_front(head, val);
}
/**
* Pops the top element off the list (first element after head) and returns the
* value.
*/
int sll_pop(Int_Node* head)
{
Int_Node* top_node = head->next;
int top_val = top_node->value;
head->next = head->next->next;
free(top_node);
return top_val;
}
/**
* Peeks at the first element of the list without popping off
*/
int sll_peek(Int_Node* head)
{
return head->next->value;
}