-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintStack.c
More file actions
50 lines (42 loc) · 874 Bytes
/
intStack.c
File metadata and controls
50 lines (42 loc) · 874 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
#include <stdio.h>
#include <stdlib.h>
#define EMPTY 0
#define INITIAL_CAPACITY 8
typedef struct intStack {
int size; //no of elements
int data[INITIAL_CAPACITY]; //fixed array
} stack;
void initialise(stack *s) {
s->size = 0;
}
void push(stack *s, int value) {
if (s->size == INITIAL_CAPACITY) {
printf("Warning: Cannot push, stack is full!\n");
return;
}
s->data[s->size] = value;
s->size++;
}
void pop(stack *s) {
if (s->size == 0) {
printf("Warning: Stack empty!\n");
return;
}
s->size--;
}
int top(stack *s) {
if (s->size == 0) {
printf("Warning: Stack empty!\n");
return INT32_MIN;
}
return s->data[s->size - 1];
}
int isEmpty(stack *s) {
return s->size ? 0 : 1;
}
int size(stack *s) {
return s->size;
}
void freeStack(stack *s) {
initialise(s);
}