source: branches/1.2/stack.c @ 394

Last change on this file since 394 was 394, checked in by moo, 8 years ago

merged [393] from trunk: svn:eol-style

  • Property svn:eol-style set to native
File size: 1.1 KB
Line 
1#include <stdlib.h>
2#include <assert.h>
3#include "stack.h"
4typedef xc_stack_t* S;
5
6void xc_stack_init(S stack)
7{
8    stack->cnt = 0;
9    stack->size = 8;
10    stack->data = malloc(sizeof(void *) * stack->size);
11}
12
13void xc_stack_destroy(S stack)
14{
15    free(stack->data);
16}
17
18void xc_stack_push(S stack, void *item)
19{
20    if (stack->cnt == stack->size) {
21        stack->size <<= 1;
22        stack->data = realloc(stack->data, sizeof(void *) * stack->size);
23    }
24    stack->data[stack->cnt++] = item;
25}
26
27void* xc_stack_pop(S stack)
28{
29    assert(stack != NULL && stack->size > 0);
30    return stack->data[--stack->cnt];
31}
32
33void* xc_stack_top(S stack)
34{
35    assert(stack != NULL && stack->cnt > 0);
36    return stack->data[stack->cnt-1];
37}
38
39void* xc_stack_get(S stack, int n)
40{
41    assert(stack != NULL && stack->cnt > 0);
42    return stack->data[n];
43}
44
45int xc_stack_size(S stack)
46{
47    assert(stack != NULL);
48    return stack->cnt;
49}
50
51void xc_stack_reverse(S stack)
52{
53    int i, j;
54    void *tmp;
55
56    assert(stack != NULL);
57    for (i = 0, j = stack->cnt - 1; i < j; i ++, j --) {
58        tmp = stack->data[i];
59        stack->data[i] = stack->data[j];
60        stack->data[j] = tmp;
61    }
62}
Note: See TracBrowser for help on using the repository browser.