-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
81 lines (71 loc) · 1.55 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
71
72
73
74
75
76
77
78
79
80
81
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stack.h"
Stack *stack_create(void)
{
Stack *s;
if ((s = calloc(1, sizeof(Stack))) == NULL)
{
perror("malloc");
exit(errno);
}
s->top = NULL;
s->size = 0;
return s;
}
void stack_destroy(Stack *s)
{
while (s->top != NULL)
{
stack_pop(s);
}
free(s);
}
void stack_push(Stack *s, char *str)
{
/* Allocate memory for new data entry */
stack_data *new_data;
if ((new_data = malloc(sizeof(*new_data))) == NULL)
{
perror("malloc");
exit(errno);
}
if ((new_data->data = malloc(sizeof(char) * strlen(str) + 1)) == NULL)
{
perror("malloc");
exit(errno);
}
/* Copy string and redirect pointers */
strcpy(new_data->data, str);
new_data->prev = s->top;
s->top = new_data;
s->size++;
}
char *stack_pop(Stack *s)
{
/* Allocate temp memory for string and copy it */
char *tmp_string;
if ((tmp_string = malloc(sizeof(char) * strlen(s->top->data) + 1)) == NULL)
{
perror("malloc");
exit(errno);
}
strcpy(tmp_string, s->top->data);
/* Redirect pointers */
stack_data *tmp_data = s->top;
s->top = s->top->prev;
s->size--;
/* Free memory of popped entry */
free(tmp_data->data);
free(tmp_data);
return tmp_string;
}
bool stack_is_empty(const Stack *s)
{
if (s->size == 0)
{
return true;
}
return false;
}