-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackExample.c
More file actions
56 lines (49 loc) · 862 Bytes
/
Copy pathStackExample.c
File metadata and controls
56 lines (49 loc) · 862 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
47
48
49
50
51
52
53
54
55
56
//stack example
# include <stdio.h>
# define STACK_MAX 100
typedef struct
{
int data[STACK_MAX];
int size;
}Stack;
void Stack_Init(Stack *S)
{
S->size=0;
}
int Stack_Top(Stack *S)
{
if(S->size == 0){
fprintf(stderr, "Error: stack empty\n");
return -1;
}
return S->data[S->size - 1];
}
void Stack_Push(Stack *S, int a)
{
if(S->size >= STACK_MAX-1){
fprintf(stderr, "Error: stack full\n");
}else{
S->data[S->size] = a;
S->size++;
}
}
void Stack_Pop(Stack *S)
{
if(S->size > 0){
S->size--;
}else{
fprintf(stderr, "Error: stack empty\n");
}
}
int main()
{
Stack S;
Stack_Init(&S);
Stack_Push(&S, 5);
Stack_Push(&S, 6);
Stack_Push(&S, 7);
Stack_Pop(&S);
printf("top is %d\n", Stack_Top(&S));
printf("%d", S.data[S.size-2]);
return 0;
}