Subversion Repositories SvarDOS

Rev

Go to most recent revision | Details | 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
{
25
}
26
 
27
// ensures stack is empty and all allocated memory freed
28
void stackTerm(STACK * p_stack)
29
{
30
    while (!stackIsEmpty(p_stack)) 
31
    {
32
        stackPopItem(p_stack);
33
    }
34
}
35
 
36
// add an item to the stack, allocating memory for STACKNODE
37
int stackPushItem(STACK * p_stack, void *p_item)
38
{
39
    STACKNODE *p_node;
40
    p_node = malloc(sizeof(STACKNODE));
41
    if (!p_node)
42
        return 0;
43
    p_node->p_item = p_item;
44
    if (!p_stack->p_top)
45
        p_node->p_prev = NULL;
46
    else
47
        p_node->p_prev = p_stack->p_top;
48
    p_stack->p_top = p_node;
49
    p_stack->items++;
50
    return 1;
51
}
52
 
53
// removes item from the stack, freeing memory allocated for STACKNODE
54
void *stackPopItem(STACK * p_stack)
55
{
56
    void *p_item;
57
    STACKNODE *p_node;
58
    if (!p_stack->items)
59
        return NULL;
60
    p_item = p_stack->p_top->p_item;
61
    p_node = p_stack->p_top;
62
    p_stack->p_top = p_stack->p_top->p_prev;
63
    free(p_node);
64
    p_stack->items--;
65
    return p_item;
66
}
67
 
68
// returns the current size of (items on) the stack
69
unsigned long stackTotalItems(STACK * p_stack)
70
{
71
    return p_stack->items;
72
}
73
 
74
// returns true if the stack is empty, nonzero otherwise
75
int stackIsEmpty(STACK * p_stack)
76
{
77
    return p_stack->items == 0;
78
}
79
 
80
 
81
 
82
/*
83
 *  The rest of this file is a sample program to show how
84
 *  to use the stack functions and also to test the stack functions.
85
 *
86
 */
87
 
88
#ifdef TEST_STACK
89
 
90
#include <stdio.h>
91
 
92
int main(void)
93
{
94
    STACK p_stack;
95
    char *str;
96
 
97
    stackDefaults(&p_stack);
98
    stackInit(&p_stack);
99
    stackPushItem(&p_stack, "One banana");
100
    stackPushItem(&p_stack, "Two banana");
101
    stackPushItem(&p_stack, "Three banana");
102
    stackPushItem(&p_stack, "Four banana");
103
    stackPushItem(&p_stack, "Five banana");
104
 
105
    str = stackPopItem(&p_stack);
106
    while (str) 
107
    {
108
        printf("%s\n", str);
109
        str = stackPopItem(&p_stack);
110
    }
111
    stackTerm(&p_stack);
112
 
113
    return 0;
114
}
115
 
116
#endif
117