-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.c
56 lines (53 loc) · 1 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
#include "stack.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
Stack_t * StackCreate(int size)
{
Stack_t *ns = (Stack_t *) malloc(sizeof(Stack_t));
assert(ns != NULL);
ns->size = size;
ns->top = 0;
ns->pElements = (int *) malloc(size * sizeof(int));
assert(ns->pElements);
return ns;
}
int StackPush(Stack_t *ps, int e)
{
if(ps->top == ps->size)
return 0;
ps->pElements[ps->top++] = e;
return 1;
}
int StackPop(Stack_t *ps, int *pe)
{
if(ps->top == 0)
return 0;
*pe = ps->pElements[--ps->top];
return 1;
}
int StackIsEmpty(Stack_t *ps)
{
return ps->top == 0;
}
void StackDestroy(Stack_t *ps)
{
free(ps->pElements);
free(ps);
}
void StackFilter(Stack_t_l *pStk, int e)
{
int t;
if(StackIsEmpty(pStk))
{
return;
}
StackPop(pStrk,&t);
StackFilter(pStrk,e);
if(t!=e)
{
StackPush(pStrk,t);
return;
}
return ;
}