forked from lindoran/tnycalc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloatstack.c
More file actions
58 lines (42 loc) · 1.11 KB
/
Copy pathfloatstack.c
File metadata and controls
58 lines (42 loc) · 1.11 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
#include <cmoc.h>
#include "floatstack.h"
#include "errors.h"
void doErrors(char err);
// Function to initialize the stack
void initialize(Stack *stack) {
stack->top = -1; // Initialize top index to -1 (empty stack)
}
int countElements(Stack *stack) {
// Add 1 to the top index to get the count of elements
return stack->top + 1;
}
// Function to check if the stack is empty
int isEmpty(Stack *stack) {
return stack->top == -1;
}
// Function to check if the stack is full
int isFull(Stack *stack) {
return stack->top == MAX_SIZE - 1;
}
// Function to push an element onto the stack
void push(Stack *stack, float value) {
if (isFull(stack)) {
doErrors(IS_STACK_OF);
return;
}
stack->items[++stack->top] = value;
}
// Function to pop an element from the stack
float pop(Stack *stack) {
if (isEmpty(stack)) {
doErrors(IS_STACK_UF);
}
return stack->items[stack->top--];
}
// Function to peek the top element of the stack
float peek(Stack *stack) {
if (isEmpty(stack)) {
doErrors(IS_EMT_STACK);
}
return stack->items[stack->top];
}