source: branches/1.3/stack.c @ 593

Last change on this file since 593 was 593, checked in by moo, 5 years ago

merged r304,r305,r306,r307 from trunk

  • Property svn:eol-style set to native
File size: 1.1 KB
RevLine 
[1]1#include <stdlib.h>
2#include <assert.h>
3#include "stack.h"
4typedef xc_stack_t* S;
5
[593]6void xc_stack_init_ex(S stack, int initsize)
[1]7{
8    stack->cnt = 0;
[593]9    stack->size = initsize;
[1]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
[593]45int xc_stack_count(S stack)
[1]46{
47    assert(stack != NULL);
48    return stack->cnt;
49}
50
51void xc_stack_reverse(S stack)
52{
[11]53    int i, j;
[1]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.