This repository was archived by the owner on Oct 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswap.c
78 lines (71 loc) · 1.9 KB
/
swap.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
/* ************************************************************************** */
/* */
/* :::::::: */
/* swap.c :+: :+: */
/* +:+ */
/* By: fbes <fbes@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2021/06/10 18:40:34 by fbes #+# #+# */
/* Updated: 2021/11/01 21:46:13 by fbes ######## odam.nl */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "push_swap.h"
/**
* Swap the first two links of a stack
* @param *s The stack to swap from
*/
void swap(t_stack *s)
{
t_link *temp;
if (s->size > 1)
{
temp = s->top;
s->top = temp->next;
temp->next = s->top->next;
s->top->next = temp;
s->top->prev = NULL;
s->top->next->prev = s->top;
s->top->next->next->prev = s->top->next;
}
}
/**
* Swap the first two elements of stack A and print sa
* @param *a Stack A
*/
void sa(t_stack *a)
{
swap(a);
write(1, "sa\n", 3);
}
/**
* Swap the first two elements of stack B and print sb
* @param *b Stack B
*/
void sb(t_stack *b)
{
swap(b);
write(1, "sb\n", 3);
}
/**
* Swap the first two elements of both stack A and stack B and print ss
* @param *a Stack A
* @param *b Stack B
*/
void ss(t_stack *a, t_stack *b)
{
swap(a);
swap(b);
write(1, "ss\n", 3);
}
/**
* Swap the first two elements of a stack and print s<s->id>
* @param *s A stack
*/
void sf(t_stack *s)
{
swap(s);
write(1, "s", 1);
write(1, &(s->id), 1);
write(1, "\n", 1);
}