-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimply_ll_ops.c
100 lines (82 loc) · 1.71 KB
/
simply_ll_ops.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
/**
* Copyright GOIDESCU Rares-Stefan 312CAb 2023-2024
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "headers/structs.h"
#include "headers/macros.h"
/*
* Functie care creeaza si initializeaza o lista simplu inlantuita
*
* @return s_ll_t* catre lista
*/
s_ll_t *create_sll(unsigned int data_size)
{
s_ll_t *list = malloc(sizeof(s_ll_t));
list->head = NULL;
list->size = data_size;
return list;
}
/*
* Functie care adauga un nou nod la inceputul
* unei liste simplu inlantuite
*/
void push_sll(s_ll_node_t **head, void *data, unsigned int data_size)
{
s_ll_node_t *new = malloc(sizeof(s_ll_node_t));
DIE(!new, "Malloc failed\n");
new->data = calloc(1, data_size);
DIE(!new->data, "Calloc failed\n");
memcpy(new->data, data, data_size);
new->next = *head;
*head = new;
}
/*
* Functie care scoate primul element dintr-o lista
* simplu inlantuita
*
* @return ll_node_t* head
*/
s_ll_node_t *pop_sll(s_ll_node_t **head_ptr)
{
if (!*head_ptr)
return NULL;
s_ll_node_t *head = *head_ptr;
(*head_ptr) = (*head_ptr)->next;
return head;
}
/*
* Functie care calculeaza lungimea unei liste simplu inlantuite
*
* @return (unsigned int)size
*/
unsigned int get_size_sll(s_ll_t *list)
{
unsigned int count = 0;
s_ll_node_t *curr = list->head;
while (curr) {
count++;
curr = curr->next;
}
return count;
}
/*
* Functie care elibereaza toata memoria
* alocata unei liste dublu inlantuite,
*/
void free_sll(s_ll_t **list_ptr)
{
s_ll_node_t *curr;
if (!*list_ptr || !*list_ptr)
return;
while (get_size_sll(*list_ptr) > 0) {
curr = pop_sll(&((*list_ptr)->head));
free(curr->data);
curr->data = NULL;
free(curr);
curr = NULL;
}
free(*list_ptr);
*list_ptr = NULL;
}