-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmore_aid_funcs.c
70 lines (63 loc) · 1.07 KB
/
more_aid_funcs.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
#include "monty.h"
/**
* push_end_func - function that pushes node to the end of the stack
* @top: stack top
* @data: stack data
* Return: 0 if successful else -1
*/
int push_end_func(stack_t **top, int data)
{
stack_t *new;
stack_t *ptr;
new = malloc(sizeof(stack_t));
if (new != NULL)
{
new->prev = NULL;
new->n = data;
new->next = NULL;
if (*top == NULL)
*top = new;
else
{
ptr = *top;
while (ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = new;
new->prev = ptr;
}
}
else
{
fprintf(stderr, "Error: malloc failed\n");
return (-1);
}
return (0);
}
/**
* pop_end_func - function to remove the bottom of the stack
* @top: stack top
* @line_number: file line number
* Return: 0 if successful else -1
*/
int pop_end_func(stack_t **top, unsigned int line_number)
{
stack_t *ptr;
if (*top != NULL)
{
ptr = *top;
while (ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->prev->next = NULL;
free(ptr);
}
else
{
fprintf(stderr, "L%d: can't pop an empty stack\n", line_number);
return (-1);
}
return (0);
}