-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoubly_list.c
More file actions
183 lines (156 loc) · 4.13 KB
/
doubly_list.c
File metadata and controls
183 lines (156 loc) · 4.13 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
// Copyright Diaconescu Stefania Clara 313CA 2023-2024
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <inttypes.h>
#include "doubly_list.h"
// Functie care trebuie apelata pentru alocarea si initializarea unei liste.
// (Setare valori initiale pentru campurile specifice structurii list).
doubly_linked_list_t*
dll_create(unsigned int data_size, unsigned int nr_bytes_per_node)
{
doubly_linked_list_t *list = malloc(1 * sizeof(doubly_linked_list_t));
DIE(!list, "malloc failed");
list->head = NULL;
list->size = 0;
list->data_size = data_size;
list->nr_bytes_per_node = nr_bytes_per_node;
return list;
}
// Functia adauga un nod in lista pe pozitia n.
void
dll_add_nth_node(doubly_linked_list_t *list, unsigned int n,
unsigned long start_addr, unsigned int bytes_per_node,
unsigned long index)
{
dll_node_t *node = malloc(1 * sizeof(dll_node_t));
DIE(!node, "malloc failed");
node->data = malloc(sizeof(info_t));
DIE(!node->data, "malloc failed");
((info_t *)node->data)->input = NULL;
if (!list->head) {
node->next = NULL;
node->prev = NULL;
list->head = node;
} else if (n == 0) {
node->next = list->head;
node->prev = NULL;
node->next->prev = node;
list->head = node;
} else if (n >= list->size) {
dll_node_t *current = list->head;
while (current->next)
current = current->next;
current->next = node;
node->next = NULL;
node->prev = current;
} else {
dll_node_t *current = list->head;
unsigned int i = 0;
while (i < n - 1 && current->next) {
current = current->next;
i++;
}
node->next = current->next;
node->prev = current;
current->next->prev = node;
current->next = node;
}
((info_t *)node->data)->nr_bytes = bytes_per_node;
((info_t *)node->data)->index = index;
((info_t *)node->data)->start_addr = start_addr;
list->size++;
}
// Elimina nodul de pe pozitia n din lista al carei pointer este trimis ca
// parametru. Functia intoarce un pointer spre acest nod proaspat eliminat din
// lista.
dll_node_t*
dll_remove_nth_node(doubly_linked_list_t *list, unsigned int n)
{
if (!list || !list->head)
return NULL;
if (n == 0) {
dll_node_t *current = list->head;
if (list->head->next)
list->head->next->prev = NULL;
list->head = list->head->next;
list->size--;
return current;
}
if (n >= list->size - 1) {
dll_node_t *current = list->head;
while (current->next)
current = current->next;
current->prev->next = NULL;
list->size--;
return current;
}
dll_node_t *current = list->head;
unsigned int i = 0;
while (i < n) {
current = current->next;
i++;
}
current->prev->next = current->next;
current->next->prev = current->prev;
list->size--;
current->next = NULL;
return current;
}
// Procedura elibereaza memoria folosita de toate nodurile din lista, iar la
// sfarsit elibereaza memoria folosita de structura lista.
void
dll_free(doubly_linked_list_t **pp_list)
{
if (!(*pp_list))
return;
if (!(*pp_list)->head) {
free(*pp_list);
(*pp_list) = NULL;
return;
}
dll_node_t *current = (*pp_list)->head;
while (current) {
dll_node_t *next = current->next;
if (((info_t *)current->data)->input)
free(((info_t *)current->data)->input);
if (current->data)
free(current->data);
free(current);
current = next;
}
free(*pp_list);
(*pp_list) = NULL;
}
// Functia printeaza adresele blocurilor din lista de blocuri nealocate.
void
dll_print_addr_list(doubly_linked_list_t *list)
{
dll_node_t *current;
if (!list->head)
return;
current = list->head;
while (current->next) {
printf(" 0x%lx", ((info_t *)current->data)->start_addr);
current = current->next;
}
printf(" 0x%lx", ((info_t *)current->data)->start_addr);
printf("\n");
}
// Functia printeaza adresele blocurilor din lista de blocuri alocate.
void
dll_print_alloc_list(doubly_linked_list_t *list)
{
dll_node_t *current;
if (!list->head)
return;
current = list->head;
while (current->next) {
printf(" (0x%lx - %d)", ((info_t *)current->data)->start_addr,
((info_t *)current->data)->nr_bytes);
current = current->next;
}
printf(" (0x%lx - %d)", ((info_t *)current->data)->start_addr,
((info_t *)current->data)->nr_bytes);
}