-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
41 lines (34 loc) · 847 Bytes
/
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
#include "expr.h"
#include "stack.h"
#include <stdio.h>
#include <stdlib.h>
struct Stack* create_stack(unsigned capacity)
{
struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));
stack->capacity = capacity;
stack->top = -1;
stack->array = malloc(stack->capacity * sizeof(void*));
return stack;
}
int is_full(struct Stack* stack)
{ return stack->top == stack->capacity - 1; }
int is_empty(struct Stack* stack)
{ return stack->top == -1; }
void push(struct Stack* stack, void* item)
{
if (is_full(stack))
return;
stack->array[++stack->top] = item;
}
void* pop(struct Stack* stack)
{
if (is_empty(stack))
return NULL;
return stack->array[stack->top--];
}
void* top(struct Stack* stack)
{
if (is_empty(stack))
return NULL;
return stack->array[stack->top];
}