-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdouble_link_list.c
More file actions
50 lines (39 loc) · 988 Bytes
/
double_link_list.c
File metadata and controls
50 lines (39 loc) · 988 Bytes
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
#include "double_link_list.h"
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
node_t *create_node(int value, node_t *previous) {
node_t *new_node = (node_t *)malloc(sizeof(node_t));
new_node->value = value;
new_node->next = NULL;
new_node->prev = previous;
if (previous != NULL) {
previous->next = new_node;
}
return new_node;
}
node_t *create_list(int *values, size_t length) {
if (length == 0) {
return NULL;
}
node_t *head = create_node(values[0], NULL);
node_t *current = head;
for (size_t i = 1; i < length; i++) {
node_t *next = create_node(values[i], current);
current = next;
}
return head;
}
node_t *reverse_list(node_t *head) {
node_t *current = head;
node_t *temp = NULL;
while (current != NULL) {
head = current;
temp = current->prev;
current->prev = current->next;
current->next = temp;
// Move to the next node in the original list
current = current->prev;
}
return head;
}