-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1-node_ops.c
More file actions
60 lines (55 loc) · 1.08 KB
/
1-node_ops.c
File metadata and controls
60 lines (55 loc) · 1.08 KB
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
#include "monty.h"
/**
* nop - function for the nop opcode
* @head: head stack
* @line_num: line number
*/
void nop(stack_t **head, unsigned int line_num)
{
(void)head;
(void)line_num;
}
/**
* swap - function that swap the top 2 elements of the stack
* @head: head stack
* @line_num: line number
*/
void swap(stack_t **head, unsigned int line_num)
{
stack_t *temp;
int pos = 0;
temp = *head;
if (temp == NULL || temp->next == NULL)
{
fprintf(stderr, "L%d: can't swap, stack too short\n", line_num);
free_stack(*head);
exit(EXIT_FAILURE);
}
else
{
pos = temp->n;
temp->n = temp->next->n;
temp->next->n = pos;
}
}
/**
* add - funtion that add the top 2 elements of the stack
* @head: head stack
* @line_num: line number
*/
void add(stack_t **head, unsigned int line_num)
{
stack_t *temp = NULL;
int pos = 0;
if (!*head || !(*head)->next)
{
fprintf(stderr, "L%d: can't add, stack too short\n", line_num);
free_stack(*head);
exit(EXIT_FAILURE);
}
temp = (*head)->next;
pos = (*head)->n;
pos += (*head)->next->n;
pop(head, line_num);
temp->n = pos;
}