-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush_and_pop.c
58 lines (50 loc) · 944 Bytes
/
push_and_pop.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
#include "monty.h"
/*push and pop functions*/
/**
* push - add an element to the stack/queue
* @h: pointer to dll
* @line: pointer to line
* @l: line number
*/
void push(stack_t **h, char *line, unsigned int l)
{
char *start_n;
stack_t *node;
start_n = reach_number(line);
if (start_n == NULL)
{
fprintf(stderr, "L%d: usage: push integer\n", l);
free(line);
free(*h);
*h = NULL;
exit(EXIT_FAILURE);
};
node = add_node(h, atoi(start_n));
free(line);
if (node == NULL)
{
puts("Error: malloc failed");
free_stack(*h);
exit(EXIT_FAILURE);
}
}
/**
* pop - pull an element from the stack or queue
* @h: pointer to dll
* @l: line number
*/
void pop(stack_t **h, unsigned int l)
{
stack_t *node;
if (_strcmp(flag, "stack") == 0)
node = pop_s(h);
else
node = dequeue(h);
if (node == NULL)
{
printf("L%d: can't pop an empty %s\n", l, flag);
free_stack(*h);
exit(EXIT_FAILURE);
}
free(node);
}