-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
101 lines (85 loc) · 2.26 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
/* SQUID - A C function library for biological sequence analysis
* Copyright (C) 1992-1995 Sean R. Eddy
*
* This source code is distributed under terms of the
* GNU General Public License. See the files COPYING
* and GNULICENSE for further details.
*
*/
/* stack.c
* SRE, Thu Mar 3 10:08:48 1994
*
* Implementation of generic stack structures.
*/
#include <stdlib.h>
#include "squid.h"
#ifdef MEMDEBUG
#include "dbmalloc.h"
#endif
/************************************************************
* intstack_s implementation.
*
* Functions: InitIntStack() - returns ptr to new stack
* PushIntStack() - (void)
* PopIntStack() - returns 1 on success, 0 if stack empty
* FreeIntStack() - returns number of elements free'd, or 0 if
* stack was empty.
*
* Implementation of the pushdown stack for storing single
* integers.
*************************************************************/
struct intstack_s *
InitIntStack(void)
{
struct intstack_s *stack;
if ((stack = (struct intstack_s *) malloc (sizeof(struct intstack_s))) == NULL)
Die("Memory allocation failure at %s line %d", __FILE__, __LINE__);
stack->nxt = NULL;
return stack;
}
void
PushIntStack(struct intstack_s *stack, int data)
{
struct intstack_s *new;
if ((new = (struct intstack_s *) malloc (sizeof(struct intstack_s))) == NULL)
Die("Memory allocation failure at %s line %d", __FILE__, __LINE__);
new->data = data;
new->nxt = stack->nxt;
stack->nxt = new;
}
int
PopIntStack(struct intstack_s *stack, int *ret_data)
{
struct intstack_s *old;
if (stack->nxt == NULL) return 0;
old = stack->nxt;
stack->nxt = old->nxt;
*ret_data = old->data;
free(old);
return 1;
}
void
ReverseIntStack(struct intstack_s *stack)
{
struct intstack_s *old;
struct intstack_s *new;
old = stack->nxt;
stack->nxt = NULL;
while (old != NULL)
{
new = old; /* remove one from top of old stack */
old = old->nxt;
new->nxt = stack->nxt; /* push it onto new stack */
stack->nxt = new;
}
}
int
FreeIntStack( struct intstack_s *stack )
{
int data;
int count = 0;
while (PopIntStack(stack, &data))
count++;
free(stack);
return count;
}