-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenqueue.c
More file actions
39 lines (38 loc) · 750 Bytes
/
enqueue.c
File metadata and controls
39 lines (38 loc) · 750 Bytes
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
#include "monty.h"
/**
* enqueue - adds element to end of the list
* @stack: top of the node
* @line_number: line number
*/
void enqueue(stack_t **stack, unsigned int line_number)
{
stack_t *new, *temp = *stack;
if (!myglob.arg)
{
dprintf(2, "L%d: usage: push integer\n", line_number);
exit(EXIT_FAILURE);
}
if (are_digits(myglob.arg) == 1)
{
dprintf(2, "L%d: usage: push integer\n", line_number);
exit(EXIT_FAILURE);
}
new = malloc(sizeof(stack_t));
if (!new)
{
fprintf(stderr, "Error: malloc failed\n");
exit(EXIT_FAILURE);
}
new->next = NULL;
new->n = atoi(myglob.arg);
if (!*stack)
{
new->prev = NULL;
*stack = new;
return;
}
while (temp->next)
temp = temp->next;
temp->next = new;
new->prev = temp;
}