Subversion Repositories SvarDOS

Rev

Rev 2020 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2019 mateusz.vi 1
/*
2
 *  STACK.C - Stack management functions.
3
 *  Written 1995,96 by Andrew Clarke and released to the public domain.
4
 *
5
 *  Slightly modified for inclusion in pdTree
6
 *  by Jeremy Davis <jeremyd@computer.org> Jan. 2001
7
 */
8
 
9
#include <stdlib.h>  // for malloc
10
#include "stack.h"
11
 
12
// initialize stack structure to known initial values.
13
void stackDefaults(STACK * p_stack)
14
{
15
    p_stack->p_top = NULL;
16
    p_stack->items = 0L;
17
}
18
 
19
#if defined __BORLANDC__ || __TURBOC__
20
#pragma argsused
21
#endif
22
// place any extra stack initialization here
23
void stackInit(STACK * p_stack)
24
{
2077 bernd.boec 25
    (void)p_stack;
2019 mateusz.vi 26
}
27
 
28
// ensures stack is empty and all allocated memory freed
29
void stackTerm(STACK * p_stack)
30
{
31
    while (!stackIsEmpty(p_stack)) 
32
    {
33
        stackPopItem(p_stack);
34
    }
35
}
36
 
37
// add an item to the stack, allocating memory for STACKNODE
38
int stackPushItem(STACK * p_stack, void *p_item)
39
{
40
    STACKNODE *p_node;
41
    p_node = malloc(sizeof(STACKNODE));
42
    if (!p_node)
43
        return 0;
44
    p_node->p_item = p_item;
45
    if (!p_stack->p_top)
46
        p_node->p_prev = NULL;
47
    else
48
        p_node->p_prev = p_stack->p_top;
49
    p_stack->p_top = p_node;
50
    p_stack->items++;
51
    return 1;
52
}
53
 
54
// removes item from the stack, freeing memory allocated for STACKNODE
55
void *stackPopItem(STACK * p_stack)
56
{
57
    void *p_item;
58
    STACKNODE *p_node;
59
    if (!p_stack->items)
60
        return NULL;
61
    p_item = p_stack->p_top->p_item;
62
    p_node = p_stack->p_top;
63
    p_stack->p_top = p_stack->p_top->p_prev;
64
    free(p_node);
65
    p_stack->items--;
66
    return p_item;
67
}
68
 
69
// returns the current size of (items on) the stack
70
unsigned long stackTotalItems(STACK * p_stack)
71
{
72
    return p_stack->items;
73
}
74
 
75
// returns true if the stack is empty, nonzero otherwise
76
int stackIsEmpty(STACK * p_stack)
77
{
78
    return p_stack->items == 0;
79
}
80
 
81
 
82
 
83
/*
84
 *  The rest of this file is a sample program to show how
85
 *  to use the stack functions and also to test the stack functions.
86
 *
87
 */
88
 
89
#ifdef TEST_STACK
90
 
91
#include <stdio.h>
92
 
93
int main(void)
94
{
95
    STACK p_stack;
96
    char *str;
97
 
98
    stackDefaults(&p_stack);
99
    stackInit(&p_stack);
100
    stackPushItem(&p_stack, "One banana");
101
    stackPushItem(&p_stack, "Two banana");
102
    stackPushItem(&p_stack, "Three banana");
103
    stackPushItem(&p_stack, "Four banana");
104
    stackPushItem(&p_stack, "Five banana");
105
 
106
    str = stackPopItem(&p_stack);
107
    while (str) 
108
    {
109
        printf("%s\n", str);
110
        str = stackPopItem(&p_stack);
111
    }
112
    stackTerm(&p_stack);
113
 
114
    return 0;
115
}
116
 
117
#endif
118