-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
64 lines (57 loc) · 1.24 KB
/
stack.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#pragma execution_character_set("UTF-8")
#include <stdlib.h>
#include <stdio.h>
#include "stack.h"
/* 栈顶为0,表示不含元素 */
void stack_initialize(STACK** ppstack)
{
if(*ppstack == NULL)
*ppstack=(STACK*)calloc(1, sizeof(STACK));
(*ppstack)->top = 0;
(*ppstack)->capacity = 0;
(*ppstack)->buf = NULL;
}
void stack_push(STACK* pstack, int elem)
{
int* newbuf = NULL;
if(pstack==NULL)
return;
if(pstack->top == pstack->capacity) //the stack if full
{
newbuf = (int*)realloc(pstack->buf, (pstack->capacity+INC_STACK)*sizeof(int));
if(newbuf==NULL)
{
fprintf(stderr, "Error reallocating memory\n");
return;
}
pstack->buf = newbuf;
pstack->capacity += INC_STACK;
}
*(pstack->buf + pstack->top++) = elem;
}
/* stat表示pop成功与否,若为1则成功,为0则失败。 返回pop的值 */
int stack_pop(STACK* pstack, int* stat)
{
if(pstack==NULL ||pstack->top==0)
{
*stat = 0; //pop error
return 0;
}
*stat = 1;//pop an element successfully
return *(pstack->buf+(--(pstack->top)));
}
void stack_destroy(STACK** ppstack)
{
if(*ppstack)
{
free((*ppstack)->buf);
free(*ppstack);
*ppstack = NULL;
}
}
int stack_isempty(const STACK* pstack)
{
if(pstack == NULL)
return 1;
return (pstack->top>0)? 0:1;
}