-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
37 lines (30 loc) · 687 Bytes
/
stack.c
File metadata and controls
37 lines (30 loc) · 687 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
//
// Created by sdutton on 09.05.23.
//
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
// Initialize stack
pStack initStack() {
pStack stack = malloc(sizeof (struct Stack));
stack->top = -1;
push(stack, stack);
return stack;
}
// Push an item onto the stack
int push(struct Stack* stack, void* item) {
if (stack->top == STACK_SIZE - 1) {
printf("Stack is full.\n");
return -1;
}
stack->data[++stack->top] = item;
return 0;
}
// Pop an item from the stack
void* pop(struct Stack* stack) {
if (stack->top == -1) {
printf("Stack is empty.\n");
return NULL;
}
return stack->data[stack->top--];
}