-
Notifications
You must be signed in to change notification settings - Fork 22
/
stack.c
59 lines (52 loc) · 1.19 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
#include <stdlib.h>
#include <stdio.h>
#include "stackconfig.h"
#include "stack.h"
void push(Stack* stack, stackType data) {
Node* newNode = malloc(sizeof(Node));
if(newNode == NULL) {
printf("Stack overflow while push()");
return;
}
newNode->data = data;
newNode->next = stack->top;
stack->top = newNode;
}
stackType pop(Stack* stack) {
if(stack->top == NULL) {
printf("Stack underflow while pop\n");
return -1;
}
Node* next = stack->top->next;
stackType data = stack->top->data;
free(stack->top);
stack->top = next;
return data;
}
stackType peek(Stack* stack) {
if(stack->top == NULL) {
printf("Stack underflow while pop\n");
return -1;
}
stackType data = stack->top->data;
return data;
}
int isEmpty(Stack* stack) {
return stack->top == NULL;
}
int isFull(Stack* stack) {
Node* newNode = malloc(sizeof(Node));
if(newNode == NULL) {
return 1;
}
free(newNode);
return 0;
}
void print(Stack* stack) {
Node* current = stack->top;
while(current != NULL) {
printf(TYPE_FMT" ", current->data);
current = current->next;
}
printf("\n");
}