-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstack_imple2.c
60 lines (57 loc) · 1.2 KB
/
stack_imple2.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
#include "monty.h"
/**
* pop_element - Removes the top element from the stack.
* @top: Pointer to the top of the stack.
* @value: Unused parameter (required by function pointer type).
*
*/
void pop_element(stack_t **top, unsigned int value)
{
stack_t *temp;
(void)value;
if (!(*top))
{
fprintf(stderr, "L%d: can't pop an empty stack", line_number);
exit(EXIT_FAILURE);
}
temp = *top;
temp = temp->next;
free(*top);
*top = temp;
if (*top)
(*top)->prev = NULL;
}
/**
* swap_element - Swaps the top two elements of the stack.
* @top: Pointer to the top of the stack.
* @value: Unused parameter (required by function pointer type).
*
*/
void swap_element(stack_t **top, unsigned int value)
{
stack_t *temp;
int temp_value;
(void)value;
if (!*top || !(*top)->next)
{
fprintf(stderr, "L%d: can't swap, stack too short", line_number);
exit(EXIT_FAILURE);
}
else
{
temp = (*top)->next;
temp_value = (*top)->n;
(*top)->n = temp->n;
temp->n = temp_value;
}
}
/**
* nop_element - Does nothing.
* @top: Pointer to the top of the stack.
* @value: Unused parameter (required by function pointer type).
*/
void nop_element(stack_t **top, unsigned int value)
{
(void)top;
(void)value;
}