-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
72 lines (62 loc) · 1.37 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
/*
* File: stack.c
* Auth: Faith Nyaberi
* Peter Ochieng
*/
#include "monty.h"
#include <string.h>
void free_stack(stack_t **stack);
int init_stack(stack_t **stack);
int check_mode(stack_t *stack);
/**
* free_stack - Frees the stack_t stack.
* @stack: A pointer to the top (stack) or
* bottom (queue) of a stack_t.
*/
void free_stack(stack_t **stack)
{
stack_t *tmp = *stack;
while (*stack)
{
tmp = (*stack)->next;
free(*stack);
*stack = tmp;
}
}
/**
* init_stack - Initializes a stack_t stack with beginning
* stack and ending queue nodes.
* @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 *s;
s = malloc(sizeof(stack_t));
if (s == NULL)
return (malloc_error());
s->n = STACK;
s->prev = NULL;
s->next = NULL;
*stack = s;
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);
}