forked from gzhang91/data_struct-study
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack.c
More file actions
executable file
·46 lines (39 loc) · 838 Bytes
/
stack.c
File metadata and controls
executable file
·46 lines (39 loc) · 838 Bytes
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
#include "stack.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
Stack *StackCreate(int size, int n) {
Stack *stack = (Stack *)malloc(sizeof(Stack));
if (!stack) {
printf("malloc failed\n");
exit(-1);
}
stack->data = (void *)malloc(size * n);
stack->size = size;
stack->n = n;
stack->top = -1;
}
void StackRelease(Stack *st) {
if (st) {
free(st);
free(st->data);
}
}
int StackPush(Stack *st, const void *data) {
if (st->top == st->n - 1) {
printf("栈已经满了\n");
return 0;
}
st->top ++;
memcpy(st->data + st->top * st->size, data, st->size);
return 1;
}
int StackPop(Stack *st, void *data) {
if (st->top < 0) {
printf("栈已经空了\n");
return 0;
}
memcpy(data, st->data + st->top * st->size, st->size);
st->top --;
return 1;
}