-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_position_operations.c
78 lines (71 loc) · 1.37 KB
/
stack_position_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
#include "monty.h"
/**
* swap - swap locations of previous stack with the top stack
* @h: node to be swapped
* @line_number: node number
*/
void swap(stack_t **h, unsigned int line_number)
{
stack_t *tmp = NULL;
if (*h == NULL || (*h)->next == NULL)
{
printf("L%u: can't swap, stack too short\n", line_number);
exit(EXIT_FAILURE);
}
tmp = (*h)->next;
if (tmp->next != NULL)
{
(*h)->next = tmp->next;
(*h)->next->prev = *h;
}
else
{
tmp->prev->prev = tmp;
tmp->prev->next = NULL;
}
tmp->prev = NULL;
tmp->next = *h;
(*h) = tmp;
}
/**
* rotl - rotate so top of stack becomes last one, second becomes first one
* @h: node to be rotated
* @line_number: node number
*/
void rotl(stack_t **h, unsigned int line_number)
{
stack_t *tmp;
(void) line_number;
if ((*h)->next != NULL)
{
tmp = *h;
while (tmp->next != NULL)
tmp = tmp->next;
(*h)->prev = tmp;
tmp->next = *h;
(*h)->next->prev = NULL;
*h = (*h)->next;
tmp->next->next = NULL;
}
}
/**
* rotr - rotate so only bottom node of stack becomes first one
* @h: node to be rotated
* @line_number: node number
*/
void rotr(stack_t **h, unsigned int line_number)
{
stack_t *tmp;
(void) line_number;
if ((*h)->next != NULL)
{
tmp = *h;
while (tmp->next != NULL)
tmp = tmp->next;
(*h)->prev = tmp;
tmp->next = *h;
tmp->prev->next = NULL;
tmp->prev = NULL;
(*h) = (*h)->prev;
}
}