~ubuntu-branches/ubuntu/trusty/hexcurse/trusty

« back to all changes in this revision

Viewing changes to src/stack.c

  • Committer: Bazaar Package Importer
  • Author(s): RISKO Gergely
  • Date: 2002-03-29 12:24:14 UTC
  • Revision ID: james.westby@ubuntu.com-20020329122414-rh3gzqb6aslm4fgp
Tags: 1.40-1
new upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "hex.h"
 
2
 
 
3
/******************************************************\
 
4
 * Description: the just sets the stack pointer to    *
 
5
 *              NULL; it really has no business being *
 
6
 *              function anyways                      *
 
7
\******************************************************/
 
8
void createStack(hexStack *stack)
 
9
{
 
10
    /* calloc() is used because it NULLS out all returned memory */
 
11
    /* stack = (hexStack *) calloc(1, sizeof(hexStack));
 
12
    stack->llist = NULL;
 
13
    stack->prev = NULL;
 
14
    */
 
15
    stack = NULL;
 
16
    stack->llist = NULL;
 
17
    stack->prev = NULL;
 
18
}
 
19
 
 
20
/******************************************************\
 
21
 * Description: this pushes a structure of type       *
 
22
 *              hexStack to the stack.  NULL being the*
 
23
 *              initial state of the stack            *
 
24
\******************************************************/
 
25
void pushStack(hexStack **stack, hexStack *tmpStack)
 
26
{
 
27
    hexStack *oldStack;
 
28
    oldStack = *stack;
 
29
 
 
30
    if (oldStack == NULL)                               /* begining of stack  */
 
31
        *stack = tmpStack;
 
32
    else 
 
33
    {
 
34
        tmpStack->prev = oldStack;
 
35
        *stack = tmpStack;
 
36
 
 
37
    }
 
38
}
 
39
 
 
40
 
 
41
/******************************************************\
 
42
 * Description: This function pops a structure off of *
 
43
 *              the stack and then free's it's        *
 
44
 *              previously allocated memory.  If the  *
 
45
 *              stack == NULL that means the bottom of*
 
46
 *              the stack has been reached.           *
 
47
\******************************************************/
 
48
void popStack(hexStack **stack)
 
49
{
 
50
    hexStack *tmpStack = *stack; 
 
51
 
 
52
    if (tmpStack != NULL)
 
53
    {                                           /* set to prev val in llist   */
 
54
        *stack = tmpStack->prev;                /* pop off the stack          */
 
55
        free(tmpStack);                         /* free allocated memory      */
 
56
    }
 
57
    
 
58
}
 
59
 
 
60
/******************************************************\
 
61
 * Description: This will pop off all values from the *
 
62
 *              stack and wil free all the allocated  *
 
63
 *              memory avoiding potential memory leaks*
 
64
\******************************************************/
 
65
void smashDaStack(hexStack **stack)
 
66
{
 
67
    hexStack *tmpStack = *stack; 
 
68
 
 
69
    while (tmpStack != NULL)
 
70
    {
 
71
        *stack = tmpStack->prev;
 
72
        free(tmpStack);
 
73
        tmpStack = *stack;
 
74
    }
 
75
}