forked from abysamross/C_NIX_Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmstack.h
60 lines (45 loc) · 1.26 KB
/
mstack.h
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
#include <stdlib.h>
#ifndef _MSTACK_H_
#define _MSTACK_H_
#ifndef typeof
#define typeof(T) __typeof__(T)
#endif
#define push(S, T, D) do { \
typeof(stack*) s = S; \
typeof(T) a = D; \
typeof(T)* ap = malloc(sizeof(T)); \
*ap = a; \
s->push1(s, (void*)ap); \
} while (0)
#define pop(S) ({ \
typeof(stack*) s = S; \
s->pop1(s);})
#define top(S) ({ \
typeof(stack*) s = S; \
s->top1(s);})
#define size(S) ({ \
typeof(stack*) s = S; \
s->size1(s);})
#define empty(S) ({ \
typeof(stack*) s = S; \
s->empty1(s);})
typedef struct stElem {
void* val;
struct stElem* nx;
struct stElem* pv;
} stElem;
typedef struct stack {
const char* type;
void* val;
stElem* sp;
stElem* bp;
int nsize;
int (*empty1) (struct stack*);
void* (*top1) (struct stack*);
void (*pop1) (struct stack*);
void (*push1) (struct stack*, void*);
int (*size1) (struct stack*);
} stack;
stack* initStack();
void freeStack(stack*);
#endif // _MSTACK_H_