-
Notifications
You must be signed in to change notification settings - Fork 1
/
stack.c
144 lines (130 loc) · 3.47 KB
/
stack.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
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
// copri, Attacking RSA by factoring coprimes
//
// License: GNU Lesser General Public License (LGPL), version 3 or later
// See the lgpl.txt file in the root directory or <https://www.gnu.org/licenses/lgpl>.
#include <gmp.h>
#include <stdlib.h>
#include "stack.h"
#include "config.h"
// ## Simple stack
// Create a new stack.
void mpz_stack_init(mpz_stack **s)
{
mpz_stack *n = malloc(sizeof(mpz_stack));
n->top = NULL;
n->size = 0;
*s = n;
}
// Delete a stack and all its nodes.
void mpz_stack_clear(mpz_stack *s)
{
mpz_stack_node *top = s->top;
while (top != NULL) {
mpz_stack_node *next = top->next;
mpz_clear(top->val);
free(top);
top = next;
}
free(s);
}
// Pushes a value onto the stack,
// return 1 if element was pushed on successfully, 0 otherwise
int mpz_stack_push(mpz_stack *s, mpz_t val)
{
if (s == NULL) return 0;
mpz_stack_node *n = malloc(sizeof(mpz_stack_node));
if (n == NULL) return 0;
mpz_init_set(n->val, val);
n->next = s->top;
s->top = n;
s->size += 1;
return 1;
}
// Removes top element from stack,
// return 1 if an element was removed and 0 otherwise
// return value is passed through value reference
int mpz_stack_pop(mpz_stack *s, mpz_t val)
{
if (s == NULL || s->top == NULL) return 0;
mpz_stack_node *top = s->top;
mpz_set(val, top->val);
s->top = top->next;
s->size -= 1;
mpz_clear(top->val);
free(top);
return 1;
}
// Get the values of the top element
// return 1 if an element an top element exists
// return value is passed through value reference
int mpz_stack_top(mpz_stack *s, mpz_t val)
{
if (s == NULL || s->top == NULL) return 0;
mpz_set(val, s->top->val);
return 1;
}
// ## (mpz, ui) pair stack
// Create a new pair stack.
void mpz_ui_pair_stack_init(mpz_ui_pair_stack **s)
{
mpz_ui_pair_stack *n = malloc(sizeof(mpz_ui_pair_stack));
n->top = NULL;
n->size = 0;
*s = n;
}
// Delete a stack and all its nodes
void mpz_ui_pair_stack_clear(mpz_ui_pair_stack *s)
{
mpz_ui_pair_stack_node *top = s->top;
while (top != NULL) {
mpz_ui_pair_stack_node *next = top->next;
mpz_clear(top->b);
free(top);
top = next;
}
free(s);
}
// Pushes a value onto the stack,
// return 1 if element was pushed on successfully, 0 otherwise
int mpz_ui_pair_stack_push(mpz_ui_pair_stack *s, size_t a, mpz_t b)
{
if (s == NULL) return 0;
mpz_ui_pair_stack_node *n = malloc(sizeof(mpz_ui_pair_stack_node));
if (n == NULL) return 0;
n->a = a;
mpz_init_set(n->b, b);
n->next = s->top;
s->top = n;
s->size += 1;
return 1;
}
// Removes top element from stack,
// return 1 if an element was removed and 0 otherwise
// return value is passed through value reference
int mpz_ui_pair_stack_pop(mpz_ui_pair_stack *s, size_t *a, mpz_t b)
{
if (s == NULL || s->top == NULL) return 0;
mpz_ui_pair_stack_node *top = s->top;
*a = top->a;
mpz_set(b, top->b);
s->top = top->next;
s->size -= 1;
mpz_clear(top->b);
free(top);
return 1;
}
// Get the values of the top element
// return 1 if an element an top element exists
// return value is passed through value reference
int mpz_ui_pair_stack_top(mpz_ui_pair_stack *s, size_t *a, mpz_t b)
{
if (s == NULL || s->top == NULL) return 0;
*a = s->top->a;
mpz_set(b, s->top->b);
return 1;
}
int mpz_ui_pair_stack_top_a(mpz_ui_pair_stack *s, size_t *a) {
if (s == NULL || s->top == NULL) return 0;
*a = s->top->a;
return 1;
}