-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
70 lines (57 loc) · 1.38 KB
/
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
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
69
70
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
/*
*
* Dynamic stack implementation
*
*/
//initial stack size
int INITIAL_SIZE = 10;
int current_size;
int *stack;
int top = -1;
//Initializes the stack by allocating memory
void stack_init() {
stack = (int*)malloc(INITIAL_SIZE * sizeof(int));
current_size = INITIAL_SIZE;
if (stack == NULL) {
fprintf(stderr, "Failed to allocate memory!\n");
exit(EXIT_FAILURE);
}
}
//pushes a number to the top of the stack
void stack_push(int number) {
//allocate more memory if there's no more space
if ((current_size - 1) <= top) {
current_size = current_size * 2;
stack = (int*)realloc(stack, current_size * sizeof(int));
}
top++;
stack[top] = number;
}
//removes the number from the top of the stack and returns it
int stack_pop() {
if (!stack_isEmpty()) {
int ret = stack[top];
top--;
return ret;
}else {
fprintf(stderr, "Could not pop from stack because it's empty!\n");
}
}
//returns the number from the top of the stack
int stack_peek() {
if (!stack_isEmpty()) {
return stack[top];
}else {
fprintf(stderr, "Could not peek from stack because it's empty!\n");
}
}
int stack_isEmpty() {
return top == -1;
}
//Free the memory allocated to the stack
void stack_destruct() {
free(stack);
}