-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathint_stack.c
More file actions
executable file
·75 lines (62 loc) · 1.91 KB
/
int_stack.c
File metadata and controls
executable file
·75 lines (62 loc) · 1.91 KB
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
71
72
73
74
75
#include "int_stack.h"
/* Initializes a stack. */
IntStack* init_int_stack(unsigned int capacity) {
IntStack *stack = (IntStack*) malloc(sizeof(IntStack));
stack->capacity = capacity;
stack->size = 0;
stack->stack = (int*) malloc(sizeof(int)*capacity);
if (stack == NULL) {
perror("Error creating stack.\n");
return NULL;
}
return stack;
}
/* Couple of useful functions */
int int_stack_is_empty(IntStack *stack) { return (stack->size == 0) ? 1 : 0; }
int int_stack_is_full(IntStack *stack) { return (stack->size == stack->capacity) ? 1 : 0; }
/* Push function - adds value to stack */
void int_stack_push(IntStack *stack, int num) {
if (int_stack_is_full(stack)) {
perror("Stack size would exceed capacity.\n");
return;
}
int i = stack->size;
stack->stack[i] = num;
stack->size++;
}
/* Pop function - pops element from stack */
int int_stack_pop(IntStack *stack) {
if (int_stack_is_empty(stack)) {
perror("Stack size is 0, cannot pop.\n");
return -1;
}
int i = stack->size-1;
int num = stack->stack[i];
stack->size--;
return num;
}
/* Returns the element at the top without popping it */
int int_stack_peek(IntStack *stack) {
if (int_stack_is_empty(stack)) {
perror("Stack size is 0, cannot peek.\n");
return -1;
}
int i = stack->size-1;
int num = stack->stack[i];
return num;
}
/* Prints a stack structure */
void print_int_stack(IntStack *stack, int is_char) {
printf("Stack: size=%d, capacity=%d [int]\n", stack->size, stack->capacity);
int size = stack->size;
int i;
if (is_char) {
for (i=0; i<size; i++) {
printf(" %c\n", (char) stack->stack[i]);
}
} else {
for (i=0; i<size; i++) {
printf(" %d\n", stack->stack[i]);
}
}
}