-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
85 lines (68 loc) · 1.15 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
#include "stack.h"
// I feel that all of these are pretty self-evident,
// if one has the general idea what stacks do.
void stack_init( stack **head )
{
(*head) = NULL;
}
int stack_is_empty( stack *head )
{
if ( head == NULL )
{
return 1;
}
else
{
return 0;
}
}
int stack_push( stack **head, char *val, FILE *fptr, int ln )
{
size_t length = strlen( val );
stack *n = ( stack* ) malloc( sizeof( stack ) );
if ( n != NULL )
{
n->filename = ( char* ) malloc( sizeof( char ) * ( length + 1 ) );
if ( n->filename != NULL )
{
strncpy( n->filename, val, length );
(n->filename)[length] = '\0';
n->file_ptr = fptr;
n->line_num = ln;
n->w_include = 1;
n->w_preprocdirective = 1;
n->w_numberformats = 1;
n->w_digraphs = 1;
n->next = (*head);
(*head) = n;
return 0;
}
else
{
free( n );
return 1;
}
}
else
{
return 1;
}
}
void stack_pop( stack **head )
{
stack *n = NULL;
if ( (*head) != NULL )
{
n = (*head);
(*head) = n->next;
free( n->filename );
free( n );
}
}
void stack_destruct( stack **head )
{
while ( !stack_is_empty( (*head) ) )
{
stack_pop( head );
}
}