-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalstack.c
83 lines (71 loc) · 2.25 KB
/
calstack.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
/* calstack.c
* Created by Wang Haoren
* --------------------------------
* Impletentation of the fundamental data structures
*/
#include "calstack.h"
#include <stdlib.h>
#include <string.h>
void Stack_new(Stack *stack) {
stack->head = malloc(CALSTACK_INIT_SIZE * sizeof(Numeric));
stack->tail = stack->head;
stack->capacity = CALSTACK_INIT_SIZE;
}
void Stack_free(Stack *stack) {
free(stack->head);
stack->head = stack->tail = NULL;
stack->capacity = 0;
}
void Stack_push(Stack *stack, const Numeric *value) {
if (stack->tail - stack->head == stack->capacity) {
Numeric *temp = malloc(CALSTACK_RESIZE_FACTOR * stack->capacity * sizeof(Numeric));
memcpy(temp, stack->head, stack->capacity * sizeof(Numeric));
free(stack->head);
stack->head = temp;
stack->tail = stack->head + stack->capacity;
stack->capacity *= CALSTACK_RESIZE_FACTOR;
}
Numeric* top=stack->tail++;
Numeric_new(top);
Numeric_assign(top, value);
}
void Stack_pop(Stack *stack, Numeric *address) {
Numeric_assign(address, --stack->tail);
Numeric_free(stack->tail);
}
size_t Stack_size(const Stack *stack) {
return stack->tail - stack->head;
}
void SStack_new(SStack *stack) {
stack->head = malloc(CALSTACK_INIT_SIZE * sizeof(Operation));
stack->tail = stack->head;
stack->capacity = CALSTACK_INIT_SIZE;
}
void SStack_free(SStack *stack) {
free(stack->head);
stack->head = stack->tail = NULL;
stack->capacity = 0;
}
void SStack_push(SStack *stack, Operation value) {
if (stack->tail - stack->head == stack->capacity) {
Operation *temp = malloc(CALSTACK_RESIZE_FACTOR * stack->capacity * sizeof(Operation));
memcpy(temp, stack->head, stack->capacity * sizeof(Operation));
free(stack->head);
stack->head = temp;
stack->tail = stack->head + stack->capacity;
stack->capacity *= CALSTACK_RESIZE_FACTOR;
}
*stack->tail++ = value;
}
Operation SStack_pop(SStack *stack) {
return *--stack->tail;
}
size_t SStack_size(const SStack *stack) {
return stack->tail - stack->head;
}
Operation SStack_top(const SStack *stack) {
return *(stack->tail-1);
}
bool SStack_empty(const SStack *stack) {
return stack->head == stack->tail;
}