-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.cpp
executable file
·68 lines (58 loc) · 1.05 KB
/
Stack.cpp
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
65
66
67
68
/*
* File Name: Stack.cpp
* Author: sunowsir
* Mail: [email protected]
* Created Time: 2018年10月22日 星期一 16时53分08秒
*/
#include <cstdio>
#include <cstdlib>
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
typedef struct Stack {
int *data;
int top, size;
} Stack;
Stack *init(int size) {
Stack *s = (Stack *)malloc(sizeof(Stack));
s->data = (int *)malloc(sizeof(int) * size);
s->top = -1;
s->size = size;
return s;
}
bool empty(Stack *s) {
return s->top == -1;
}
bool push(Stack *s, int value) {
if (s->top + 1 >= s->size) {
return false;
}
s->top += 1;
s->data[s->top] = value;
return true;
}
bool pop(Stack *s) {
if (empty(s)) {
return false;
}
s->top -= 1;
return true;
}
int top(Stack *s) {
if (empty(s)) {
return -1;
}
return s->data[s->top];
}
void clear(Stack *s) {
if (s == NULL) {
return ;
}
free(s->data);
free(s);
return ;
}
int main() {
return 0;
}