-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperations.c
executable file
·128 lines (120 loc) · 2.22 KB
/
operations.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "monty.h"
/**
* push - add node in stack_t
* @value: value's node
* @strct: strct_t
* Return: (strct)
*/
strct_t push(char *value, strct_t strct)
{
register int n;
stack_t *new, *current = strct.stack;
char *ptr = NULL;
ptr = strchr(value, '\n');
if (ptr)
*ptr = 0;
if (!strlen(value) || !_isdigit(value))
{
dprintf(STDERR_FILENO, "L%d: usage: push integer\n", strct.line_number);
fclose(strct.file);
free(strct.line);
free_stck(strct.stack);
exit(EXIT_FAILURE);
}
n = atoi(value);
new = malloc(sizeof(stack_t));
if (!new)
{
dprintf(STDERR_FILENO, "Error: malloc failed\n");
fclose(strct.file);
free(strct.line);
free_stck(strct.stack);
exit(EXIT_FAILURE);
}
new->n = n;
new->next = NULL;
if (!current)
{
new->prev = NULL;
strct.stack = new;
return (strct);
}
while (current->next)
{
current = current->next;
}
current->next = new;
new->prev = current;
return (strct);
}
/**
* push1 - add node in stack_t
* @value: value's node
* @strct: strct_t
* Return: strct_t
*/
strct_t push1(char *value, strct_t strct)
{
register int n;
stack_t *new;
char *ptr = NULL;
ptr = strchr(value, '\n');
if (ptr)
*ptr = 0;
if (!strlen(value) || !_isdigit(value))
{
dprintf(STDERR_FILENO, "L%d: usage: push integer\n", strct.line_number);
fclose(strct.file);
free(strct.line);
free_stck(strct.stack);
exit(EXIT_FAILURE);
}
n = atoi(value);
new = malloc(sizeof(stack_t));
if (!new)
{
dprintf(STDERR_FILENO, "Error: malloc failed\n");
fclose(strct.file);
free(strct.line);
free_stck(strct.stack);
exit(EXIT_FAILURE);
}
new->n = n;
new->next = strct.stack;
new->prev = NULL;
if (new->next)
new->next->prev = new;
strct.stack = new;
return (strct);
}
/**
* pall - print all stack_t
* @stack: head stack
* @line_number: line number
* Return: (void)
*/
void pall(stack_t **stack, unsigned int line_number)
{
unsigned int i = 0;
stack_t *current = *stack;
(void)line_number;
if (current == NULL)
{
return;
}
/*
* while (current->next)
* {
* current = current->next;
* }
* while (current)
* {
* printf("%d\n", current->n);
* current = current->prev;
* }
*/
for (i = 0; current; i++, current = current->next)
{
printf("%d\n", current->n);
}
}