-
Notifications
You must be signed in to change notification settings - Fork 0
/
op_push.c
64 lines (55 loc) · 1.24 KB
/
op_push.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
#include "monty.h"
/**
* push - Adds a new node at the beginning of the stack
* @stack: The head of the stack
* @param: The value to adds on the stack
*
* Return: Nothing
*/
void push(stack_t **stack, unsigned int param)
{
stack_t *new_node = NULL;
new_node = malloc(sizeof(stack_t));
if (new_node == NULL)
handle_error(ERR_BAD_MALL, NULL, 0, NULL);
new_node->n = param;
if (*stack)
{
new_node->next = *stack;
new_node->prev = (*stack)->prev;
(*stack)->prev = new_node;
*stack = new_node;
return;
}
new_node->next = *stack;
new_node->prev = NULL;
*stack = new_node;
}
/**
* push_queue - Adds a new node at the end of the stack
* @stack: The head of the stack
* @param: The value to adds on the stack
*
* Return: Nothing
*/
void push_queue(stack_t **stack, unsigned int param)
{
stack_t *current = NULL, *new_node = NULL;
new_node = malloc(sizeof(stack_t));
if (new_node == NULL)
handle_error(ERR_BAD_MALL, NULL, 0, NULL);
new_node->n = param;
if (*stack)
{
current = *stack;
while (current->next != NULL)
current = current->next;
new_node->next = NULL;
new_node->prev = current;
current->next = new_node;
return;
}
new_node->next = *stack;
new_node->prev = NULL;
*stack = new_node;
}