-
Notifications
You must be signed in to change notification settings - Fork 0
/
generic-stack.c
129 lines (114 loc) · 2.63 KB
/
generic-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
/************************************************************************
*
* File name: generic-stack.c
* Description: Generic Stack's Implementation
* Author: dennis_fan(dennis_fan@outlook.com), 2013/2/26
* Version: 1.0
*************************************************************************/
#include "generic-stack.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MIN_STACK_SIZE (4)
/* Create a new stack */
Stack
stack_create(int elemsize, PfCbFree freefn)
{
Stack stk;
stk = malloc(sizeof(struct StackRecord));
if ( stk == NULL) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
stk->array = malloc(elemsize * MIN_STACK_SIZE);
if (stk->array == NULL) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
stk->elemsize = elemsize;
stk->loglength = 0;
stk->alloclength = MIN_STACK_SIZE;
}
/* Dispose the stack*/
void
stack_dispose(Stack stk)
{
stack_make_empty(stk);
free(stk->array);
free(stk);
}
/* Make the given stack empty*/
void
stack_make_empty(Stack stk)
{
if ( stk->freefn ) {
int i;
for ( i = 0; i < stk->loglength; ++i) {
stk->freefn((char *)stk->array +
i * stk->elemsize);
}
}
stk->loglength = 0;
}
/* Return true if the stack is empty*/
int
stack_is_empty(Stack stk)
{
return stk->loglength == 0;
}
static void
stack_grow(Stack stk)
{
stk->alloclength *= 2;
stk->array = realloc(stk->array,
stk->alloclength * stk->elemsize);
}
/* Insert a new element onto stack */
void
stack_push(Stack stk, ElementAddr elemaddr)
{
ElementAddr target;
if ( stk->loglength == stk->alloclength )
stack_grow(stk);
target = (char *)stk->array + stk->loglength * stk->elemsize;
memcpy(target, elemaddr, stk->elemsize);
stk->loglength++;
}
/* Delete the top element off the stack */
void
stack_pop(Stack stk)
{
ElementAddr target;
if ( stack_is_empty(stk) ) {
fprintf(stderr, "Empty stack\n");
exit(1);
}
if ( stk->freefn ) {
target = (char *)stk->array +
(stk->loglength-1) * stk->elemsize;
stk->freefn(target);
}
stk->loglength--;
}
/* Fetch the top element from the stack */
void
stack_top(Stack stk, ElementAddr elemaddr)
{
void *target = (char *)stk->array +
(stk->loglength-1) * stk->elemsize;
memcpy(elemaddr, target, stk->elemsize);
}
/* Fetch & Delete the top element from the stack */
void
stack_top_and_pop(Stack stk, ElementAddr elemaddr)
{
ElementAddr target;
if ( stack_is_empty(stk) ) {
fprintf(stderr, "Empty stack\n");
exit(1);
}
target = (char *)stk->array +
(stk->loglength-1) * stk->elemsize;
memcpy(elemaddr, target, stk->elemsize);
stk->loglength--;
}