-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
executable file
·68 lines (58 loc) · 1.3 KB
/
Copy pathstack.c
File metadata and controls
executable file
·68 lines (58 loc) · 1.3 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "monty.h"
void free_stack(stack_t **stack);
int init_stack(stack_t **stack);
int check_mode(stack_t *stack);
/**
* free_stack - Frees a stack_t stack.
*
* @stack: A pointer to the top(stack) or bottom(queue) of list.
*/
void free_stack(stack_t **stack)
{
stack_t *tmp = (*stack);
while (*stack)
{
tmp = (*stack)->next;
free(*stack);
*stack = tmp;
}
}
/**
* init_stack - Initialize a stack_t stack with
* beginning stack and ending queue.
*
* @stack: A pointer to an unitialized stack_t stack.
*
* Return: If an error occurs - EXIT_FAILURE.
* Otherwise - EXIT_SUCCESS.
*/
int init_stack(stack_t **stack)
{
stack_t *start;
start = malloc(sizeof(stack_t));
if (start == NULL)
return (malloc_error());
start->n = STACK;
start->prev = NULL;
start->next = NULL;
*stack = start;
return (EXIT_SUCCESS);
}
/**
* check_mode - Checks if a stack_t linked list is in stack or queue mode.
*
* @stack:A pointer to the top (stack) or
* bottom (queue) of a stack_t linked list.
*
* Return: If the stack_t is in stack mode - STACK (0).
* If the stack_t is in queue mode - QUEUE (1).
* Otherwise - 2.
*/
int check_mode(stack_t *stack)
{
if (stack->n == STACK)
return (STACK);
else if (stack->n == QUEUE)
return (QUEUE);
return (2);
}