-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpz_factor_list.c
84 lines (69 loc) · 1.71 KB
/
mpz_factor_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
#include "mpz_factor_list.h"
/** \brief Push a factor pair to the given list.
*
* \param list mpz_factor_list_t** The list.
* \param factor1 mpz_t The first factor.
* \param factor2 mpz_t The second factor.
* \return void
*/
void mpz_factor_list_push(mpz_factor_list_t ** list, mpz_t factor1, mpz_t factor2)
{
// Create a new node.
mpz_factor_list_t * newNode;
newNode = malloc(sizeof(mpz_factor_list_t));
// Set the factors of the node.
mpz_init_set(newNode->factor1, factor1);
mpz_init_set(newNode->factor2, factor2);
// Insert the new node as first element of the list.
newNode->next = *list;
*list = newNode;
}
/** \brief Pop the last factor pair from the list.
*
* \param list mpz_factor_list_t** The list.
* \return void
*/
void mpz_factor_list_pop(mpz_factor_list_t ** list)
{
if (*list == NULL)
{
return;
}
mpz_clear((*list)->factor1);
mpz_clear((*list)->factor2);
mpz_factor_list_t * nextNode = NULL;
nextNode = (*list)->next;
free(*list);
*list = nextNode;
}
/** \brief Clear the whole list.
*
* \param list mpz_factor_list_t** The list.
* \return void
*
*/
void mpz_factor_list_clean(mpz_factor_list_t ** list)
{
while ( *list != NULL )
{
mpz_factor_list_pop(list);
}
}
/** \brief Print the contents of the list to the output.
*
* \param list mpz_factor_list_t* The list.
* \return void
*
*/
void mpz_factor_list_print(mpz_factor_list_t * list)
{
mpz_factor_list_t * current = list;
while (current != NULL)
{
mpz_out_str(stdout, 10, current->factor1);
printf(" x ");
mpz_out_str(stdout, 10, current->factor2);
printf("\n");
current = current->next;
}
}