-
Notifications
You must be signed in to change notification settings - Fork 0
/
monty1.c
44 lines (38 loc) · 811 Bytes
/
monty1.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
#include "monty.h"
/**
* add - Adds the top two elements of the stack.
* @stack: pointer to head of stack
* @line_num: file's line number
* Return: Void
*/
void add(stack_t **stack, unsigned int line_num)
{
stack_t *h = *stack, *n;
if ((*stack) == NULL || (*stack)->next == NULL)
{
fprintf(stderr, "L%u: can't add, stack too short\n", line_num);
exit(EXIT_FAILURE);
}
if (*stack && (*stack)->next)
{
n = h->next;
n->n += h->n;
free(h);
*stack = n;
}
}
/**
* pint - Prints value at top of stack.
* @stack: pointer to head of stack
* @line_num: file's line number
* Return: Void
*/
void pint(stack_t **stack, unsigned int line_num)
{
if (*stack == NULL)
{
fprintf(stderr, "L%u: can't pint, stack empty\n", line_num);
exit(EXIT_FAILURE);
}
printf("%d\n", (*stack)->n);
}